PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > strange behavior, when converting float to int
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
strange behavior, when converting float to int

Réponse
 
LinkBack Outils de la discussion
Vieux 06/05/2008, 07h38   #1
Maxim Antonov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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:~$

  Réponse avec citation
Vieux 06/05/2008, 15h34   #2
Philip Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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

  Réponse avec citation
Vieux 06/05/2008, 15h40   #3
Paul Scott
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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

  Réponse avec citation
Vieux 06/05/2008, 16h24   #4
Thiago Pojda
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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




  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 10h18.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12310 seconds with 12 queries