Edit report at https://bugs.php.net/bug.php?id=65485&edit=1

 ID:                 65485
 Updated by:         johan...@php.net
 Reported by:        stephane at it-asia dot com
 Summary:            Cast gives different values in one way or the other
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            *General Issues
 Operating System:   Windows 8 & Mint Maya
 PHP Version:        5.4.18
 Block user comment: N
 Private report:     N

 New Comment:

Floating point values have a limited precision. Hence a value might 
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly 
printing it without any mathematical operations.

If you would like to know more about "floats" and what IEEE
754 is, read this:
http://www.floating-point-gui.de/

Thank you for your interest in PHP.

Casting to string will be imprecise, then doing cast to int will be imprecise 
again. Two imprecisions increase the difference.


Previous Comments:
------------------------------------------------------------------------
[2013-08-20 08:50:20] stephane at it-asia dot com

Description:
------------
I have different value when I cast a double to int and when I cast to string 
before casting to int.

I understand 39.48 is difficult to store in base 2.

The problem is the cast algorythm is not the same if you cast a float to int or 
if you cast a float to string, This involves huges mistakes in accountancy 
software. Whatever the way you choose (float -> int or float -> string -> int ) 
, you should have the same result at the end.

Please define the right way to process data in that case.

I have the same problem with almost every machines, Windows or Debian based.

Thanks !

Test script:
---------------
$d = "39.48" * "100";
print("39.48 * 100 : ");
var_dump ($d);

$i = (int) $d;
print("<br />int:  ");
var_dump ($i);

$s = (string) $d;
print("<br />string:  ");
var_dump ($s);

$i = (int) $s;
print("<br />int:  ");
var_dump ($i);

Expected result:
----------------
same value if you cast double => int and if you cast double => string => int

Actual result:
--------------
39.48 * 100 : double(3948)
int:  int(3947)
string:  string(4) "3948"
int:  int(3948)


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=65485&edit=1

Reply via email to