Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      remquo, remquof, remquol

      From cppreference.com
      <c‎ |numeric‎ |math
       
       
       
      Common mathematical functions
      Functions
      Basic operations
      remquo
      (C99)
      (C99)
      (C99)
      (C99)(C99)(C99)(C23)
      Maximum/minimum operations
      (C99)
      (C99)
      Exponential functions
      (C23)
      (C99)
      (C99)
      (C23)
      (C23)

      (C99)
      (C99)(C23)
      (C23)
      (C23)
      Power functions
      (C99)
      (C23)
      (C23)

      (C99)
      (C23)
      (C23)
      Trigonometric and hyperbolic functions
      (C23)
      (C23)
      (C23)
      (C23)
      (C99)
      (C99)
      (C99)
      Nearest integer floating-point
      (C99)(C99)(C99)
      (C23)(C23)(C23)(C23)
      Floating-point manipulation
      (C99)(C99)
      (C99)(C23)
      (C99)
      Narrowing operations
      (C23)
      (C23)
      (C23)
      (C23)
      (C23)
      (C23)
      Quantum and quantum exponent
      Decimal re-encoding functions
      Total order and payload functions
      Classification
      Error and gamma functions
      (C99)
      (C99)
      (C99)
      (C99)
      Types
      Macro constants
      Special floating-point values
      (C99)(C23)
      Arguments and return values
      Error handling
      Fast operation indicators
       
      Defined in header<math.h>
      float       remquof(float x,float y,int*quo);
      (1)(since C99)
      double      remquo(double x,double y,int*quo);
      (2)(since C99)
      longdouble remquol(longdouble x,longdouble y,int*quo);
      (3)(since C99)
      Defined in header<tgmath.h>
      #define remquo( x, y, quo )
      (4)(since C99)
      1-3) Computes the floating-point remainder of the division operationx/y as theremainder() function does. Additionally, the sign and at least the three of the last bits ofx/y will be stored inquo, sufficient to determine the octant of the result within a period.
      4) Type-generic macro: If any non-pointer argument has typelongdouble,remquol is called. Otherwise, if any non-pointer argument has integer type or has typedouble,remquo is called. Otherwise,remquof is called.

      Contents

      [edit]Parameters

      x, y - floating-point values
      quo - pointer to an integer value to store the sign and some bits ofx/y

      [edit]Return value

      If successful, returns the floating-point remainder of the divisionx/y as defined inremainder, and stores, in*quo, the sign and at least three of the least significant bits ofx/y (formally, stores a value whose sign is the sign ofx/y and whose magnitude is congruentmodulo 2n
      to the magnitude of the integral quotient ofx/y, wheren is an implementation-defined integer greater than or equal to3).

      Ify is zero, the value stored in*quo is unspecified.

      If a domain error occurs, an implementation-defined value is returned (NaN where supported).

      If a range error occurs due to underflow, the correct result is returned if subnormals are supported.

      Ify is zero, but the domain error does not occur, zero is returned.

      [edit]Error handling

      Errors are reported as specified inmath_errhandling.

      Domain error may occur ify is zero.

      If the implementation supports IEEE floating-point arithmetic (IEC 60559),

      • The currentrounding mode has no effect.
      • FE_INEXACT is never raised
      • Ifx is ±∞ andy is not NaN, NaN is returned andFE_INVALID is raised
      • Ify is ±0 andx is not NaN, NaN is returned andFE_INVALID is raised
      • If eitherx ory is NaN, NaN is returned

      [edit]Notes

      POSIX requires that a domain error occurs ifx is infinite ory is zero.

      This function is useful when implementing periodic functions with the period exactly representable as a floating-point value: when calculatingsin(πx) for a very largex, callingsin directly may result in a large error, but if the function argument is first reduced withremquo, 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.

      On some platforms this operation is supported by hardware (and, for example, on Intel CPU,FPREM1 leaves exactly 3 bits of precision in the quotient).

      [edit]Example

      Run this code
      #include <fenv.h>#include <math.h>#include <stdio.h> #ifndef __GNUC__#pragma STDC FENV_ACCESS ON#endif double cos_pi_x_naive(double x){constdouble pi=acos(-1);returncos(pi* x);} // the period is 2, values are (0;0.5) positive, (0.5;1.5) negative, (1.5,2) positivedouble cos_pi_x_smart(double x){constdouble pi=acos(-1);int extremum;double rem= remquo(x,1,&extremum);    extremum=(unsigned)extremum%2;// keep 1 bit to determine nearest extremumreturn 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 handlingfeclearexcept(FE_ALL_EXCEPT);int quo;printf("remquo(+Inf, 1) = %.1f\n", remquo(INFINITY,1,&quo));if(fetestexcept(FE_INVALID))puts("    FE_INVALID raised");}

      Possible output:

      cos(pi * 0.25) = 0.707107cos(pi * 1.25) = -0.707107cos(pi * 1000000000000.25) = 0.707123cos(pi * 1000000000001.25) = -0.707117cos(pi * 1000000000000.25) = 0.707107cos(pi * 1000000000001.25) = -0.707107 remquo(+Inf, 1) = -nan    FE_INVALID raised

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 7.12.10.3 The remquo functions (p: TBD)
      • 7.25 Type-generic math <tgmath.h> (p: TBD)
      • F.10.7.3 The remquo functions (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 7.12.10.3 The remquo functions (p: 186)
      • 7.25 Type-generic math <tgmath.h> (p: 272-273)
      • F.10.7.3 The remquo functions (p: 385)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.12.10.3 The remquo functions (p: 255)
      • 7.25 Type-generic math <tgmath.h> (p: 373-375)
      • F.10.7.3 The remquo functions (p: 529)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.12.10.3 The remquo functions (p: 236)
      • 7.22 Type-generic math <tgmath.h> (p: 335-337)
      • F.9.7.3 The remquo functions (p: 465)

      [edit]See also

      computes quotient and remainder of integer division
      (function)[edit]
      (C99)(C99)
      computes remainder of the floating-point division operation
      (function)[edit]
      computes signed remainder of the floating-point division operation
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/numeric/math/remquo&oldid=172057"

      [8]ページ先頭

      ©2009-2025 Movatter.jp