roundé¢æ°ãã®7:å¶æ°ä¸¸ã
ããå æ¸ã¾ã¨ãããä»ã®è¨äºæ¸ããã£ã¦æãã§ãããã¾ã ã¾ã roundé¢æ°ã®ç¶ãã§ããå®éã®ã¨ããåãçµæ§é£½ãã¦ããããã¾ãã
ãã®ä¸é£ã®roundé¢æ°ã®ä»¶ã«ã¤ãã¦è¨åãã¦ããè¨äºãè¦ãã¨ãããªãæªããç¥èã¬ãã«ã®äººãããããã«æãã¾ãããããªè¨äºããã¡ãã¡æ¬ã£ã¦ãã¦ãããªãç¡ãããªã¨æã£ãã®ã§ãããä¸è¨ã®å 容ãä¿¡ãã¦ããããªäººãè¦ããã¾ãã*1ããªã®ã§æ¸ã ããã³ããå ¥ãã¾ãã
ãªããroundé¢æ°ã¯ãåæ¨äºå ¥ãããªãã®ã¯ã·ã¹ãã æ¥çã§ã¯æåãªã話ã
ãå¶æ°ä¸¸ãã㯠IEEE 754 ã§å®ãããã¦ãããJIS ã ISO ã«ãåãè¦å®ãããã¾ãã
å¤ãã® Round() é¢æ°ã®æåããã® IEEE 754 ã«åã£ããå¶æ°ä¸¸ããã§ãã
ãå¶æ°ä¸¸ããã¯åæ¨äºå ¥ã¨ã»ã¼åä¸ã§ããã次ã®ï¼ç¹ãéãã¾ãã
ã丸ãåä½ã®ä¸åº¦ã¾ããªãã§ãã©ã£ã¡ã¤ããã®å ´åã¯ãå¶æ°å´ãæ¡ç¨ããã
ãããã£ã¦ã1.25 ã 0.01 ã®ä½ã§ä¸¸ãã㨠1.2 ã«ãªãã1.35 㯠1.4 ã«ãªãã¾ãã
まっちゃだいふくの日記★とれんどふりーく★
PHPãã¯ããroundé¢æ°ãå¶æ°ä¸¸ãã«ãªã£ã¦ããè¨èªã¯åã®å¨å²ã«ã¯æ®ã©è¦å½ããã¾ãããå°ãªãã¨ãRubyãPythonã¯éãã¾ããã¤ãã§ã«è¨ãã°C(ã¨ãããC99)ãéãã¾ãããJavaã¨JavaScriptã®Math.roundãéãã¾ããã©ãããã°ã·ã¹ãã æ¥çä¸è¬è«ã®ããã«æãããã§ãããããããã¨ãCãJavaã¯æ¢ã«å°æ°æ´¾ãªãã§ãããï¼ç¾æ©è²ã£ã¦ãMSç°å¢ã§ã¯æ¯è¼çä¸è¬çãã¨ãã話é¡ã ã¨ãã¦ãç¡é¢ä¿ãªãã¡ç¥èã§ã並ã¹ã¦æ¸ãã®ã¯ãã¹ãªã¼ããèªã£ã¦ããã¨ããæãã¾ããã
ä»®ã«Un*xçéããããé ã人ã ã¨ãã¦ãããã¨ãã°[ruby-list:36917]以éã®è°è«*2ãè¦ãã°ãPythonãPHPãéããããªãã¦è©±é¡ãè¦ã¤ããã¾ãããä»ã®è¨èªã«ã¤ãã¦ãå°ãæ¤ç´¢ããã°ç°¡åã«è¦ã¤ããã¾ãããããã¯ãRubyãPythonã®roundé¢æ°ã«ã¤ãã¦ã®ã½ã¼ã¹ã³ã¼ãã確èªããã ãã§ãä¸ç®çç¶ãªè©±ã§ããããman 3 roundããæã¦ãç°å¢ãæã£ã¦ããªãããã§ãç¡ãã§ãããã
æ´ã«ããã®å¼ç¨é¨èªä½ã©ããã¨æãã¾ãããã®æç« ã¯å è¨äºãçãåæ§æãã¦ãããã®ãªãã§ããã©ãå ã®æç« ã®å¾åé¨ã«ååé¨ã®ä¾ãã¤ãªãã ããã§ã1.25ã¨1.35ã®è©±ãIEEE754æµ®åå°æ°ç¹æ°ã®ä¸¸ãã®è©±ã«è¦ããããªãæç« ã«çã¾ãå¤ãã£ã¦ãã¾ãã1.25ã¯ã¾ã ãã1.35ã2é²ã®æµ®åå°æ°ç¹æ°ã§ããã¿ãªè¡¨ããã¨ã¯ä¸å¯è½ã§ãããã©ãããæå³ã§ã丸ãåä½ã®ä¸åº¦çãä¸ãã«ãªãã¨è¨ãã®ã§ããããã1.35ã§ããã°ä¸æ£ç¢ºãªç解ã§ãçµæã¯åãã«ãªãã¾ããã©ããã¨ãã°doubleã®4.0925ã¨4.0935ãå°æ°ç¹ä»¥ä¸ç¬¬3ä½ã¾ã§ã«ä¸¸ããå ´åã«ã¯ã©ãã§ãããã*3ãå ã®æç« ãæ¸ãã人ã¯ä¸å®ä»¥ä¸ç解ãã¦ããã¨æãã¾ãããåæ§æãã人ã®ç¥èã¬ãã«ã«ã¤ãã¦ã¯çåã§ãããå ã®è¨äºã256åèªãã æ¹ãããããããªãã§ããããã
åã®æ¥ã ã®ç®æ¨ã®ä¸ã¤ã¯ãä¸ã®ä¸ã«ããµãã¦ãããç¥èããèªåã®è¡èã¨ãã¦ã®ãç¥æµãã¸ã¨å¤ãã¦ãããã¨ã§ã*4ãç¥æµãå¾ããã¨ã¯èª°ã«ã§ãã§ãããã¨ã§ãèªåã®æãåãããèªåã®é ãåãããã ãã®ãã¨ã§ããç¥æµã身ã«ã¤ãããã¨ãæ ã£ã¦ã³ããã ãã®è¨äºãä¹±çºãããããã¾ã¤ããå£åã³ãã¼ããã©å·»ããããªäººç©ã¯ãåã®ä¾¡å¤å¤æåºæºã§è¨ãã°è² ã®ä¾¡å¤ãçãã§ãã¾ã*5ã
åãããããè¨äºãæ¸ãã¨ç®èã ããã§èªåã§ãã©ããã¨æããã¨ãããã®ã§ãã§ããã ãæ§ãããã¨æã£ã¦ãã¾ããå¿ãã人ã端ããåªããããã³ããå ¥ãã¦ãã£ã¦ãããã°ä¸çãå¹³åã«ãªãã®ããããã¾ããããåã«ã¯ç¡çã§ãã
追è¨1: è足ããããã¾ããããåããæ®æ®µä½¿ã£ã¦ããCPUã¯IEEE754æºæ ã§ãããå é¨çã«å¶æ°ä¸¸ããè¡ã£ã¦ãã¾ããããã¯ã©ããããã¨ãã¨ããã¨ããã¨ãã°54bitç®ã1ã§ä»¥éã®bitãå ¨é¨0ã§ãããããªè¨ç®çµæãä»®æ°é¨52bit(hidden bitãå«ããã¨53bit)ã®å精度ã«ä¸¸ããããªã©ã¨ããå ´åã®è©±ã§ããCPUãIEEE754ã®ä¸¸ããå®è£ ãã¦ãã¦ããã©ã«ãã®ä¸¸ãã¢ã¼ããå¶æ°ä¸¸ãã ã£ãã¨ãã¦ã*6ãroundé¢æ°ã®æåã¯åè¨èªã§åå¥ã«å®è£ ããªãã¨å¶æ°ä¸¸ãã«ã¯ãªãã¾ããããã®ããããæ··åãã¦ãã人ãå¤å°å± ããã«æãã¾ããã®ã§ã念ã®ããã
追è¨2: Roundé¢æ°ãå¶æ°ä¸¸ããªè¨èªã¨ãããã¨ã§ãC#ã§Roundé¢æ°ã®æåã調ã¹ãããã°ã©ã ãé©å½ã«æ¸ãã¦ã¿ã¾ããããã£ããã£ãã¦ç½®ãã¦ããã¾ããã©ããªãã ãå°ãå¤ãªç®æãããã¾ããããçå±ã¯ããããã£ã¦ããããã§ãã*7ãåãå¾ã ã«é£½ãã¦ãã¦ããã®ã§ããã¿ã«ãããå¥ç¹ãªæ¹ãããããã°è§£èª¬ããé¡ããã¾ãã
追è¨3:
*1:ãã®è¨äºãåå ã§ã¯ãªãã¦ãä½äººããç¬ç«ã«åããã¨ãèããã ãããããã¾ããã
*2:å¶ç¶ã«ãMatzãããPHPã®roundé¢æ°ã®ã½ã¼ã¹ã³ã¼ãããã§ãã¯ãã¦ãã¾ãããã®é ã¯PHPãPythonãRubyã¨å ¨ãåãå®è£ ã ã£ãããã§ãã
*3:æ¬ææå¾ã追è¨2ãåç §ã®ãã¨
*4:ç¾½ç åæ²»ã決æåãããã®ãã¯ãªã§ããå½¼ãè¨ãå§ãããã¨ã§ãç¡ãã¨ã¯æãã¾ãã
*5:èªæã®æå³ãè¾¼ãã¦ãä¸è¨è¨äºã®äººãæªæããã£ãããã§ã¯ç¡ãã¨æãã¾ãããè¨äºãæ¸ãã®ã«ããå°ãæéã使ã£ããã©ããã¨æãã¾ã
*6:æ®éã¯ãã®ã¯ãã§ãã
*7:C#èªä½ã®ã½ã¼ã¹ã³ã¼ããè¦ãããã§ã¯ãªãã®ã§æ³åã§ããã©