diff options
Diffstat (limited to 'nuttx/lib')
-rw-r--r-- | nuttx/lib/lib_rint.c | 86 |
1 files changed, 49 insertions, 37 deletions
diff --git a/nuttx/lib/lib_rint.c b/nuttx/lib/lib_rint.c index 632c6632d..a0f7dbd4f 100644 --- a/nuttx/lib/lib_rint.c +++ b/nuttx/lib/lib_rint.c @@ -41,6 +41,7 @@ * Included Files ************************************************************/ +#include <nuttx/config.h> #include <sys/types.h> #include <stdlib.h> @@ -74,50 +75,61 @@ double_t rint(double_t x) { - double_t retValue; + double_t ret; -/* If the current rounding mode rounds toward negative - * infinity, rint() is identical to floor(). If the current - * rounding mode rounds toward positive infinity, rint() is - * identical to ceil(). */ -#if ((defined(FP_ROUND_POSITIVE)) && (FP_ROUNDING_POSITIVE != 0)) - retValue = ceil(x); + /* If the current rounding mode rounds toward negative + * infinity, rint() is identical to floor(). If the current + * rounding mode rounds toward positive infinity, rint() is + * identical to ceil(). + */ -#elif ((defined(FP_ROUND_NEGATIVE)) && (FP_ROUNDING_NEGATIVE != 0)) - retValue = floor(x); +#if defined(CONFIG_FP_ROUND_POSITIVE) && CONFIG_FP_ROUNDING_POSITIVE != 0 -#else - - /* In the default rounding mode (round to nearest), rint(x) is the - * integer nearest x with the additional stipulation that if - * |rint(x)-x|=1/2, then rint(x) is even. */ - - long dwInteger = (long)x; - double_t fRemainder = x - (double_t)dwInteger; + ret = ceil(x); - if (x < 0.0) { +#elif defined(CONFIG_FP_ROUND_NEGATIVE) && CONFIG_FP_ROUNDING_NEGATIVE != 0 - /* fRemainder should be in range 0 .. -1 */ - if (fRemainder == -0.5) - dwInteger = ((dwInteger+1)&~1); - else if (fRemainder < -0.5) { - dwInteger--; + ret = floor(x); - } /* end if */ - } /* end if */ - else { - - /* fRemainder should be in range 0 .. 1 */ - if (fRemainder == 0.5) - dwInteger = ((dwInteger+1)&~1); - else if (fRemainder > 0.5) { - dwInteger++; - - } /* end if */ - } /* end else */ +#else - retValue = (double_t)dwInteger; + /* In the default rounding mode (round to nearest), rint(x) is the + * integer nearest x with the additional stipulation that if + * |rint(x)-x|=1/2, then rint(x) is even. + */ + + long dwinteger = (long)x; + double_t fremainder = x - (double_t)dwinteger; + + if (x < 0.0) + { + /* fremainder should be in range 0 .. -1 */ + + if (fremainder == -0.5) + { + dwinteger = ((dwinteger+1)&~1); + } + else if (fremainder < -0.5) + { + dwinteger--; + } + } + else + { + /* fremainder should be in range 0 .. 1 */ + + if (fremainder == 0.5) + { + dwinteger = ((dwinteger+1)&~1); + } + else if (fremainder > 0.5) + { + dwinteger++; + } + } + + ret = (double_t)dwinteger; #endif - return retValue; + return ret; } |