본문 바로가기

프로그래밍 언어/C++

floating-point conversion에 대해

반응형

conversion의 설계 개념

C++의 conversion를 정의할 때, 직교하는 3 요소, value category, cv-qualification, data representation를 설계 개념으로 사용한다. conversion를 설계할 때, 요소 중 하나만을 변형하고, 다른 요소를 변형하지 않는다. 어떤 이유로, 예를 들어 하위 호환성을 위해, 설계 개념을 위반할 때는 관련된 특수 조항을 스펙 문서에 별도로 명시한다.

floating-point conversion rank

  1. floating-point type T다른 floating-point type의 value set를 진부분 집합으로 갖는다면, T가 greater rank하다.
  2. long doubledouble보다, dobulefloat보다 greater rank하다.
  3. 같은 value set를 갖는 두 extended floating-point typeequal rank하다.
  4. cv-unqualified standard floating-point type T와 같은 value set를 갖는 extended floating-point type는 T와 equal rank하다.
  5. 하나 이상의 cv-unqualified standard floating-point type과 같은 value set를 갖는 extended floating-point typedoubleequal rank하다.
  6. 어느 한쪽의 value set이 다른 쪽읜 value set를 부분 집합으로 갖지 않는 floating-point type T1,T2unordered rank하다. 예를 들어, 비교하는 두 타입 중 하나가 larger range이고, 동시에 lower precision일 때 unordered rank하다.

floating-point conversion subrank

  1. equal rank를 갖는 서로 다른 floating-point typefloating-point conversion subrank로 정렬한다. equal rankfloating-point conversion subranktotal ordering하다.
  2. std::float16_t, std::float32_t, std::float64_t, std::float128_t type는 equal rank를 갖고 있는 standard floating-point type보다 greater subrank하다. 다른 경우에는 greater subrank를 implementation-defined한 방법으로 선택한다.

floating-point conversions

data representation 요소를 변형하기 위해 설계한 conversion이다. data representation를 변경하기 때문에, conversion rank로 분류된다. 또한 일반적으로 value category이나 cv-qualification를 변경하지 않는다.

floating-point conversionsprvalue category에서 prvalue category로 변환을 정의한다.

floating-point typeprvalue categorygreater or equal conversion rank를 갖는 다른 floating-point typeprvalue category로 변경할 수 있다. standard floating-point typeprvalue category또다른 standard floaging-point typeprvalue category로 변경할 수 있다.

source value를 목적 타입의 정확한 값으로 표현할 수 있다면, conversion 결과는 목적 타입의 정확한 값이다. source value가 목적 타입의 근접한 두 사이값이면, conversion 결과는 implementation-definition으로 선택한 두 값 중 하나다. 그 외 경우는 undefined behavior다. 따라서 변환 과정에서 정밀도를 손실한다.

floating-point promotions conversionfloating-point conversions에서 제외한다.

반응형