ãã®è¨äºã¯C++ Advent Calendar 2024ã®3æ¥ç®ã®è¨äºã§ãã
æ¡å¼µæµ®åå°æ°ç¹æ°åã«ã¤ãã¦ã¯ãã¾ã詳ãã説æããªãã®ã§ä»ã®ãã¼ã¸ãåç §ãã¦ãã ããã
ãªããæ¸ãã¦ã¿ããèªæãªæãããã¦ãã¾ããããä¸å¿åå¿é²ã§ãã
æ¡å¼µæµ®åå°æ°ç¹æ°åã®å¤æã©ã³ã¯
C++23ã«ããã¦æ¡å¼µæµ®åå°æ°ç¹æ°åã¨ããæ°ããæµ®åå°æ°ç¹æ°åã®ãµãã¼ããï¼å¿ é ã§ã¯ãªããã®ã®ï¼è¿½å ããã¦ãã¾ããè¦æ ¼ã§ã¯ãæ¡å¼µæµ®åå°æ°ç¹æ°åãå«ãæµ®åå°æ°ç¹æ°åã®å¤æããªã¼ãã¼ãã¼ã解決ã«ã¤ãã¦ãæµ®åå°æ°ç¹æ°åã®å¤æã©ã³ã¯ã¨ãããã®ãç¨ãã¦èª¬æããã¦ãã¾ãã
ãã®å¤æã©ã³ã¯ã¯æµ®åå°æ°ç¹æ°åã«å¯¾å¿ããæµ®åå°æ°ç¹æ°è¡¨ç¾ã表ç¾å¯è½ãªå¤ã®éåã®å å«é¢ä¿ã«ãã£ã¦å®ç¾©ããã¦ããããã表ç¾å¯è½ãªå¤ã®éåã大ããåï¼ããå¹ ã®åºãæµ®åå°æ°ç¹æ°åï¼ã®å¤æã©ã³ã¯ãä¸ä½ã«æ¥ãããã«ãªã£ã¦ãã¾ããã¨ã¯ãããfloat16ã¨bfloat16ã®ããã«äºãã«å å«é¢ä¿ãæç«ããªãåãåå¨ããã®ã§ããã®é åºã¯åé åºã«ãªãã¾ãã
N4950 [conv.rank]/2ã«ããã¯è¦å®ããã¦ãããæ¦ããã®ãããªãã¨ãæ¸ããã¦ãã¾ãã
å ¨ã¦ã®æµ®åå°æ°ç¹æ°åã¯ã次ã®ããã«å®ç¾©ãããæµ®åå°æ°ç¹æ°å¤æã©ã³ã¯ãæã¤
1. æµ®åå°æ°ç¹æ°åT
ã®ã©ã³ã¯ã¯ããã®å¤ã®éåãT
ã®å¤ã®éåã®çé¨åéåã¨ãªãæµ®åå°æ°ç¹æ°åã®ã©ã³ã¯ããã大ãããªã
2.long double
ã®ã©ã³ã¯ã¯double
ããã大ãããdouble
ã®ã©ã³ã¯ã¯float
ããã大ãã
3. åãå¤ã®éåãæã¤2ã¤ã®æ¡å¼µæµ®åå°æ°ç¹æ°åã®ã©ã³ã¯ã¯åã
4. ï¼CV修飾ãç¡è¦ãã¦ï¼æ¨æºæµ®åå°æ°ç¹æ°åã®ãã¡ã®ã¡ããã©1ã¤ã¨åãå¤ã®éåãæã¤æ¡å¼µæµ®åå°æ°ç¹æ°åã®ã©ã³ã¯ã¯ããã®æ¨æºæµ®åå°æ°ç¹æ°åã¨åã
5. ï¼CV修飾ãç¡è¦ãã¦ï¼æ¨æºæµ®åå°æ°ç¹æ°åã®ãã¡ã®2ã¤ä»¥ä¸ã¨åãå¤ã®éåãæã¤æ¡å¼µæµ®åå°æ°ç¹æ°åã®ã©ã³ã¯ã¯ãdouble
ã¨åã
翻訳ãæ°ã«ãªãå ´åã¯åæãåç §ãã¦ãã ããã
3ã«è©²å½ããæ¡å¼µæµ®åå°æ°ç¹æ°åã®ãã¢ã¯C++23æç¹ã§ã¯åå¨ãã¦ããªãæ°ãããã®ã§ãããããã§æ°ã«ãªãã®ã¯ããã§ã¯ãªã5ã®è¦å®ã§ããdouble
ã¨float
ãIEEE754ã®å精度ã¨å精度ã®è¡¨ç¾ãæã¤ãã®ã¨ããã¨ã4ã«ãã£ã¦std::float64_t
ã¨std::float32_t
ã¯ããããdouble
ã¨float
ã¨åãå¤æã©ã³ã¯ã«ãªãã¾ããããã5ã¯ä¸ä½ä½ãè¨ã£ã¦ããã®ã§ããããï¼ãããã¯ãä½ãæ³å®ãã¦ããã®ãã»ã»ã»ï¼
long double
æ¨æºæµ®åå°æ°ç¹æ°åã«ã¯ããä¸ã¤long double
ã¨ãã奴ããã¾ããããã¯å°ãªãã¨ãdouble
ã¨åã精度ãæã¤ã¨ãããã¨ãããããæå®ããã¦ããªãèªç±äººãªã®ã§ããããã®å®ä½ãå®è£
ã«ãã£ã¦å®ã«ãã©ã¨ãã£è±ãã«ãªã£ã¦ãã¾ããããã¦ã¨ãã«ãlong double
ãdouble
ã¨åã表ç¾ãæã¤å ´åãæ®éã«ããã¾ãã
ä¾ãã°MSVCã®Windowsç°å¢ãããã§ãããä»ã«ãARMã®32ãããç°å¢ãªã©ããããªãããã§ããdouble
ãIEEE754ã®å精度表ç¾ã«ãªã£ã¦ããã¨ããã¨ããã®å ´åæ¡å¼µæµ®åå°æ°ç¹æ°åstd::float64_t
ã«å¯¾ãã¦åã表ç¾ãæã¤æ¨æºæµ®åå°æ°ç¹æ°åã2ã¤åå¨ãã¦ãããã¨ã«ãªãã¾ãã
å ç¨ã®å¤æã©ã³ã¯ã®è¦å®5ã¯ã¾ãã«ãã®ãããªå ´åã®äºãæ³å®ããæå®ãã¦ãã¾ãã
ããæ¡å¼µæµ®åå°æ°ç¹æ°ã¨ä»ã®æµ®åå°æ°ç¹æ°åéã®å¤æã«ããã¦ãå¤æã©ã³ã¯ã®ä½ãåããé«ãåã¸ã®å¤æã¯ãã¹ã¬ã¹å¤æã¨ãã¦æé»çã«è¡ããä¸æ¹ã§ãå¤æã©ã³ã¯ãä¸ãæ¹åã®å¤æã¯ç¸®å°å¤æã§ããæé»çã«ã¯è¡ãã¾ãããè¦å®2ã«ãã£ã¦long double > double
ã¨ãªãããããã®å ´åã«std::float64_t
ã¯ã©ã¡ããã¨åãå¤æã©ã³ã¯ã«ãªãå¿
è¦ããããããã¯double
ã¨åãã«ãªããã¨ãè¦å®4ã¯æå®ãã¦ãã¾ãã
ãã®å ´åã«ããlong double
ã¨åãå¤æã©ã³ã¯ã«ãªãã¨ããã¨ãlong double
ã¨std::float64_t
ã¯åãå¤æã©ã³ã¯ãªã®ã§ç¸äºã«æé»å¤æå¯è½ãªã®ã«å¯¾ãã¦ãstd::float64_t
ã¨double
ã§ã¯å¤æã©ã³ã¯ãç°ãªãããdouble
ããã®å¤æã¯æé»çã«è¡ãããã®ã®ãdouble
ã¸ã®å¤æã¯æ示çå¤æãå¿
è¦ã¨ãªãã¾ãã
// long doubleã¨std::float64_tãåãå¤æã©ã³ã¯ã ã£ãã¨ãã㨠const long double ld = 1.0; const double d = 1.0; const std::float64_t fp64 = 1.0f64; // long doubleã¨std::float64_tã¯ç¸äºå¤æå¯è½ long double ld2 = fp64; // â std::float64_t fp64_2 = ld; // â // doubleã¨std::float64_tã¯ä¸æ¹éè¡ std::float64_t fp64_3 = d; // â double d2 = fp64; // â
ãã®æåã¯ãããã便å©ãªãã®ã§ã¯ããã¾ãããã©ãèãã¦ãdouble
ã®ä½¿ç¨æ©ä¼ã®æ¹ãå¤ãã§ããããå¾ã£ã¦å®éã®ä»æ§ã§ã¯ãã®ãããªå ´åã®æ¡å¼µæµ®åå°æ°ç¹æ°åã®å¤æã©ã³ã¯ã¯double
ã¨åãã«ãªããå°ãªãã¨ãdouble
ã¨ã®ããåããã¹ã ã¼ãºã«ãã¦ãã¾ãã
// å®éã®C++23ã§ã¯ const long double ld = 1.0; const double d = 1.0; const std::float64_t fp64 = 1.0f64; // long doubleã¨std::float64_tã¯ä¸æ¹éè¡ long double ld2 = fp64; // â std::float64_t fp64_2 = ld; // â // doubleã¨std::float64_tã¯ç¸äºå¤æå¯è½ std::float64_t fp64_3 = d; // â double d2 = fp64; // â
éWindowsã®x86-64ç°å¢ã§ã¯long double
ã®è¡¨ç¾ã¯80ãããã®æ¡å¼µå精度ã«ãªã£ã¦ãããã¨ãå¤ãã§ããããã®åä½ã¯ãªãã·ã§ã³ï¼-mlong-double-64
ï¼ã§å¤æ´ãããã¨ãã§ããã®ã§ãã®æåãå®éã«ç¢ºããããã¨ãã§ãã¾ãã
ä½è«ã§ãããAVRãã¤ã³ã³ã®ç°å¢ã§GCC9ã¾ã§ã¯double
ãlong double
ãfloat
ã¨åã32ãããå¹
ã®è¡¨ç¾ã«ãªã£ã¦ããããã§ãgcc10以éããªãã·ã§ã³ã§å¤æ´å¯è½ã¨ã®ãã¨ã§ãããã®ãããªç°å¢ã§ã¯ãï¼ããå®è£
ãããã°ï¼std::float32_t
ã«å¯¾ãã¦åã表ç¾ãæã¤æ¨æºæµ®åå°æ°ç¹æ°åã3ã¤åå¨ãããã¨ã«ãªãã¾ãã»ã»ã»