Float fractionPart

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Float fractionPart

Matias Maretto
Hi: I don't know if this only happened on mi PC, but I evaluate:
123456.456 fractionPart
And I get: 0.456000000005588.
I'm using Dolphin 6.02

Mati.


Reply | Threaded
Open this post in threaded view
|

Re: Float fractionPart

Ian Bartholomew-21
Matias,

> Hi: I don't know if this only happened on mi PC, but I evaluate:
> 123456.456 fractionPart
> And I get: 0.456000000005588.
> I'm using Dolphin 6.02

You will see similar things occurring for Floating point values in all
languages.  Decimal floats cannot always be expressed accurately in a
binary system, there will always be a point below which the inaccuracies
will become evident.  This is normally hidden but some operations, as
above, can result in the inaccuracy becoming visible.

As long as you are aware of this it doesn't cause too may problems.
There most common problem is comparison.  If you evaluate

123456.456 fractionPart = 0.456

the answer is false.  Dolphin provides a method to get around this, one
that is aware of the limitations of Float representation.

123456.456 fractionPart equals: 0.456

will answer true.  You can also enforce a precision yourself, which may
be easier as #equals: is not polymorphic across all Numbers

(123456.456 fractionPart roundTo: 0.0001) = 0.456

answers true

--
Ian

Use the Reply-To address to contact me (limited validity).
Mail sent to the From address is ignored.


Reply | Threaded
Open this post in threaded view
|

Re: Float fractionPart

Matias Maretto
Ian: Thanks. I was using this to show a Price in a Word Document. It
was very shocking when I saw this "horrible" number. But, you are
right, once you have detected it, it is to solve.
Thanks a lot!.