Mombu the Php Forum sponsored links

Go Back   Mombu the Php Forum > Php > strange behavior, when converting float to int
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 20th April 20:48
max
External User
 
Posts: 1
Default strange behavior, when converting float to int



Hi, all!

Is this a bug?

max@maximus:~$ /usr/bin/php 1.php
45
45
float(46)


max@maximus:~$ /usr/bin/php -v
PHP 5.3.0-dev (cli) (built: Apr 4 2008 11:37:33)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2008 Zend Technologies


max@maximus:~$ cat 1.php
<?php
$i = 10.35 + 20.76 + 5.80 + 5.80 + 3.29;
echo (int)$i;
echo "\n";
echo intval($i);
echo "\n";
var_dump($i);

max@maximus:~$
  Reply With Quote


  sponsored links


2 20th April 22:41
philthathril
External User
 
Posts: 1
Default 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
  Reply With Quote
3 20th April 22:41
pscott
External User
 
Posts: 1
Default strange behavior, when converting float to int


Is this not coming from the underlying C libs that directly use the FP
on the CPU?

I would say that the compile flags, CPU settings etc would probably have
an impact on accuracy.

--Paul


All Email originating from UWC is covered by disclaimer
http://www.uwc.ac.za/portal/public/portal_services/disclaimer.htm
  Reply With Quote
4 20th April 22:42
thiago.pojda
External User
 
Posts: 1
Default RES: strange behavior, when converting float to int


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 [mailtohilthathril@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
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666