R
Roger Pack
1999.0.ceil
19992000
is this expected?
Is there any BigFloat that avoids this?
Thanks!
-=r
19992000
is this expected?
Is there any BigFloat that avoids this?
Thanks!
-=r
=> 1999=> # said:((BigDecimal("12.99")+BigDecimal("7.0"))*100).to_f.ceil
=> Float(BigDecimal("12.99")+7.0).class
2000
is this expected?
Float computer math is full of those effects - and so are the archives
of this list with articles similar to yours.
I hate those subtle rounding errors. Any thoughts if I were to suggest
Ruby by default use BigDecimal instead of float?
2. efficiency (Float is faster than BigDecimal)
3. standards (AFAIK the Float implementation is backed by an ISO
standard while I am not sure whether this is the case for BigDecimal).
True.
4. limited use: while your particular example will work as you expect,
BigDecimal is still not a real number (in math terms) but still a
rational number => rounding errors for other numbers will be introduced
when switching from Float to BigDecimal by default.
Roger said:Can't disagree with that.
True.
4. limited use: while your particular example will work as you expect,
BigDecimal is still not a real number (in math terms) but still a
rational number => rounding errors for other numbers will be introduced
when switching from Float to BigDecimal by default.
The only real benefit of it is that it won't bite "as often," for better
or worse.
This is a learning curve that could hit you at the edges [adding
extremely small and extremely large numbers, for example] and not during
normal day use
One "almost good" answer would be to have ruby output float to strings
by default with enough precision to be able to recreate the original.
Thoughts?
-=r
Roger Pack wrote:
In this case, I think it's IEEE.
I guess the take home is to avoid floats whenever you'll be doing any
rounding or truncating.
Robert said:I would not go as far. IMHO the take home should be awareness of the
fact that mathematical numbers do not blend well with any computerized
representation and that you always have to be careful when dealing with
them. Floats are perfectly ok for a large number of applications so
generally avoiding them seems a too harsh rule. Btw, if that were true
then we could throw them out of the language without loss but I am
pretty sure if you suggest that there would be vigorous objection.
Kind regards
robert
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.