blob: f59b141ab0bfef8ea00668692128ef7e6e5b5e10 [file] [log] [blame]
Kai Tietz518dd332007-08-10 09:54:15 +00001/*
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
11double
12nexttoward (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}