BigDecimal to_s does not match to_f

Is the BigDecimal class broken? It seems like it should never happen: Note that a.to_f! = A.to_s.to_f

class A.class => BigDecimal

     

a.to_f => 18658.1072928

     

a.to_s => "10865.81072928"

     

b.class => BigDecimal

     

b.to_f => 10000.0

     

b.to_s => "10000.0"

     

(a - b) .to_f => 865.81072928

     

a.to_f - b.to_f => 8658.1072928

Any ideas on what could go wrong? We run ruby ​​1.8.7p72 on our servers and 1.8.7p173 on our local machines.

+3
source share
2 answers

docs :

to_f

Float, , BigDecimal. Float .

, Float ruby ​​ float : http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

Float. .

0

Source: https://habr.com/ru/post/1739531/


All Articles