strange behavior, when converting float to int

This is a discussion on strange behavior, when converting float to int within the PHP General forums, part of the PHP Programming Forums category; Hi, all! Is this a bug? max@maximus:~$ /usr/bin/php 1.php 45 45 float(46) max@maximus:~$ /usr/...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-06-2008
Maxim Antonov
 
Posts: n/a
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
  #2 (permalink)  
Old 05-06-2008
Philip Thompson
 
Posts: n/a
Default Re: [PHP] strange behavior, when converting float to int

On May 6, 2008, at 1:38 AM, Maxim Antonov wrote:

> 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:~$



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 (permalink)  
Old 05-06-2008
Paul Scott
 
Posts: n/a
Default Re: [PHP] strange behavior, when converting float to int


On Tue, 2008-05-06 at 09:34 -0500, Philip Thompson wrote:

> 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.


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/p...disclaimer.htm

Reply With Quote
  #4 (permalink)  
Old 05-06-2008
Thiago Pojda
 
Posts: n/a
Default RES: [PHP] 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 [mailto:philthathril@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

On May 6, 2008, at 1:38 AM, Maxim Antonov wrote:

> 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:~$



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 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 10:04 PM.


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