09:41:50 ~$ ruby19 x.rb
Float 0.00000000010000000827
BigDecimal 0.00000000010000000000
Float 0.00000000000000000827
BigDecimal 0.00000000000000000000
false
true
09:41:52 ~$ cat x.rb
Good examples.
I should explain better my original thought perhaps--I don't actually
want to convert "rounded" data but yes make it "cleaner" and "simpler"
to make BigDecimals.
I think I can achieve this by allowing for BigDecimal(float) but,
similar to how String#to_s works currently in trunk:
"only accept default values of floats"
i.e.
BigDecimal(0.9)
=> #<BigDecimal:b7db9e14,'0.9E0',4(8)>
BigDecimal((2.0-1.1).to_s) # 2.0-1.1 != 0.9
# BOOM not allowed, since that's a float that's been rounded away from
its pure form (0.9's default form)
You can ascertain this a la
("%g" % (2.0-1.1)).to_f != (2.0-1.1) # blow up in this case--those are
rounded floats we don't want to convert them.
Somewhat sane?
-=r
···
--
Posted via http://www.ruby-forum.com/\.
Doing a quick bit of testing suggested that using "%f" % float
might be better than using float.to_s because in some cases
the former preserves information that is actually in the float
which the latter uses. In fact, I was going to suggest you propose
to use "%f" % self in Float#to_d instead of self.to_s.
But after some more thought and a bit of testing, I found that
for me in IRB "%f" % float only gives results to 6 decimal places
but float.to_s seems to give results to 14 or 15 significant figures
so overall using float.to_s for the conversion seems better.
The good news is that with current ruby trunk I believe that Float#to_s
is as accurate as doing a ("%f" % self) [it no longer loses accuracy].
If we want a BigDecimal method (and in view of BigDecimal( float.to_s )
I don't think we do - but BigDecimal( float.to_s ) should perhaps
be mentioned prominently in BigDecimal documentation)
how about something like (not tested):
def BigDecimal.from_f( f )
if f.kind_of?( Float ) then BigDecimal( f.to_s )
else raise "BigDecimal.from_f: argument must be a Float"
end
end
That would be nice, though the way you suggested earlier (require
'bigdecimal/util'; 3.3.to_d) works for me
···
-=
--
Posted via http://www.ruby-forum.com/\.
Hello,
Ok. This next isn't meant as an attacking question but...do you use { in
normal strings often? Granted probably more than #, but...?
Yes, quite extensively when generating C code. Would hate to have to use
\{...} instead of {...} for C blocks.
So do I when generating LaTeX code.
Cheers,
···
--
JJ Fleck