Pathological floating point problems: Difference between revisions

No edit summary
Line 1,253:
 
===Task 2: Chaotic Bank Society===
A) Unlike Ruby, had to manually create "E" to sufficient precision.
<lang Ruby>require "big"
 
Line 1,275:
{{Out}}
<pre>Bank balance after 25 years = 0.03993872967323021</pre>
 
B) Or from Factor, use large rational approximation for "E = 106246577894593683 / 39085931702241241".
<lang Ruby>require "big"
e = 106246577894593683.to_big_d.div(39085931702241241.to_big_d, 132)
 
balance = e - 1
1.upto(25) { |y| balance = (balance * y) - 1 }
puts "Bank balance after 25 years = #{balance.to_f}"</lang>
{{Out}}
<pre>Bank balance after 25 years = 0.03993873004901714</pre>
 
===Task 3: Rump's example===
Anonymous user