Some odd behaviors of BigDecimal

e$B1sF#$G$9!#e(B

BigDecimal
e$B$N%F%9%H$r=q$$$F$$$F!"$$$/$D$+5$$K$J$k5sF0$,$"$j$^$7$?!#e(B

  1. Infinity e$B$Ne(B n e$B>h$,>o$Ke(B NaN
    e$B$K$J$k$N$O0U?^E*$G$7$g$&$+!#e(B

ruby19 -rbigdecimal -e ‘x = BigDecimal(“Infinity”); p [x1, x0,
x**-1]’
[#BigDecimal:8261238,‘NaN’,4(8), #BigDecimal:8261224,‘NaN’,4(8),
#BigDecimal:8261210,‘NaN’,4(8)]

inf ** 0 e$B$,e(B NaN e$B$K$J$k$N$ODj5A<[email protected]$H;W$$$^$9$,!"e(B** 1
e$B$He(B ** -1 e$B$Oe(B
e$B$=$l$>$le(B inf e$B$He(B 0 e$B$r4|BT$7$F$$$^$7$?!#e(B
Float [email protected]$H0J2<$N$h$&$K$J$j$^$9e(B (e$B$&$A$N4D6-$N>l9ge(B) e$B!#e(B

$ ruby19 -e ‘x = 1.0/0.0; p [x1, x0, x**-1]’
[Infinity, 1.0, 0.0]

  1. 1E1000…(0 e$B$,$$$C$Q$$e(B)…000 e$B$re(B BigDecimal
    e$B$KM?$($k$He(B 0 e$B$,JV$k$N$O0U?^E*e(B
    e$B$G$7$g$&$+!#e(B

$ ruby19 -rbigdecimal -e ‘p BigDecimal(“1E1” + “0” * 10000)’
#BigDecimal:826133c,‘0.0’,4(8)

Float [email protected]$He(B Infinity e$B$K$J$j$^$9!#e(B

$ ruby19 -e ‘p (“1E1” + “0” * 10000).to_f’
Infinity

  1. limit(1) e$B$Ge(B 3 ** 4 e$B$r7W;;$9$k$He(B 90
    e$B$K$J$j$^$9$,0U?^E*$G$7$g$&$+!#e(B

$ ruby19 -rbigdecimal -e ‘BigDecimal.limit(1); p BigDecimal(“3”) ** 4’
#BigDecimal:8261300,‘0.9E2’,4(12)

e$B$A$c$s$HDI$C$F$J$$$G$9$,!“e(B3 * 3 * 3 * 3 = 10 * 3 * 3 = 30 * 3 = 90
e$B$He(B
e$B$J$C$F$$$k$N$+$J$”$H;W$$$^$9!#;[email protected][email protected]$o$l$l$PG<[email protected]$7$^$9!#e(B

e$B1sF#$G$9!#e(B

2008/09/04 23:46 Yusuke ENDOH [email protected]:

e$B$=$l$>$le(B inf e$B$He(B 0 e$B$r4|BT$7$F$$$^$7$?!#e(B
#BigDecimal:826133c,‘0.0’,4(8)
#BigDecimal:8261300,‘0.9E2’,4(12)

e$B$A$c$s$HDI$C$F$J$$$G$9$,!“e(B3 * 3 * 3 * 3 = 10 * 3 * 3 = 30 * 3 = 90 e$B$He(B
e$B$J$C$F$$$k$N$+$J$”$H;W$$$^$9!#;[email protected][email protected]$o$l$l$PG<[email protected]$7$^$9!#e(B

e$BJV;v$,$J$+$C$?$N$G!"$H$j$"$($:$9$Y$F0U?^E*$H8+$J$7$?%F%9%H$re(B
e$B%3%_%C%H$7$^$7$?!#e(B(r19401)

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:36358] Re: some odd behaviors of BigDecimal”
on Thu, 18 Sep 2008 02:17:23 +0900, “Yusuke ENDOH” [email protected]
writes:

|2008/09/04 23:46 Yusuke ENDOH [email protected]:
|> BigDecimal e$B$N%F%9%H$r=q$$$F$$$F!"$$$/$D$+5$$K$J$k5sF0$,$"$j$^$7$?!#e(B
|>
|>
|> 1) Infinity e$B$Ne(B n e$B>h$,>o$Ke(B NaN e$B$K$J$k$N$O0U?^E*$G$7$g$&$+!#e(B
|>
|> ruby19 -rbigdecimal -e ‘x = BigDecimal(“Infinity”); p [x1, x0, x**-1]’
|> [#BigDecimal:8261238,‘NaN’,4(8), #BigDecimal:8261224,‘NaN’,4(8),
|> #BigDecimal:8261210,‘NaN’,4(8)]
|>
|> inf ** 0 e$B$,e(B NaN e$B$K$J$k$N$ODj5A<[email protected]$H;W$$$^$9$,!“e(B** 1 e$B$He(B ** -1 e$B$Oe(B
|> e$B$=$l$>$le(B inf e$B$He(B 0 e$B$r4|BT$7$F$$$^$7$?!#e(B
|> Float [email protected]$H0J2<$N$h$&$K$J$j$^$9e(B (e$B$&$A$N4D6-$N>l9ge(B) e$B!#e(B
|>
|> $ ruby19 -e ‘x = 1.0/0.0; p [x1, x0, x**-1]’
|> [Infinity, 1.0, 0.0]
|>
|>
|> 2) 1E1000…(0 e$B$,$$$C$Q$$e(B)…000 e$B$re(B BigDecimal e$B$KM?$($k$He(B 0 e$B$,JV$k$N$O0U?^Ee(B
|> e$B$G$7$g$&$+!#e(B
|>
|> $ ruby19 -rbigdecimal -e ‘p BigDecimal(“1E1” + “0” * 10000)’
|> #BigDecimal:826133c,‘0.0’,4(8)
|>
|> Float [email protected]$He(B Infinity e$B$K$J$j$^$9!#e(B
|>
|> $ ruby19 -e ‘p (“1E1” + “0” * 10000).to_f’
|> Infinity
|>
|>
|> 3) limit(1) e$B$Ge(B 3 ** 4 e$B$r7W;;$9$k$He(B 90 e$B$K$J$j$^$9$,0U?^E
$G$7$g$&$+!#e(B
|>
|> $ ruby19 -rbigdecimal -e ‘BigDecimal.limit(1); p BigDecimal(“3”) ** 4’
|> #BigDecimal:8261300,‘0.9E2’,4(12)
|>
|> e$B$A$c$s$HDI$C$F$J$$$G$9$,!“e(B3 * 3 * 3 * 3 = 10 * 3 * 3 = 30 * 3 = 90 e$B$He(B
|> e$B$J$C$F$$$k$N$+$J$”$H;W$$$^$9!#;[email protected][email protected]$o$l$l$PG<[email protected]$7$^$9!#e(B
|
|e$BJV;v$,$J$+$C$?$N$G!”$H$j$"$($:$9$Y$F0U?^E*$H8+$J$7$?%F%9%H$re(B
|e$B%3%_%C%H$7$^$7$?!#e(B(r19401)

e$B$9$_$^$;$s!#:G6a!"e(BBigDecimale$B$,J|CV5$L#$G$9$M!#e(B

e$B8D?ME*$J0U8+$r=R$Y$k$H!"e(B3)e$B$K$D$$$F$O$I$&[email protected]$+J,$+$j$^e(B
e$B$;$s$,!"e(B1)e$B$He(B2)e$B$K$D$$$F$Oe(BFloate$B$K9g$o$;$?J}$,$h$$$H;W$$$^$9!#e(B

e$B1sF#$G$9!#e(B

2008/09/18 7:39 Yukihiro M. [email protected]:

|> $ ruby19 -e ‘x = 1.0/0.0; p [x1, x0, x**-1]’
|>
|> e$B$J$C$F$$$k$N$+$J$"$H;W$$$^$9!#;[email protected][email protected]$o$l$l$PG<[email protected]$7$^$9!#e(B
|
|e$BJV;v$,$J$+$C$?$N$G!"$H$j$"$($:$9$Y$F0U?^E*$H8+$J$7$?%F%9%H$re(B
|e$B%3%_%C%H$7$^$7$?!#e(B(r19401)

e$B$9$_$^$;$s!#:G6a!"e(BBigDecimale$B$,J|CV5$L#$G$9$M!#e(B

e$B8D?ME*$J0U8+$r=R$Y$k$H!"e(B3)e$B$K$D$$$F$O$I$&[email protected]$+J,$+$j$^e(B
e$B$;$s$,!"e(B1)e$B$He(B2)e$B$K$D$$$F$Oe(BFloate$B$K9g$o$;$?J}$,$h$$$H;W$$$^$9!#e(B

e$B;d$b$=$&;W$$$^$9!#$=$N$h$&$K$7$F%3%_%C%H$7$^$7$?!#e(B

$ ./ruby -rbigdecimal -e ‘(-2…2).each {|n| p (BigDecimal("-Infinity")
** n).to_f }’
0.0
-0.0
1.0
-Infinity
Infinity

$ ./ruby -rbigdecimal -e ‘%w(1E1 -1E1 1E-1 -1E-1).each {|s| p
BigDecimal(s + “0” * 10000).to_f }’
Infinity
-Infinity
0.0
-0.0