Bluehost.com Web Hosting $6.95

RE: [PHP] Math Weirdness

This is a discussion on RE: [PHP] Math Weirdness within the PHP General forums, part of the PHP Programming Forums category; On 14 July 2008 20:47, tedd advised: > Round-off errors normally don't enter into things unless your ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-15-2008
Ford, Mike
 
Posts: n/a
Default RE: [PHP] Math Weirdness

On 14 July 2008 20:47, tedd advised:

> Round-off errors normally don't enter into things unless your doing
> multiplication and division operations. At that point, what you get
> back from the operation is an approximation and not the actual
> number.


Bull! Nearly all computer floating point numbers are approximations
because of being held in binary rather than decimal. Any number with a
(decimal) fractional part that doesn't end with the digit 5 is
necessarily an approximation, and that's only half the story. So as soon
as you involve numbers like 0.1 or 0.2, you've already got minor
inaccuracies which will propagate through any kind of arithmetic -- it's
just worse with multiplication or division because these tend to result
in inaccuracy in more significant digits!

Bottom line: a floating point value should *always* be treated with an
appropriate degree of suspicion.

Cheers!

Mike

--
Mike Ford, Electronic Information Developer,
C507, Leeds Metropolitan University, Civic Quarter Campus,
Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 812 4730


To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm
Reply With Quote
  #2 (permalink)  
Old 07-15-2008
Robin Vickery
 
Posts: n/a
Default Re: [PHP] Math Weirdness

2008/7/15 tedd <tedd.sperling@gmail.com>:
>
> I said:
>
> "Round-off errors normally don't enter into things unless your doing
> multiplication and division operations."
>
> And that is not "Bull" -- it's true. You can add and subtract all the
> floating point numbers (the one's we are talking about here) you want
> without any rounding errors whatsoever.


$ php -r 'echo 0.7 - 0.2 == 0.5 ? "true\n" : "false\n";'
false

The operation you do isn't important. You just can't represent certain
numbers exactly in binary, just as you can't represent 1/3 exactly in
decimal. They have to be rounded internally.

-robin
Reply With Quote
  #3 (permalink)  
Old 07-15-2008
Jochem Maas
 
Posts: n/a
Default Re: [PHP] Math Weirdness

Robin Vickery schreef:
> 2008/7/15 tedd <tedd.sperling@gmail.com>:
>> I said:
>>
>> "Round-off errors normally don't enter into things unless your doing
>> multiplication and division operations."
>>
>> And that is not "Bull" -- it's true. You can add and subtract all the
>> floating point numbers (the one's we are talking about here) you want
>> without any rounding errors whatsoever.

>
> $ php -r 'echo 0.7 - 0.2 == 0.5 ? "true\n" : "false\n";'
> false
>
> The operation you do isn't important. You just can't represent certain
> numbers exactly in binary, just as you can't represent 1/3 exactly in
> decimal. They have to be rounded internally.


which makes php casting so much fun:

$ php -r 'echo (string)(0.7 - 0.2) == 0.5 ? "true\n" : "false\n";'
true



>
> -robin
>


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:09 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0