2012/11/4追è¨
ãã£ã¡è¦ã¦ããã£ãæ¹ãããããã
Javaプログラマーのためのjava.math.BigDecimalまとめ - きどたかのブログ
2012/11/4è¿½è¨ çµãã
ãã£ããã¨ç¾ åãã¦ã¿ãã
- java.math.BigDecimalã¯ãã¯ã©ã¹èª¬æã«ããéãä»»æ精度ã®ã¹ã±ã¼ã«ãªãæ´æ°ã¨ã32bitã®ã¹ã±ã¼ã«ã§æ§æããããã¤ã¾ãã¯ãBigDecimalã¯BigIntegerã¨intã§æ§æããã¦ããã
- precisionã¨ã¯ã精度ãã§ãããä»®æ°ã®æ¡æ°ã§ãããä»®æ°ã¯ä»»æ精度ã®ã¹ã±ã¼ã«ãªãæ´æ°ã®BigIntegerã§ããã®10é²è¡¨ç¾ã§ã®æ¡æ°ã§ããã
- precisionã¯ä¸çªå·¦ããåãã«ç¾ãã0以å¤ã®æ°åããæ°ããæ¡æ°ã§ããã0.03ã®ç²¾åº¦ã¯1ã§ãããã¹ã±ã¼ã«2ã§ããã
- BigDecimalã®ç²¾åº¦ã¯precisionã§è¡¨ç¾åºæ¥ãããdoubleçã®æµ®åå°æ°ç¹ã®ç²¾åº¦ã¯å ¨ä½ã§ä½¿ç¨ããbitæ°ã§è¡¨ç¾ããã
- scaleã¨ã¯å°æ°ç¹ä»¥ä¸ã®æ¡æ°ã§ãããããã¯åºæ°10ã®ææ°é¨åã§ããã(å¤2精度1ã¹ã±ã¼ã«1= 0.2 = 2*10^-1 = 2E1)
- scaleããã¤ãã¹æ¹åã«å¢ããã¨ã10åã¥ã¤å¤ã¯å¤§ãããªãã100ã¯ç²¾åº¦3ã¹ã±ã¼ã«0ã ã£ããã精度1ã¹ã±ã¼ã«-2ãªãã¦ãã¨ãããã
- scaleã¯exponentã§ããããBigDecimalã®è©±ãããå ´åã¯scaleã¨è¡¨ç¾ããæ¹ãè¯ãã
- 0ã¨0.0ã¯ç価(equals)ã§ã¯ãªãããåä¸ã®é åºä»ãã§ããã(compareTo)
- intã§è¡¨ããã¦ãã丸ãã¢ã¼ãã¯ãEnumã®RoundingModeã使ãæ°ããæ¹æ³ã¸ã·ãããã¹ãã ã
- æ¼ç®ä¸ã®ä¸¸ãä½ç½®ã®æå®ã¯precisionã¨scaleã®äºç¨®é¡ãããããscaleã®æå®ãåºæ¥ãã®ã¯é¤ç®ã®ã¿ã§ããã
- precisionã¨scaleã®éãã¯ãä»®æ°ã®å·¦ç«¯ããèããã®ãprecisionãå³ç«¯ããèãã¦ãã®ãscaleã§ããã
- scaleæä½ãããå ´åãä¸çªå³ç«¯ããèããã®ã§ã¯ãªããç¾å¨ã®å°æ°ç¹ããã®ç¸å¯¾ä½ç½®ã§èããã
- valueOf(double)ã誤ç¨ãã¦ã¯ãªããªããã³ã³ã¹ãã©ã¯ã¿ã使ã£ãæ¹ããvalueOf(double)ããã精度ãé«ããªã£ã¦ããã
- ã¹ã±ã¼ã«ã¯ãªã¼ãã¼ããã¼ã»ã¢ã³ãã¼ããã¼ãããã¨ããããintã®ç¯å²ãåºã¦ãã¾ãã±ã¼ã¹ãããã
- ã¼ãé¤ç®ããã¨ä¾å¤ã«ãªããæµ®åå°æ°ç¹ã§ã¯ãªãã®ã§Infiniteã¨ãã«ã¯ãªãã
- æ£ãè² ã®ç¡é大ãNaNã§ããdoubleãã³ã³ã¹ãã©ã¯ã¿ãvalueOfã«æ¸¡ãã¨ã¨ã©ã¼ã«ãªãã
- ãã¼ãã¦ã§ã¢åºæã®DFP(10é²æµ®åå°æ°ç¹)ã使ãå ´åãé常ã¯precisionã«ããè¨ç®ãããããããMathContextã®å®æ°ã«ãªã£ã¦ãããããªIEEEè¦æ ¼ãä¸è¬çã
- æ°å¦çãªlogãç¨ããè¨ç®ãåºæ°å¤æãç¨ãã¦ãprecisionã0.3(=log[10] 2)ã§å²ãã¨ã2é²æ°ã®å¿ è¦æ¡æ°(bitæ°)ãæ±ã¾ãããã®æ¡æ°ã32ã§å²ãã°ãBigIntegerãå é¨ã§ä¿æããªãã¨ãããªãint[]ã®è¦ç´ æ°ãæ±ã¾ãã
- toString()ãå®æã«ä½¿ããªãtoPlainString()ãèªã¿ãªããã
- movePointLeftãmovePointRightã¯ãåºæ¬ã¯ã¹ã±ã¼ã«æä½ã ãã精度ã®æä½ã«ãªãã±ã¼ã¹ãããã