If I want to increase my significant digits beyond 15 in a result of a
math expression involving floats, how do I do it? So, for example, I
would like more than 15 sig digits in the result of:
If I want to increase my significant digits beyond 15 in a result of a
math expression involving floats, how do I do it? So, for example, I
would like more than 15 sig digits in the result of:
irb(main):001:0> 4.005 / 7
=> 0.572142857142857
irb is calling inspect on the value of the expression. It’s the
inspect method the one that truncates to 15 digits when it creates the
string. You can create your own strings with greater precision like
this:
irb(main):001:0> result = 4.005 / 7
=> 0.572142857142857
irb(main):002:0> “%.20f” % result
=> “0.57214285714285717521”
irb(main):003:0> “%.30f” % result
=> “0.572142857142857175212213860505”
Thank you! I forgot that IRB is returning a truncated string to the
screen.
That 15 is hard-coded in the definition of Float#to_s.
Yes, and exposing more digits is not accurate:
puts “%.70f”%(4.005 / 7)
#=>0.5721428571428571752122138605045620352029800415039062500000000000000000
require ‘bigdecimal’
a = BigDecimal.new(“4.005”)
b = a/7
puts b
#=>0.572142857142857142857143E0
p b.precs
#=>[24, 32]
#first number is the number of significant digits in b; the second
number is the maximum precision my system can handle (not sure about
this). The float is bogus after the 16th digit.
Xavier N. wrote:
That 15 is hard-coded in the definition of Float#to_s.
Yes, and exposing more digits is not accurate:
The float is bogus after the 16th digit.
Working with big numbers here, it seems that precision is only
guaranteed to 15 digits on either side of the decimal. When a whole
number >15 digits is divided the decimal is approximated, and anything
17 digits prints a float of n.0 no matter how it’s “%n.nf” formatted.
Is the hard-coded 15 just a “safe” limit? Would I be able to recompile
ruby with a higher definition? If possible, how high could I go?
17 digits prints a float of n.0 no matter how it’s “%n.nf” formatted. class Float - RDoc Documentation
“Float objects represent real numbers using the native architecture‘s
double-precision floating point representation.”
#first number is the number of significant digits in b; the second
number is the maximum precision my system can handle (not sure about
this). The float is bogus after the 16th digit.
It’s an implementation of arbitrary precision floating point numbers
(decimal and binary). If you don’t need it to be fast it has some
advantages over BigDecimal.
It’s an implementation of arbitrary precision floating point numbers
(decimal and binary). If you don’t need it to be fast it has some
advantages over BigDecimal.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.