-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfloats.cpp
59 lines (49 loc) · 1.5 KB
/
floats.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* Float manipulation functions
*
* Copyright (C) 2023 Markus Wallerberger and others
* SPDX-License-Identifier: MIT
*/
#include "xprec/ddouble.hpp"
#include "xprec/internal/utils.hpp"
#include "xprec/numbers.hpp"
#include <cassert>
#ifndef XPREC_API_EXPORT
#define XPREC_API_EXPORT
#endif
namespace xprec {
XPREC_API_EXPORT
DDouble nextafter(DDouble x, DDouble y)
{
using dd_limits = std::numeric_limits<DDouble>;
// Handle no-ops
if (x == y)
return x;
// direction
double dir = x > y ? -INFINITY : INFINITY;
// There are two ways to define z = nextafter(x, +INFINITY):
// 1. as the smallest value z for which z != x
// 2. as the smallest value z for which z - x != 0
//
// Both definitions are not applicable for x = -0.0, where z = 0.0, but
// are otherwise equivalent. The complication for DDouble is that due to
// the "varying" epsilon, definition 2 is stronger than 1 (sweeps across
// fewer numbers).
double lo;
if (x.lo() == 0)
lo = std::copysign(std::numeric_limits<double>::min(), dir);
else
lo = std::nextafter(x.lo(), dir);
// This should work since the representation is unique
DDouble z = ExDouble(x.hi()).add_small(lo);
// Handle overflows
if (!isfinite(z)) {
if (isinf(x) && signbit(x) != signbit(y))
return copysign(dd_limits::max(), x);
else
return std::nextafter(x.hi(), dir);
}
// Otherwise, simply return
assert (z != x);
return z;
}
}