Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      copysign, copysignf, copysignl

      From cppreference.com
      <c‎ |numeric‎ |math
       
       
       
      Common mathematical functions
      Functions
      Basic operations
      (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)
      (C23)(C23)
      copysign
      (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       copysignf(float x,float y);
      (1)(since C99)
      double      copysign(double x,double y);
      (2)(since C99)
      longdouble copysignl(longdouble x,longdouble y);
      (3)(since C99)
      Defined in header<tgmath.h>
      #define copysign(x, y)
      (4)(since C99)
      1-3) Composes a floating-point value with the magnitude ofx and the sign ofy.
      4) Type-generic macro: If any argument has typelongdouble,copysignl is called. Otherwise, if any argument has integer type or has typedouble,copysign is called. Otherwise,copysignf is called.

      Contents

      [edit]Parameters

      x, y - floating-point values

      [edit]Return value

      If no errors occur, the floating-point value with the magnitude ofx and the sign ofy is returned.

      Ifx is NaN, then NaN with the sign ofy is returned.

      Ify is -0, the result is only negative if the implementation supports the signed zero consistently in arithmetic operations.

      [edit]Error handling

      This function is not subject to any errors specified inmath_errhandling.

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

      [edit]Notes

      copysign is the only portable way to manipulate the sign of a NaN value (to examine the sign of a NaN,signbit may also be used).

      [edit]Example

      Run this code
      #include <math.h>#include <stdio.h> int main(void){printf("copysign(1.0,+2.0)      = %+.1f\n", copysign(1.0,+2.0));printf("copysign(1.0,-2.0)      = %+.1f\n", copysign(1.0,-2.0));printf("copysign(INFINITY,-2.0) = %f\n",    copysign(INFINITY,-2.0));printf("copysign(NAN,-2.0)      = %f\n",    copysign(NAN,-2.0));}

      Possible output:

      copysign(1.0,+2.0)      = +1.0copysign(1.0,-2.0)      = -1.0copysign(INFINITY,-2.0) = -infcopysign(NAN,-2.0)      = -nan

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 7.12.11.1 The copysign functions (p: TBD)
      • 7.25 Type-generic math <tgmath.h> (p: TBD)
      • F.10.8.1 The copysign functions (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 7.12.11.1 The copysign functions (p: TBD)
      • 7.25 Type-generic math <tgmath.h> (p: TBD)
      • F.10.8.1 The copysign functions (p: TBD)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.12.11.1 The copysign functions (p: 255)
      • 7.25 Type-generic math <tgmath.h> (p: 373-375)
      • F.10.8.1 The copysign functions (p: 529)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.12.11.1 The copysign functions (p: 236)
      • 7.22 Type-generic math <tgmath.h> (p: 335-337)
      • F.9.8.1 The copysign functions (p: 465)

      [edit]See also

      (C99)(C99)
      computes absolute value of a floating-point value (\(\small{|x|}\)|x|)
      (function)[edit]
      (C99)
      checks if the given number is negative
      (function macro)[edit]
      C++ documentation forcopysign
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/numeric/math/copysign&oldid=172004"

      [8]ページ先頭

      ©2009-2025 Movatter.jp