Skip to content

Instantly share code, notes, and snippets.

@paddor
Created October 25, 2011 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paddor/1314556 to your computer and use it in GitHub Desktop.
Save paddor/1314556 to your computer and use it in GitHub Desktop.
require 'bigdecimal'
require 'bigdecimal/math'
class Integer
def fact
return self * (self-1).fact if self > 1
return 1
end
end
Positions = 20
Precision = 1 * 10 ** -Positions
puts "precision positions: #{Positions}"
puts "\n##### Math::E ######"
puts "e = #{Math::E}"
##
# with simple Float
#
puts "\n##### calculated with simple Float ######"
e = 0
i = 0
while true
add = 1.0/i.fact
# puts "#{i}: add = #{add}"
e += add
break if add <= Precision
i += 1
end
puts "e = #{e.round(Positions)}"
##
# with BigDecimal
#
puts "\n##### calculated with BigDecimal ######"
e = BigDecimal.new('0')
i = 0
one = BigDecimal.new('1')
while true
add = one/i.fact
# puts "#{i}: add = #{add}"
e += add
break if add <= Precision
i += 1
end
puts "e = #{e.round(Positions).to_s('F')}"
##
# with BigMath::E()
#
puts "\n##### calculated with BigMath::E() ######"
e = BigMath::E(Positions)
puts "e = #{e.round(Positions).to_s('F')}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment