From d54fe7c1f704a63824c5bfa0ece65245572e9b27 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Wed, 4 Mar 2015 21:21:30 -0500 Subject: Initial commit --- src/libcf/cf_nint.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/libcf/cf_nint.c (limited to 'src/libcf/cf_nint.c') diff --git a/src/libcf/cf_nint.c b/src/libcf/cf_nint.c new file mode 100644 index 0000000..8488911 --- /dev/null +++ b/src/libcf/cf_nint.c @@ -0,0 +1,47 @@ +/***************************************************************************** + * Johns Hopkins University + * Center For Astrophysical Sciences + * FUSE + ***************************************************************************** + * + * Synopsis: n = cf_nint(x) + * + * Description: Converts double to nearest integer. + * + * Variables: double x A double + * + * Return: int n Nearest integer + * + * History: 08/25/03 wvd v1.1 Begin work + * 02/09/04 wvd v1.2 Add cf_nlong() + * 02/17/04 wvd v1.3 Test for overflow of int, long + * 02/18/04 wvd v1.4 Change format of error message. + * Change from values.h to limits.h + * + *************************************************************************/ + +#include +#include "calfuse.h" + +int +cf_nint(double x) + +{ + if (x > INT_MAX || x < INT_MIN) + cf_if_error("Cannot convert %10.4e to an integer", x); + + if (x < 0.) return (int) (x - 0.5); + else return (int) (x + 0.5); +} + + +long +cf_nlong(double x) + +{ + if (x > LONG_MAX || x < LONG_MIN) + cf_if_error("Cannot convert %10.4e to a long", x); + + if (x < 0.) return (long) (x - 0.5); + else return (long) (x + 0.5); +} -- cgit