diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/numeric%2Fmath%2Fremquo.html | |
new repository
Diffstat (limited to 'devdocs/c/numeric%2Fmath%2Fremquo.html')
| -rw-r--r-- | devdocs/c/numeric%2Fmath%2Fremquo.html | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/devdocs/c/numeric%2Fmath%2Fremquo.html b/devdocs/c/numeric%2Fmath%2Fremquo.html new file mode 100644 index 00000000..aef01c0a --- /dev/null +++ b/devdocs/c/numeric%2Fmath%2Fremquo.html @@ -0,0 +1,99 @@ + <h1 id="firstHeading" class="firstHeading">remquo, remquof, remquol</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 remquof( float x, float y, int *quo );</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 remquo( double x, double y, int *quo );</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 remquol( long double x, long double y, int *quo );</pre> +</td> <td> (3) </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 remquo( x, y, quo )</pre> +</td> <td> (4) </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> Computes the floating-point remainder of the division operation <code>x/y</code> as the <code><a href="remainder" title="c/numeric/math/remainder">remainder()</a></code> function does. Additionally, the sign and at least the three of the last bits of <code>x/y</code> will be stored in <code>quo</code>, sufficient to determine the octant of the result within a period.</div> <div class="t-li1"> +<span class="t-li">4)</span> Type-generic macro: If any non-pointer argument has type <code>long double</code>, <code>remquol</code> is called. Otherwise, if any non-pointer argument has integer type or has type <code>double</code>, <code>remquo</code> is called. Otherwise, <code>remquof</code> is called.</div> <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> x, y </td> <td> - </td> <td> floating point values </td> +</tr> <tr class="t-par"> <td> quo </td> <td> - </td> <td> pointer to an integer value to store the sign and some bits of <code>x/y</code> </td> +</tr> +</table> <h3 id="Return_value"> Return value</h3> <p>If successful, returns the floating-point remainder of the division <code>x/y</code> as defined in <code><a href="remainder" title="c/numeric/math/remainder">remainder</a></code>, and stores, in <code>*quo</code>, the sign and at least three of the least significant bits of <code>x/y</code> (formally, stores a value whose sign is the sign of <code>x/y</code> and whose magnitude is congruent modulo 2<sup class="t-su">n</sup> to the magnitude of the integral quotient of <code>x/y</code>, where n is an implementation-defined integer greater than or equal to 3).</p> +<p>If <code>y</code> is zero, the value stored in <code>*quo</code> is unspecified.</p> +<p>If a domain error occurs, an implementation-defined value is returned (NaN where supported).</p> +<p>If a range error occurs due to underflow, the correct result is returned if subnormals are supported.</p> +<p>If <code>y</code> is zero, but the domain error does not occur, zero 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>Domain error may occur if <code>y</code> is zero.</p> +<p>If the implementation supports IEEE floating-point arithmetic (IEC 60559),</p> +<ul> +<li> The current <a href="../fenv/fe_round" title="c/numeric/fenv/FE round">rounding mode</a> has no effect. </li> +<li> <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INEXACT</a></code> is never raised </li> +<li> If <code>x</code> is ±∞ and <code>y</code> is not NaN, NaN is returned and <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INVALID</a></code> is raised </li> +<li> If <code>y</code> is ±0 and <code>x</code> is not NaN, NaN is returned and <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INVALID</a></code> is raised </li> +<li> If either <code>x</code> or <code>y</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/remquo.html">POSIX requires</a> that a domain error occurs if <code>x</code> is infinite or <code>y</code> is zero.</p> +<p>This function is useful when implementing periodic functions with the period exactly representable as a floating-point value: when calculating sin(πx) for a very large <code>x</code>, calling <code><a href="sin" title="c/numeric/math/sin">sin</a></code> directly may result in a large error, but if the function argument is first reduced with <code>remquo</code>, the low-order bits of the quotient may be used to determine the sign and the octant of the result within the period, while the remainder may be used to calculate the value with high precision.</p> +<p>On some platforms this operation is supported by hardware (and, for example, on Intel CPU, <code>FPREM1</code> leaves exactly 3 bits of precision in the quotient)</p> +<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h> +#include <math.h> +#include <fenv.h> +#ifndef __GNUC__ +#pragma STDC FENV_ACCESS ON +#endif +double cos_pi_x_naive(double x) +{ + const double pi = acos(-1); + return cos(pi * x); +} +// the period is 2, values are (0;0.5) positive, (0.5;1.5) negative, (1.5,2) positive +double cos_pi_x_smart(double x) +{ + const double pi = acos(-1); + int extremum; + double rem = remquo(x, 1, &extremum); + extremum = (unsigned)extremum % 2; // keep 1 bit to determine nearest extremum + return extremum ? -cos(pi * rem) : cos(pi * rem); +} +int main(void) +{ + printf("cos(pi * 0.25) = %f\n", cos_pi_x_naive(0.25)); + printf("cos(pi * 1.25) = %f\n", cos_pi_x_naive(1.25)); + printf("cos(pi * 1000000000000.25) = %f\n", cos_pi_x_naive(1000000000000.25)); + printf("cos(pi * 1000000000001.25) = %f\n", cos_pi_x_naive(1000000000001.25)); + printf("cos(pi * 1000000000000.25) = %f\n", cos_pi_x_smart(1000000000000.25)); + printf("cos(pi * 1000000000001.25) = %f\n", cos_pi_x_smart(1000000000001.25)); + // error handling + feclearexcept(FE_ALL_EXCEPT); + int quo; + printf("remquo(+Inf, 1) = %.1f\n", remquo(INFINITY, 1, &quo)); + if(fetestexcept(FE_INVALID)) puts(" FE_INVALID raised"); +}</pre></div> <p>Possible output:</p> +<div class="text source-text"><pre data-language="c">cos(pi * 0.25) = 0.707107 +cos(pi * 1.25) = -0.707107 +cos(pi * 1000000000000.25) = 0.707123 +cos(pi * 1000000000001.25) = -0.707117 +cos(pi * 1000000000000.25) = 0.707107 +cos(pi * 1000000000001.25) = -0.707107 +remquo(+Inf, 1) = -nan + FE_INVALID raised</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C17 standard (ISO/IEC 9899:2018): </li> +<ul> +<li> 7.12.10.3 The remquo functions (p: 186) </li> +<li> 7.25 Type-generic math <tgmath.h> (p: 272-273) </li> +<li> F.10.7.3 The remquo functions (p: 385) </li> +</ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul> +<li> 7.12.10.3 The remquo functions (p: 255) </li> +<li> 7.25 Type-generic math <tgmath.h> (p: 373-375) </li> +<li> F.10.7.3 The remquo functions (p: 529) </li> +</ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul> +<li> 7.12.10.3 The remquo functions (p: 236) </li> +<li> 7.22 Type-generic math <tgmath.h> (p: 335-337) </li> +<li> F.9.7.3 The remquo functions (p: 465) </li> +</ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="div" title="c/numeric/math/div"> <span class="t-lines"><span>div</span><span>ldiv</span><span>lldiv</span></span></a></div> +<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> computes quotient and remainder of integer division <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td> <div><a href="fmod" title="c/numeric/math/fmod"> <span class="t-lines"><span>fmod</span><span>fmodf</span><span>fmodl</span></span></a></div> +<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> computes remainder of the floating-point division operation <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td> <div><a href="remainder" title="c/numeric/math/remainder"> <span class="t-lines"><span>remainder</span><span>remainderf</span><span>remainderl</span></span></a></div> +<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> computes signed remainder of the floating-point division operation <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/numeric/math/remquo" title="cpp/numeric/math/remquo">C++ documentation</a></span> for <code>remquo</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/remquo" class="_attribution-link">https://en.cppreference.com/w/c/numeric/math/remquo</a> + </p> +</div> |
