Namespaces
Variants

modf, modff, modfl

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
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
Arguments and return values
Error handling
Fast operation indicators
헤더 파일에 정의됨 <math.h>
float modff ( float arg, float * iptr ) ;
(1) (C99 이후)
double modf ( double arg, double * iptr ) ;
(2)
long double modfl ( long double arg, long double * iptr ) ;
(3) (C99 이후)
1-3) 주어진 부동 소수점 값 arg 를 정수 부분과 분수 부분으로 분해하며, 각 부분은 arg 와 동일한 타입과 부호를 가집니다. 정수 부분(부동 소수점 형식)은 iptr 이 가리키는 객체에 저장됩니다.

목차

매개변수

arg - 부동 소수점 값
iptr - 정수 부분을 저장할 부동 소수점 값에 대한 포인터

반환값

오류가 발생하지 않으면, arg 와 동일한 부호를 갖는 소수 부분을 반환합니다. 정수 부분은 iptr 이 가리키는 값에 저장됩니다.

반환된 값과 * iptr 에 저장된 값의 합은 arg 가 됩니다(반올림 허용).

오류 처리

이 함수는 math_errhandling 에 지정된 어떤 오류에도 영향을 받지 않습니다.

구현이 IEEE 부동 소수점 연산(IEC 60559)을 지원하는 경우,

  • 만약 arg 가 ±0이면, ±0이 반환되고 ±0이 * iptr 에 저장됩니다.
  • 만약 arg 가 ±∞이면, ±0이 반환되고 ±∞가 * iptr 에 저장됩니다.
  • 만약 arg 가 NaN이면, NaN이 반환되고 NaN이 * iptr 에 저장됩니다.
  • 반환된 값은 정확하며, 현재 반올림 모드 는 무시됩니다.

참고 사항

이 함수는 다음과 같이 구현된 것처럼 동작합니다:

double modf(double value, double *iptr)
{
#pragma STDC FENV_ACCESS ON
    int save_round = fegetround();
    fesetround(FE_TOWARDZERO);
    *iptr = std::nearbyint(value);
    fesetround(save_round);
    return copysign(isinf(value) ? 0.0 : value - (*iptr), value);
}

예제

#include <float.h>
#include <math.h>
#include <stdio.h>
int main(void)
{
    double f = 123.45;
    printf("Given the number %.2f or %a in hex,\n", f, f);
    double f3;
    double f2 = modf(f, &f3);
    printf("modf() makes %.2f + %.2f\n", f3, f2);
    int i;
    f2 = frexp(f, &i);
    printf("frexp() makes %f * 2^%d\n", f2, i);
    i = ilogb(f);
    printf("logb()/ilogb() make %f * %d^%d\n", f / scalbn(1.0, i), FLT_RADIX, i);
    // special values
    f2 = modf(-0.0, &f3);
    printf("modf(-0) makes %.2f + %.2f\n", f3, f2);
    f2 = modf(-INFINITY, &f3);
    printf("modf(-Inf) makes %.2f + %.2f\n", f3, f2);
}

가능한 출력:

Given the number 123.45 or 0x1.edccccccccccdp+6 in hex,
modf() makes 123.00 + 0.45
frexp() makes 0.964453 * 2^7
logb()/ilogb() make 1.92891 * 2^6
modf(-0) makes -0.00 + -0.00
modf(-Inf) makes -INF + -0.00

참고문헌

  • C23 표준 (ISO/IEC 9899:2024):
  • 7.12.6.12 The modf functions (p: TBD)
  • F.10.3.12 The modf functions (p: TBD)
  • C17 표준 (ISO/IEC 9899:2018):
  • 7.12.6.12 modf 함수 (p: TBD)
  • F.10.3.12 modf 함수 (p: TBD)
  • C11 표준 (ISO/IEC 9899:2011):
  • 7.12.6.12 The modf functions (p: 246-247)
  • F.10.3.12 The modf functions (p: 523)
  • C99 표준 (ISO/IEC 9899:1999):
  • 7.12.6.12 modf 함수 (p: 227)
  • F.9.3.12 modf 함수 (p: 460)
  • C89/C90 표준 (ISO/IEC 9899:1990):
  • 4.5.4.6 modf 함수

참고 항목

(C99) (C99) (C99)
주어진 값의 크기보다 크지 않은 가장 가까운 정수로 반올림
(함수)
C++ 문서 for modf