[444] | 1 | |
---|
| 2 | /* @(#)e_remainder.c 5.1 93/09/24 */ |
---|
| 3 | /* |
---|
| 4 | * ==================================================== |
---|
| 5 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
---|
| 6 | * |
---|
| 7 | * Developed at SunPro, a Sun Microsystems, Inc. business. |
---|
| 8 | * Permission to use, copy, modify, and distribute this |
---|
| 9 | * software is freely granted, provided that this notice |
---|
| 10 | * is preserved. |
---|
| 11 | * ==================================================== |
---|
| 12 | */ |
---|
| 13 | |
---|
| 14 | /* |
---|
| 15 | FUNCTION |
---|
| 16 | <<remainder>>, <<remainderf>>---round and remainder |
---|
| 17 | INDEX |
---|
| 18 | remainder |
---|
| 19 | INDEX |
---|
| 20 | remainderf |
---|
| 21 | |
---|
| 22 | SYNOPSIS |
---|
| 23 | #include <math.h> |
---|
| 24 | double remainder(double <[x]>, double <[y]>); |
---|
| 25 | float remainderf(float <[x]>, float <[y]>); |
---|
| 26 | |
---|
| 27 | DESCRIPTION |
---|
| 28 | <<remainder>> and <<remainderf>> find the remainder of |
---|
| 29 | <[x]>/<[y]>; this value is in the range -<[y]>/2 .. +<[y]>/2. |
---|
| 30 | |
---|
| 31 | RETURNS |
---|
| 32 | <<remainder>> returns the integer result as a double. |
---|
| 33 | |
---|
| 34 | PORTABILITY |
---|
| 35 | <<remainder>> is a System V release 4. |
---|
| 36 | <<remainderf>> is an extension. |
---|
| 37 | |
---|
| 38 | */ |
---|
| 39 | |
---|
| 40 | /* remainder(x,p) |
---|
| 41 | * Return : |
---|
| 42 | * returns x REM p = x - [x/p]*p as if in infinite |
---|
| 43 | * precise arithmetic, where [x/p] is the (infinite bit) |
---|
| 44 | * integer nearest x/p (in half way case choose the even one). |
---|
| 45 | * Method : |
---|
| 46 | * Based on fmod() return x-[x/p]chopped*p exactlp. |
---|
| 47 | */ |
---|
| 48 | |
---|
| 49 | #include "fdlibm.h" |
---|
| 50 | |
---|
| 51 | #ifndef _DOUBLE_IS_32BITS |
---|
| 52 | |
---|
| 53 | #ifdef __STDC__ |
---|
| 54 | static const double zero = 0.0; |
---|
| 55 | #else |
---|
| 56 | static double zero = 0.0; |
---|
| 57 | #endif |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | #ifdef __STDC__ |
---|
| 61 | double remainder(double x, double p) |
---|
| 62 | #else |
---|
| 63 | double remainder(x,p) |
---|
| 64 | double x,p; |
---|
| 65 | #endif |
---|
| 66 | { |
---|
| 67 | __int32_t hx,hp; |
---|
| 68 | __uint32_t sx,lx,lp; |
---|
| 69 | double p_half; |
---|
| 70 | |
---|
| 71 | EXTRACT_WORDS(hx,lx,x); |
---|
| 72 | EXTRACT_WORDS(hp,lp,p); |
---|
| 73 | sx = hx&0x80000000; |
---|
| 74 | hp &= 0x7fffffff; |
---|
| 75 | hx &= 0x7fffffff; |
---|
| 76 | |
---|
| 77 | /* purge off exception values */ |
---|
| 78 | if((hp|lp)==0) return (x*p)/(x*p); /* p = 0 */ |
---|
| 79 | if((hx>=0x7ff00000)|| /* x not finite */ |
---|
| 80 | ((hp>=0x7ff00000)&& /* p is NaN */ |
---|
| 81 | (((hp-0x7ff00000)|lp)!=0))) |
---|
| 82 | return (x*p)/(x*p); |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | if (hp<=0x7fdfffff) x = fmod(x,p+p); /* now x < 2p */ |
---|
| 86 | if (((hx-hp)|(lx-lp))==0) return zero*x; |
---|
| 87 | x = fabs(x); |
---|
| 88 | p = fabs(p); |
---|
| 89 | if (hp<0x00200000) { |
---|
| 90 | if(x+x>p) { |
---|
| 91 | x-=p; |
---|
| 92 | if(x+x>=p) x -= p; |
---|
| 93 | } |
---|
| 94 | } else { |
---|
| 95 | p_half = 0.5*p; |
---|
| 96 | if(x>p_half) { |
---|
| 97 | x-=p; |
---|
| 98 | if(x>=p_half) x -= p; |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | GET_HIGH_WORD(hx,x); |
---|
| 102 | SET_HIGH_WORD(x,hx^sx); |
---|
| 103 | return x; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | #endif /* defined(_DOUBLE_IS_32BITS) */ |
---|