Kai Tietz | 518dd33 | 2007-08-10 09:54:15 +0000 | [diff] [blame^] | 1 | /*
|
| 2 | nexttoward.c
|
| 3 | Contributed by Danny Smith <dannysmith@users.sourceforge.net>
|
| 4 | No copyright claimed, absolutely no warranties.
|
| 5 |
|
| 6 | 2005-05-10
|
| 7 | */
|
| 8 |
|
| 9 | #include <math.h>
|
| 10 |
|
| 11 | double
|
| 12 | nexttoward (double x, long double y)
|
| 13 | {
|
| 14 | union
|
| 15 | {
|
| 16 | double d;
|
| 17 | unsigned long long ll;
|
| 18 | } u;
|
| 19 |
|
| 20 | long double xx = x;
|
| 21 |
|
| 22 | if (isnan (y) || isnan (x))
|
| 23 | return x + y;
|
| 24 |
|
| 25 | if (xx == y)
|
| 26 | /* nextafter (0.0, -O.0) should return -0.0. */
|
| 27 | return y;
|
| 28 | u.d = x;
|
| 29 | if (x == 0.0)
|
| 30 | {
|
| 31 | u.ll = 1;
|
| 32 | return y > 0.0L ? u.d : -u.d;
|
| 33 | }
|
| 34 |
|
| 35 | /* Non-extended encodings are lexicographically ordered,
|
| 36 | with implicit "normal" bit. */
|
| 37 | if (((x > 0.0) ^ (y > xx)) == 0)
|
| 38 | u.ll++;
|
| 39 | else
|
| 40 | u.ll--;
|
| 41 | return u.d;
|
| 42 | }
|