Edit report at https://bugs.php.net/bug.php?id=65485&edit=1
ID: 65485 Comment by: stephane at it-asia dot com Reported by: stephane at it-asia dot com Summary: Cast gives different values in one way or the other 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: Yes I know about the imprecision and the link, this is what you say all the time here. But if you THINK about the matter, the casting algorythm is different, and this is WRONG. Other languages found solutions for that. I can't believe in 2013 PHP still cannot provide a reliable solution to make simple operation like "multiplications" and int casting. The cast algorythm for float -> string -> int is GOOD. It is ALL THE TIME. The cast algorythm for float -> int is WRONG. It is ALL THE TIME. Please implement the same solution in both sides, hence choose the good one. Previous Comments: ------------------------------------------------------------------------ [2013-08-20 09:02:04] johan...@php.net 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. ------------------------------------------------------------------------ [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