![]() |
sponsored links |
|
|
sponsored links
|
|
|
4
20th April 22:42
External User
Posts: 1
|
There is a logical explanation.
"[...] you need to be aware that the float data type is not always capable of representing numbers in the way you expect it to." And then follows a example: "Consider, for example this simple statement: echo (int) ((0.1 + 0.7) * 10); You would expect that the expression ((0.1 + 0.7) * 10) would evaluate to 8 (and, in fact, if you print it out without the integer conversion, it does). However the statement above outputs 7 instead. This happens because the result of this simple arithmetic expression is stored internally as 7.999999 instead of 8; when the value is converted to int, PHP simply truncates away the fractional part, resulting in a rather significant error (12.5%, to be exact)." [manually typed] Zend PHP5 Certification Guide 2nd Edition, Chapter 1 - PHP Basics, page 9-10. SHAFIK, D., RAMSEY, B. Ed. php|architect nanobooks Not sure if this is what happening with you as I did not try to reproduce your error, but looks like it. This book is good, a lot of gotchas. You should get it ![]() Regards, Thiago Henrique Pojda Desenvolvimento Web +55 41 3033-7676 thiago.pojda@softpartech.com.br Excelência em Softwares Financeiros -----Mensagem original----- De: Philip Thompson [mailto hilthathril@gmail.com] Enviada em: terça-feira, 6 de maio de 2008 11:34 Para: PHP-General List Assunto: Re: [php] strange behavior, when converting float to int I got the same results. I'm not exactly sure what's happening, but I'd be curious to see if there's anyone else who can shed some light. PHP Version 5.2.4. ~Philip -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|