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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
<h1 id="firstHeading" class="firstHeading">nextafter, nextafterf, nextafterl, nexttoward, nexttowardf, nexttowardl</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><math.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">float nextafterf( float from, float to );</pre>
</td> <td> (1) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">double nextafter( double from, double to );</pre>
</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">long double nextafterl( long double from, long double to );</pre>
</td> <td> (3) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">float nexttowardf( float from, long double to );</pre>
</td> <td> (4) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">double nexttoward( double from, long double to );</pre>
</td> <td> (5) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">long double nexttowardl( long double from, long double to );</pre>
</td> <td> (6) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dsc-header"> <th> Defined in header <code><tgmath.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">#define nextafter(from, to)</pre>
</td> <td> (7) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">#define nexttoward(from, to)</pre>
</td> <td> (8) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> </table> <div class="t-li1">
<span class="t-li">1-3)</span> First, converts both arguments to the type of the function, then returns the next representable value of <code>from</code> in the direction of <code>to</code>. If <code>from</code> equals to <code>to</code>, <code>to</code> is returned.</div> <div class="t-li1">
<span class="t-li">4-6)</span> First, converts the first argument to the type of the function, then returns the next representable value of <code>from</code> in the direction of <code>to</code>. If <code>from</code> equals to <code>to</code>, <code>to</code> is returned, converted from <code>long double</code> to the return type of the function without loss of range or precision.</div> <div class="t-li1">
<span class="t-li">7)</span> Type-generic macro: If any argument has type <code>long double</code>, <code>nextafterl</code> is called. Otherwise, if any argument has integer type or has type <code>double</code>, <code>nextafter</code> is called. Otherwise, <code>nextafterf</code> is called.</div> <div class="t-li1">
<span class="t-li">8)</span> Type-generic macro: If the argument <code>from</code> has type <code>long double</code>, <code>nexttowardl</code> is called. Otherwise, if <code>from</code> has integer type or the type <code>double</code>, <code>nexttoward</code> is called. Otherwise, <code>nexttowardf</code> is called.</div> <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> from, to </td> <td> - </td> <td> floating point values </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>If no errors occur, the next representable value of <code>from</code> in the direction of <code>to</code>. is returned. If <code>from</code> equals <code>to</code>, then <code>to</code> is returned, converted to the type of the function.</p>
<p>If a range error due to overflow occurs, <code><a href="huge_val" title="c/numeric/math/HUGE VAL">±HUGE_VAL</a></code>, <code>±HUGE_VALF</code>, or <code>±HUGE_VALL</code> is returned (with the same sign as <code>from</code>).</p>
<p>If a range error occurs due to underflow, the correct result is returned.</p>
<h3 id="Error_handling"> Error handling</h3> <p>Errors are reported as specified in <a href="math_errhandling" title="c/numeric/math/math errhandling"><code>math_errhandling</code></a>.</p>
<p>If the implementation supports IEEE floating-point arithmetic (IEC 60559),</p>
<ul>
<li> if <code>from</code> is finite, but the expected result is an infinity, raises <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INEXACT</a></code> and <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_OVERFLOW</a></code> </li>
<li> if <code>from</code> does not equal <code>to</code> and the result is subnormal or zero, raises <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INEXACT</a></code> and <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_UNDERFLOW</a></code> </li>
<li> in any case, the returned value is independent of the current rounding mode </li>
<li> if either <code>from</code> or <code>to</code> is NaN, NaN is returned </li>
</ul> <h3 id="Notes"> Notes</h3> <p><a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/nextafter.html">POSIX specifies</a> that the overflow and the underflow conditions are range errors (<code><a href="../../error/errno" title="c/error/errno">errno</a></code> may be set).</p>
<p>IEC 60559 recommends that <code>from</code> is returned whenever <code>from == to</code>. These functions return <code>to</code> instead, which makes the behavior around zero consistent: <code>nextafter(-0.0, +0.0)</code> returns <code>+0.0</code> and <code>nextafter(+0.0, -0.0)</code> returns <code>-0.0</code>.</p>
<p><code>nextafter</code> is typically implemented by manipulation of IEEE representation (<a rel="nofollow" class="external text" href="https://github.com/bminor/glibc/blob/master/math/s_nextafter.c">glibc</a> <a rel="nofollow" class="external text" href="https://github.com/ifduyue/musl/blob/master/src/math/nextafter.c">musl</a>).</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <math.h>
#include <stdio.h>
#include <float.h>
#include <fenv.h>
int main(void)
{
float from1 = 0, to1 = nextafterf(from1, 1);
printf("The next representable float after %.2f is %.20g (%a)\n", from1, to1, to1);
float from2 = 1, to2 = nextafterf(from2, 2);
printf("The next representable float after %.2f is %.20f (%a)\n", from2, to2, to2);
double from3 = nextafter(0.1, 0), to3 = 0.1;
printf("The number 0.1 lies between two valid doubles:\n"
" %.56f (%a)\nand %.55f (%a)\n", from3, from3, to3, to3);
// difference between nextafter and nexttoward:
long double dir = nextafterl(from1, 1); // first subnormal long double
float x = nextafterf(from1, dir); // first converts dir to float, giving 0
printf("Using nextafter, next float after %.2f (%a) is %.20g (%a)\n",
from1, from1, x, x);
x = nexttowardf(from1, dir);
printf("Using nexttoward, next float after %.2f (%a) is %.20g (%a)\n",
from1, from1, x, x);
// special values
{
#pragma STDC FENV_ACCESS ON
feclearexcept(FE_ALL_EXCEPT);
double from4 = DBL_MAX, to4 = nextafter(from4, INFINITY);
printf("The next representable double after %.2g (%a) is %.23f (%a)\n",
from4, from4, to4, to4);
if(fetestexcept(FE_OVERFLOW)) puts(" raised FE_OVERFLOW");
if(fetestexcept(FE_INEXACT)) puts(" raised FE_INEXACT");
} // end FENV_ACCESS block
float from5 = 0.0, to5 = nextafter(from5, -0.0);
printf("nextafter(+0.0, -0.0) gives %.2g (%a)\n", to5, to5);
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">The next representable float after 0.00 is 1.4012984643248170709e-45 (0x1p-149)
The next representable float after 1.00 is 1.00000011920928955078 (0x1.000002p+0)
The number 0.1 lies between two valid doubles:
0.09999999999999999167332731531132594682276248931884765625 (0x1.9999999999999p-4)
and 0.1000000000000000055511151231257827021181583404541015625 (0x1.999999999999ap-4)
Using nextafter, next float after 0.00 (0x0p+0) is 0 (0x0p+0)
Using nexttoward, next float after 0.00 (0x0p+0) is 1.4012984643248170709e-45 (0x1p-149)
The next representable double after 1.8e+308 (0x1.fffffffffffffp+1023) is inf (inf)
raised FE_OVERFLOW
raised FE_INEXACT
nextafter(+0.0, -0.0) gives -0 (-0x0p+0)</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.12.11.3 The nextafter functions (p: 187) </li>
<li> 7.12.11.4 The nexttoward functions (p: 187) </li>
<li> 7.25 Type-generic math <tgmath.h> (p: 272-273) </li>
<li> F.10.8.3 The nextafter functions (p: 386) </li>
<li> F.10.8.4 The nexttoward functions (p: 386) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.12.11.3 The nextafter functions (p: 256) </li>
<li> 7.12.11.4 The nexttoward functions (p: 257) </li>
<li> 7.25 Type-generic math <tgmath.h> (p: 373-375) </li>
<li> F.10.8.3 The nextafter functions (p: 529) </li>
<li> F.10.8.4 The nexttoward functions (p: 529) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.12.11.3 The nextafter functions (p: 237) </li>
<li> 7.12.11.4 The nexttoward functions (p: 238) </li>
<li> 7.22 Type-generic math <tgmath.h> (p: 335-337) </li>
<li> F.9.8.3 The nextafter functions (p: 466) </li>
<li> F.9.8.4 The nexttoward functions (p: 466) </li>
</ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/numeric/math/nextafter" title="cpp/numeric/math/nextafter">C++ documentation</a></span> for <code>nextafter</code> </td>
</tr> </table> <div class="_attribution">
<p class="_attribution-p">
© cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
<a href="https://en.cppreference.com/w/c/numeric/math/nextafter" class="_attribution-link">https://en.cppreference.com/w/c/numeric/math/nextafter</a>
</p>
</div>
|