Chapter 27. Additional C99 Support for Mathematics

지환·2022년 3월 28일
0

마지막 챕터..
전문적인 내용이 꽤 있어서(지금 당장 복소수 다룰 일은 없을 것 같아서..) 그냥 일단 슬슬 적어두고 나중에 필요한 내용이면 나중에 책 자세히 보는걸로 해야지.


27.1 The <stdint.h> Header

Integer Types

bits 수가 정확히 명시된 integer type과 관련 macro를 제공한다.
Standard에선 type들의 정확한 크기를 명시하지 않기때문에 portability가 떨어질 수 있다.
하지만 이 header를 통해 그 점을 보완할 수 있다.

다섯가지 group의 types이 있고(자세한건 책),
각 types의 limits은 macro에서 정의하고 있다.

27.2 The <inttypes.h> Header

Format Conversion of Integer Types

<stdint.h>를 include하는 header이다.
(그래서 <inttypes.h>를 include하면 굳이 <stdint.h> include 할 필요 없음)

Q. 어차피 include할거면 `stdint.h` 헤더는 왜 필요하지?

A. C99에서 freestanding implementation은 <stdint.h>를 제공하도록 요구한다.
반대로 <inttypes.h>는 hosted implementation에서만 제공하도록 요구한다.
따라서 freestanding에서 사용하려면 해당 header가 필요하다.
hosted 이더라도 <inttypes.h>의 모든 기능이 필요로 하지 않은 경우도 있으니,
그때는 <stdint.h>만 include해서 사용할 수 있다.

<stdint.h>를 두가지 방법으로 확장하는데,
1. ...printf, ...scanf에서 사용할 수 있는 macro를 제공하고
2. greatest-width integers(<stdint.h>에서 제공하는 type의 group 중 하나)를 다룰 수 있는 functions을 제공한다.

27.3 Complex Numbers

복소수 다루는 방법..관련해서 설명하는데 아직 딱히 필요하진 않은거 같애서 pass

27.4 The <complex.h> Header

마찬가지로 일단 pass

27.5 The <tgmath.h> Header

Type_Generic Math
math 관련 header들에 있던 수학 함수들의 type generic parameterized macros를 제공한다.

27.6 The <fenv.h> Header

Floating-Point Environment

IEEE 754(IEC 60559) 가 floating point number 나타내는데 가장 널리 쓰인다.

이 헤더는 IEEE 754에서 정의한 floating-point status flagcontrol modes에 접근할 수 있게 해준다.
물론 다른 방식의 floating point 표현법도 지원은 하지만, 이 header가 만들어진 이유는 IEEE 754를 지원하기 위해서이다.

왜 프로그램에서도 이런 기능을 지원해야되는지?? 에 대한 대답은 이 책에서 대답할 내용이 아니라고한다..
"what every computer scientist should know about floating-point arithmetic - David Goldberg"의 문서를 한번 보라고 하네.. (44페이지짜리네... 나중에 함봐야지 ㅎㅎ)

Floating-Point Status Flags and Control Modes

Section 23.4에서 floating-point exceptions이랑 rounding direction에 대해 다룬적이 있다. 그건 이 헤더와 연관된다.
자세히 보기전에 관련 개념 짚고가자.

floating-point status flag
flaoting point execption이 raise되면 세팅되는 system 변수이다.
(앞서봤듯이) 다섯가지 종류의 exception이 있다.
각 exception은 대응하는 status flag가 있다.

status flag를 다루기위해 fexcept_t type을 정의하는데, 얘는 status flag의 collective value이다.
뭔말이냐면, 예를들어 fexcept_t는 간단한 integer type이라면, 각 single bit는 각 flag를 나타낼 수 있다.
(표준에 의해 integer로 하라고 요구된건 아님, structure일 수도 있고..)
(만약 structure면 더 자세한 정보를 저장할 수 있음, 뭐 어디서 exception이 발생했는지 등등)

floating-point control mode
floating-point arithmetic을 할때의 행동(?)을 변화시키기위해 세팅하는 system 변수이다.
IEEE 표준에선 rounding direction을 control하기 위한 "directed-rounding" mode를 명시한다.

4가지 rounding direction이 있다.
1) Round toward nearest
2) Round toward zero
3) Round toward positive infinity
4) Round toward negative infinity
Default는 (1)toward nearest

몇 implementation은 두가지 추가 control mode를 제공한다.
rounding precision을 정하는 control mode와
trap enablement를 정하는 control mode(exception이 raise됐을때 멈출지 말지를 결정)

floating-point environment라 함은, 특정 implementation에서 지원하는 FP status flag와 FP control mode를 아울러 이르는 말이다.
fenv_t type의 value는 전체 FP environment를 나타낸다.(fenv.h에 정의돼있음)

<fenv.h> Macros

자세한 list는 책 참고

책의 list 중 FE_ALL_EXCEPTFE_DFL_ENV만 필수임.
(table에 없어도 implementation이 추가로 정의할 수 있다. FE_로 시작하고 대문자 따라와야됨)

보니까, FP exception의 값을 macro로 bit가 겹치지 않게 정의하고 있음.
rounding direction 관련 macro도 마찬가지..

The FENV_ACCESS Pragma

#pragma STDC FENV_ACCESS on-off-switch
형식으로 이 헤더의 함수를 사용한다고 compiler에게 알려줘야된다. 제대로 최적화가 되지 않을 수 있기 때문이다.

switch가 OFF인데 status flag를 테스트하거나, non-default control mode를 사용하면 UB이다.

Functions..

FP exception
Rounding
Evnironment
관련 함수들 몇개있네
자세한건 책 참고

0개의 댓글