Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8fe4aef

Browse files
committed
Replace internal C function pg_hypot() by standard hypot()
The code comment said, "It is expected that this routine willeventually be replaced with the C99 hypot() function.", so let's dothat now.This function is tested via the geometry regression test, so if it isfaulty on any platform, it will show up there.Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>Discussion:https://www.postgresql.org/message-id/flat/170308e6-a7a3-4484-87b2-f960bb564afa%40eisentraut.org
1 parent47c7a7e commit8fe4aef

File tree

4 files changed

+6
-82
lines changed

4 files changed

+6
-82
lines changed

‎src/backend/access/spgist/spgproc.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ point_box_distance(Point *point, BOX *box)
5151
else
5252
dy=0.0;
5353

54-
returnHYPOT(dx,dy);
54+
returnhypot(dx,dy);
5555
}
5656

5757
/*

‎src/backend/utils/adt/geo_ops.c‎

Lines changed: 4 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ line_distance(PG_FUNCTION_ARGS)
12761276

12771277
PG_RETURN_FLOAT8(float8_div(fabs(float8_mi(l1->C,
12781278
float8_mul(ratio,l2->C))),
1279-
HYPOT(l1->A,l1->B)));
1279+
hypot(l1->A,l1->B)));
12801280
}
12811281

12821282
/* line_interpt()
@@ -2001,7 +2001,7 @@ point_distance(PG_FUNCTION_ARGS)
20012001
staticinlinefloat8
20022002
point_dt(Point*pt1,Point*pt2)
20032003
{
2004-
returnHYPOT(float8_mi(pt1->x,pt2->x),float8_mi(pt1->y,pt2->y));
2004+
returnhypot(float8_mi(pt1->x,pt2->x),float8_mi(pt1->y,pt2->y));
20052005
}
20062006

20072007
Datum
@@ -5005,7 +5005,7 @@ circle_mul_pt(PG_FUNCTION_ARGS)
50055005
result= (CIRCLE*)palloc(sizeof(CIRCLE));
50065006

50075007
point_mul_point(&result->center,&circle->center,point);
5008-
result->radius=float8_mul(circle->radius,HYPOT(point->x,point->y));
5008+
result->radius=float8_mul(circle->radius,hypot(point->x,point->y));
50095009

50105010
PG_RETURN_CIRCLE_P(result);
50115011
}
@@ -5020,7 +5020,7 @@ circle_div_pt(PG_FUNCTION_ARGS)
50205020
result= (CIRCLE*)palloc(sizeof(CIRCLE));
50215021

50225022
point_div_point(&result->center,&circle->center,point);
5023-
result->radius=float8_div(circle->radius,HYPOT(point->x,point->y));
5023+
result->radius=float8_div(circle->radius,hypot(point->x,point->y));
50245024

50255025
PG_RETURN_CIRCLE_P(result);
50265026
}
@@ -5492,71 +5492,3 @@ plist_same(int npts, Point *p1, Point *p2)
54925492

54935493
return false;
54945494
}
5495-
5496-
5497-
/*-------------------------------------------------------------------------
5498-
* Determine the hypotenuse.
5499-
*
5500-
* If required, x and y are swapped to make x the larger number. The
5501-
* traditional formula of x^2+y^2 is rearranged to factor x outside the
5502-
* sqrt. This allows computation of the hypotenuse for significantly
5503-
* larger values, and with a higher precision than when using the naive
5504-
* formula. In particular, this cannot overflow unless the final result
5505-
* would be out-of-range.
5506-
*
5507-
* sqrt( x^2 + y^2 ) = sqrt( x^2( 1 + y^2/x^2) )
5508-
* = x * sqrt( 1 + y^2/x^2 )
5509-
* = x * sqrt( 1 + y/x * y/x )
5510-
*
5511-
* It is expected that this routine will eventually be replaced with the
5512-
* C99 hypot() function.
5513-
*
5514-
* This implementation conforms to IEEE Std 1003.1 and GLIBC, in that the
5515-
* case of hypot(inf,nan) results in INF, and not NAN.
5516-
*-----------------------------------------------------------------------
5517-
*/
5518-
float8
5519-
pg_hypot(float8x,float8y)
5520-
{
5521-
float8yx,
5522-
result;
5523-
5524-
/* Handle INF and NaN properly */
5525-
if (isinf(x)||isinf(y))
5526-
returnget_float8_infinity();
5527-
5528-
if (isnan(x)||isnan(y))
5529-
returnget_float8_nan();
5530-
5531-
/* Else, drop any minus signs */
5532-
x=fabs(x);
5533-
y=fabs(y);
5534-
5535-
/* Swap x and y if needed to make x the larger one */
5536-
if (x<y)
5537-
{
5538-
float8temp=x;
5539-
5540-
x=y;
5541-
y=temp;
5542-
}
5543-
5544-
/*
5545-
* If y is zero, the hypotenuse is x. This test saves a few cycles in
5546-
* such cases, but more importantly it also protects against
5547-
* divide-by-zero errors, since now x >= y.
5548-
*/
5549-
if (y==0.0)
5550-
returnx;
5551-
5552-
/* Determine the hypotenuse */
5553-
yx=y /x;
5554-
result=x*sqrt(1.0+ (yx*yx));
5555-
5556-
if (unlikely(isinf(result)))
5557-
float_overflow_error();
5558-
if (unlikely(result==0.0))
5559-
float_underflow_error();
5560-
5561-
returnresult;
5562-
}

‎src/backend/utils/adt/geo_spgist.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ pointToRectBoxDistance(Point *point, RectBox *rect_box)
390390
else
391391
dy=0;
392392

393-
returnHYPOT(dx,dy);
393+
returnhypot(dx,dy);
394394
}
395395

396396

‎src/include/utils/geo_decls.h‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ FPge(double A, double B)
8888
#defineFPge(A,B)((A) >= (B))
8989
#endif
9090

91-
#defineHYPOT(A,B)pg_hypot(A, B)
9291

9392
/*---------------------------------------------------------------------
9493
* Point - (x,y)
@@ -275,11 +274,4 @@ CirclePGetDatum(const CIRCLE *X)
275274
#definePG_GETARG_CIRCLE_P(n) DatumGetCircleP(PG_GETARG_DATUM(n))
276275
#definePG_RETURN_CIRCLE_P(x) return CirclePGetDatum(x)
277276

278-
279-
/*
280-
* in geo_ops.c
281-
*/
282-
283-
externfloat8pg_hypot(float8x,float8y);
284-
285277
#endif/* GEO_DECLS_H */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp