The basic issue is one of representation -- your represent numbers in
base 10 (decimal); the Machine represents numbers in base 2 (binary).
When you  (or the Machine) translates between bases, there may be some
loss in precision -- a number that is finite, terminating fraction in
base19 (0.78, for example) may well be an infinitely repeating
fraction in base2, or conversely.  When you do mathematical
manipulations converting bases back and forth, you may have a few bits
left over after the decimal point.

That is why numerical algorithms don't ask "is a == b" but as "is (a -
b) < fuzz", where fuzz is defined as "not quit zero but close enough
for our purposes".  If you need "precision", then scale your
calculation so that you are working with whole numbers; for example:

    (0.70 - 0.68) == (70 - 69)/100.

Gives you the expected 0.02.


B
On Fri, Jul 17, 2009 at 4:11 AM, Thomas Bätzler<t.baetz...@bringe.com> wrote:
> Paul Johnson <p...@pjcj.net> wrote:
>> On Fri, Jul 17, 2009 at 12:26:58PM +0200, Thomas Bätzler wrote:
>> > Octavian Rasnita <orasn...@gmail.com> asked:
>> > > I have tried the following calculation with ActivePerl 5.10.0 build
>> > > 1004 under Windows XP Pro:
>> > >
>> > > print 0.79 - 0.798;
>> > >
>> > > And it gave the following result:
>> > > -0.00800000000000001
>> > >
>> > > which is obviously wrong.
>> >
>> > No, it isn't.
>>
>> Well ... yes, it is.
>
> Let's settle on "the result isn't what you'd expect if you had no prior 
> experience with machine numbers" ;-)
>
> Cheers,
> Thomas
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to