You are here: Re: adding numbers in php « All PHP « IT news, forums, messages
Re: adding numbers in php

Posted by Tim Roberts on 10/13/30 11:30

"Sander Peters" <sanderpeters@yahoo.com> wrote:
>
>I have the following situation:
>
>$a=5.6000;
>$b=8.9000;
>$c=($a+$b);
>
>echo $c;
>
>I will get the value '14.5' back from PHP.

Maybe. I'm sure you will shortly be coming up against the NEXT surprising
truth about floating point values, and that is that 5.6 and 8.9 cannot be
represented exactly in binary. Just like trying to represent 1/3 in
decimal, values like 5.6 and 8.9 requires an infinite number of bits. So,
the value you actually get is only an approximation.

Because of that, 5.6 + 8.9 does not actually equal 14.5 (which CAN be
represented exactly in binary). It will PRINT as 14.5, because printing
rounds it, but if you do this:

if( $c == 14.5 )
echo "yes"

it will not actually print "yes".
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация