aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKristian Evers <kristianevers@gmail.com>2018-05-06 17:38:52 +0300
committerKristian Evers <kristianevers@gmail.com>2018-05-08 09:16:05 +0200
commit987603a4ab2b925363fdcb6bf99fa89658d67613 (patch)
tree142678ab21e5cd3b792ff8a71ee27effcbb7ce79 /src
parent78f6a82a2ed9a3ad98f49e73c85a2d14aa9ee93c (diff)
downloadPROJ-987603a4ab2b925363fdcb6bf99fa89658d67613.tar.gz
PROJ-987603a4ab2b925363fdcb6bf99fa89658d67613.zip
Add round() and lround() to proj_math.h
Diffstat (limited to 'src')
-rw-r--r--src/pj_math.c28
-rw-r--r--src/proj_math.h1
2 files changed, 29 insertions, 0 deletions
diff --git a/src/pj_math.c b/src/pj_math.c
index 751c2750..97d4ae56 100644
--- a/src/pj_math.c
+++ b/src/pj_math.c
@@ -67,5 +67,33 @@ int pj_isnan (double x) {
return x != x;
}
+double pj_round(double x) {
+ /* The handling of corner cases is copied from boost; see
+ * https://github.com/boostorg/math/pull/8
+ * with improvements to return -0.0 when appropriate */
+ double t;
+ if (x == 0)
+ return x; /* Retain sign of 0 */
+ else if (0 < x && x < 0.5)
+ return +0.0;
+ else if (0 > x && x > -0.5)
+ return -0.0;
+ else if (x > 0) {
+ t = ceil(x);
+ return 0.5 < t - x ? t - 1 : t;
+ } else { /* Includes NaN */
+ t = floor(x);
+ return 0.5 < x - t ? t + 1 : t;
+ }
+}
+
+long pj_lround(double x) {
+ /* Default value for overflow + NaN + (x == LONG_MIN) */
+ long r = LONG_MIN;
+ x = round(x);
+ if (fabs(x) < -(double)LONG_MIN) /* Assume (double)LONG_MIN is exact */
+ r = (int)x;
+ return r;
+}
#endif /* !(defined(HAVE_C99_MATH) && HAVE_C99_MATH) */
diff --git a/src/proj_math.h b/src/proj_math.h
index 1e0c6217..ca3ab401 100644
--- a/src/proj_math.h
+++ b/src/proj_math.h
@@ -29,6 +29,7 @@
#define PROJ_MATH_H
#include <math.h>
+#include <limits.h>
#ifdef __cplusplus
extern "C" {