roundé¢æ°ãã®5:ããããåæãã¦ããã§ããï¼
ãã¦ãã¾ãã¾ããPHPの奇妙なround関数ãã®ç¶ãã§ããä»åã¯ã¿ã¤ãã«ãããããªãè² ãããã§ããã©ãæ°ã«ããããã¾ãããã
Matzさんの記事ã«åè«ããã«ã¯ããªãçè«æ¦è£ ãå¿ è¦ã ããã¨ããèãããããã2åã®è¨äºã¯åã¨ãã¦ãé å¼µã£ã¦æ¸ãã¦ãã¾ããããã ãç¸å½æ°ã®æ¹ãè°è«ã«ã¤ãã¦ãã¦ããªãã®ã§ã¯ãªãããã¨ãããã¨ã«æ°ã¥ãã¾ãã*1ããã®ãã¼ã¸ã¯åãé¢ç½ãã¨æã£ããã¨ãä»äººã«ä¼ããã®ãç®çã®ã¯ããªã®ã«ããã¾ãä¼ãããªãããã§ã¯æå³ããªããªã£ã¦ãã¾ãã¾ããã¾ããã§ããã°é¢ç½ããä¼ãããã¨æã£ã¦ããã®ã§ãããåã«ä½è£ãç¡ãå 容ãæ±ã£ã¦ããã¨ä¼ãããã¨ããé£ããã£ãããã¾ã*2ã
ã¾ããããããçè«æ¦è£ ãå¿ è¦ãªè©±ã«ãªã£ã¦ãã£ãï¼ã¨ããã¨ããããªãã¨ã¯ãªããã§ããããåã®æ³åã§ã¯Matzããã¯æè¦çãªãã¨ãè¨ã£ã¦ããã ãã§ãå ·ä½çã«ããããã¢ããªã±ã¼ã·ã§ã³ã§å®³ãããã¨ãã話ã§ã¯ãªãããã«æãã¾ããä¸æ¹ã§ãã©ããªã¦ã¼ã¶ã¼ãæ¥ã¦ãæå¥ãè¨ãããªããããªå®ç§ãªè¨èªãä½ããã¨ããã®ã¯ã¨ã¦ãç解ã§ããæè¦ã§ããèªåãªã絶対ããããå®è£ ã«ããªããã¨ããæè¦è¡¨æã«å¯¾ãã¦ã¯å ¨ãæå¥ãããã¾ãããç´ æ´ããããã¨ã ã¨æãã¾ãã
ãã ãä¸ç¹ã ãè¨ã£ã¦ããã¹ããã¨ãããã¨ãæãã¾ãã
ãã¼ã¨ãé常ãªã絶対ã«ããå¾ãªãè¨è¨ãªãã ãã PHPã¦ã¼ã¶ã¯ããããè¨è¨ã»ã³ã¹ã®è¨èªãä¿¡é ¼ã§ããã®ã*2ã
ãªãã ãã¨ã¦ãæ²ããæ°æã¡ã«ãªã£ãã
Matzにっき(2007-05-28)
ãªããªãçç³»ããããã¬å 容ã§ããããè¦ããã«ããããªå®è£ ããããããPHPã«ã¯ä»ã«ãåé¡ãé ãã¦ãããããããªããã¨è§£éãã¦è¯ãã®ã§ããããããªãã»ã©ãããããããã¾ãããããããããªãããããã¾ãããMatzããã¯ãããªçç³»ã£ã½ããªãçºè¨ã§é£ããããããªã¦ã¼ã¶ã¼ã欲ãããã§ãããããéã«å¤±æãã人ãå± ããã§ããã©ã
ç ½ãããã®ãç®çã§ã¯ãªãã§ãããããã¾ãããããåºããã®ã¯ããã¦ããã¾ããåãè¨ãããã®ã¯ä¸ç¹ãé£ããç ½ãã¯ä¸åãªè¶£å³ãããªãã§ãããã£ã¦ãã¨ã§ããRubyã«ã¯ããã¨ãããããããããããã§ãããã宣ä¼ããã®ããã大人ã£ã¦ãã®ãããªãã§ããããã
ãã¦ãåãPHPã®å¯¾å¿ã¯æ ¼å¥½æªãã¨ã¯æãã¾ããããã¯å¦å®ãã¾ãããã©ãæ ¼å¥½æªããã¨ãåé¡ãªããã§ã¯ãªãã¨æãã¾ããåããã®å¯¾å¿ã«ã¤ãã¦åé¡ã ã¨æã£ãç¹ã¯3ç¹ã§ãã
- ããããä¸éå端ãªæ°ã§å®è£ ããçç±ãã©ããããèªã¿åããªã
- ããããå®è£ ã«ãããã¨ãããã¥ã¢ã«ã«æ¸ãã¦ããªã
- ãããªï¼ããæå³ã§è¦ªåãªï¼å®è£ ã«ãããåå¿è ãæµ®åå°æ°ã®ç½ ã«ããããªããªã£ã¦ãã¾ãï¼ããã¯æè²çã«ãããããªãï¼*3
ããããæåã®é¢æ°ãªãã ããã¨ãããã¨ãããã¥ã¢ã«ã«æ¸ãã¦ããã°åé¡ãç¡ãã¨åã¯æã£ã¦ãã¾ãããä½ãå
·ä½çãªåé¡ç¹ãææã§ããæ¹ãããã£ããããã§ããããã
PHPãå®ç¾©ããï¼ãã¨ãããï¼ä¾ã®ã0.50000000001ãã¨ããæ°ãè¦ç´ãã¦ã¿ã¾ããããroundé¢æ°ã§ãã®ç¨åº¦ã®èª¤å·®ãå ¥ã£ãã¨ãã¦ããæå¹æ°åã¨ãã¦èããã¨10é²æç®ã§11æ¡ç®ããã以éã®æ¡ã«1ã足ããã¨ã«ãªãã¾ã*4ãã¤ã¾ããæå¹æ¡æ°ããã以ä¸ã®ãã¼ã¿ãæ±ã£ã¦ããç¯å²ã«ããã¦ã¯ããã®å®è£ ã®å½±é¿ãåãããã¨ã¯ããã¾ãããä¾ãã°32ãããæ´æ°ï¼æ大ã§10é²10æ¡ï¼å士ã®é¤ç®ç¨åº¦ã§ããã°ãã©ããã£ã¦ããã®å®è£ ã®å½±é¿ã¯åãã¾ãããåããªãã¨ããã¨ä¸æ£ç¢ºããããã¾ããããããããæçæ°ãæµ®åå°æ°ã¨ãã¦è¡¨ããéã«0.5ãã0.00000000001以å ã®è·é¢ã«ãã£ãã¨ããã°ãããã¯ç¢ºå®ã«0.5ãæå³ãã¾ãã*5
ã¾ããIEEE64ãããæµ®åå°æ°ç¹æ°ã®æå¹æ°åã¯10é²ã§è¨ãã°16æ¡ç¨åº¦ã§ãã10é²16æ¡ããå ã¯ã¢ãã¦ãã§ãããã¦ã12æ¡ãããã§ããå æ¸ã ã¨å°ãã¢ããªã±ã¼ã·ã§ã³ã£ã¦ä½ã§ãããï¼ããã¯ããããã¦10é²16æ¡ã§ãä¸ååãªã¢ããªã±ã¼ã·ã§ã³ã ã£ãããã¾ãããï¼ããããã§ããã°ãåã2回目の記事ã§ç´¹ä»ãããGMP関数ãããBCMath任意精度数学関数ããªã©ã使ãã¹ãã§ããããã¯ä»ã®è¨èªã§ãåæ§ã ããã¨æãã¾ãã
ãã®ä¸é£ã®è©±é¡ã¯åºæ¬çã«ã¯å°ãç¬ã£ã¦çµãããããªå°ãã¿ã®ã¤ããã§ãã*6ãããã«Matzãããããã¢ããã¨ããææ³ãè¨ã£ãã ãã§æ³¨ç®ããã¦ãã¾ã£ãããã§ããã©ããªãã§ãããªã«æ³¨ç®ããã¦ããã®ãæ¬å½ã«ä¸æè°ã§ãã
å®ã¯ãæåã®è¨äºä¸ã«ã¾ã è¨æ£ãã¦ããªãåã1ç¹æ®ã£ã¦ãã¾ããããããã®ç¥ããå éããã¦ãã¾ã£ãçç±ã®ä¸ã¤ããããã¾ãããããã¯æ¬¡åã®è©±é¡ã¨ãããã¨ã§ã
ã¾ããé å¼µã£ã¦PHPãæè·ãã¦ã¿ããã®ã®ãããããã¹ããªå®è£ ãªã®ãã¨ããã¨ãããéãæ°ããã¾ããããã«ã¤ãã¦ã®è°è«ãã¾ã æ¸ãã¦ããå¿ è¦ãããããã§ãã
ããããä»ã®å°ãã¿ãæ¸ãã¦æ¯æãããããã§ããã©ããã°ããã¯ãã®ãã¿ã«æ¯ãåããããã§ããããæè¦ã»åè«ãªã©ãå¾ ã¡ãã¦ããã¾ãã
*1:ã³ã¡ã³ãæ¬ããã®ä»ã§å©ãã¦é ããããã¦ãæ¸ãã¦ããåã«ã¨ã£ã¦ãè伸ã³ããå 容ã§ãã
*2:åèªèº«ã®åå¼·ã¨ããæå³ã§ã¯å¤§å¤é¢ç½ãã£ãã®ã¯ç¢ºãã§ããã©
*3:ããã¯ä»ã®å ´æã§ãããã§ããããã¾ããããæåã®è¨äºãæ¸ããæç¹ã§ã®åã®åéãã§ã
*4:0.5ä»è¿ã®ã¨ãã®ã¿11æ¡ã1.5ããå ã¯12æ¡ç®ããå ã¨ãããã¨ã«ãªãã¾ããããããããã®ããã«10é²ã§èª¬æãã¦ããã®ã§ã¢ãã¦ãã§ãã
*5:ããããããã®ããã«æå¹æ°åã®è©±ãå°å ¥ãã¦ã¿ãã®ã§ãããè«æ³ã¨ãã¦ã¯å¤±æã ã£ãããããã¾ããããã®è©±é¡ã¯æ¬¡å以éã«åæ§æããå¿ è¦ãããããã§ããè«æ³ã«åé¡ãããã¨ãã¦ãããã§çµè«ã¨ãã¦ãã話é¡ã¯çå®ã§ãi/jï¼iãjãintï¼ã0.5以å¤ã§0.5ãã0.00000000001以å ã®è·é¢ã«åå¨ãããã¨ã¯ãããã¾ããã
*6:ãã2åã¯å¤ã«çé¢ç®ã¶ã£ã¦ãã¾ãããã©