master
1/**
2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the mingw-w64 runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5 */
6/*
7 nexttoward.c
8 Contributed by Danny Smith <dannysmith@users.sourceforge.net>
9 No copyright claimed, absolutely no warranties.
10
11 2005-05-10
12*/
13
14#include <math.h>
15
16double
17nexttoward (double x, long double y)
18{
19 union
20 {
21 double d;
22 unsigned long long ll;
23 } u;
24
25 long double xx = x;
26
27 if (isnan (y) || isnan (x))
28 return x + y;
29
30 if (xx == y)
31 /* nextafter (0.0, -O.0) should return -0.0. */
32 return y;
33 u.d = x;
34 if (x == 0.0)
35 {
36 u.ll = 1;
37 return y > 0.0L ? u.d : -u.d;
38 }
39
40 /* Non-extended encodings are lexicographically ordered,
41 with implicit "normal" bit. */
42 if (((x > 0.0) ^ (y > xx)) == 0)
43 u.ll++;
44 else
45 u.ll--;
46 return u.d;
47}