oppiliappan / eva

a calculator REPL, similar to bc(1)
MIT License
823 stars 29 forks source link

Precision problem with floating points numbers #34

Closed snpefk closed 2 years ago

snpefk commented 4 years ago

Hi! Less words, more example. When I'm trying to add up 41.90 + 91.90 + 209 + 249 eva gave a 591.7999999999, although I expected 591.80.

oppiliappan commented 4 years ago

noted. eva uses rust's floating point arithmetic, we should definitely move over to fixed point arithmetic.

snpefk commented 4 years ago

I'm not a big expert in computer arithmetic, but my research (a.k.a browsing Wikipedia) suggest to look at "Arbitrary-precision arithmetic" since bc use similar approach.

rug seems like a nice choice. It's implement Integer, Rational and Float and configurable (you can turn off Complex numbers). The only drawback I found was the dependence on GNU libraries.

pickfire commented 3 years ago

I recently just noticed this issue, isn't num solving this? I think it's num_rational that provides arbitrary precision.

oppiliappan commented 3 years ago

@pickfire I am afraid I never got around to actually implementing it (I imported the num crate and completely forgot about it soon after)

pickfire commented 3 years ago

@NerdyPepper Heh, you could make this a hacktoberfest issue and hope someone solve it.

JensGrabner commented 3 years ago

In Excel, i have these 591.800000000000068. Relative failure of 1.15E-16. Welcome in the real world ;-)

TheTechRobo commented 2 years ago

Running cargo r on the master branch works fine for me:

image

pickfire commented 2 years ago

The display is fixed in master branch, but still it doesn't support numbers larger than f64, I guess this can be closed for now.