Bug #65485 [Com]: Cast gives different values in one way or the other
| From: | stephane at it-asia dot com | Date: | Tue, 20 Aug 2013 10:14:59 +0000 |
| Subject: | Bug #65485 [Com]: Cast gives different values in one way or the other | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-181040@lists.php.net to get a copy of this message | ||
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:
I don't find sane to see a 3948 float value going to 3947 after an int casting, I really,
really don't find it sane.
I don't find sane as well to see the same number ending with different value after several
casts. The cast doesn't provide the same precision when it is one case, ot the other. One uses
floor(), when the other uses (round). And to be honest, one is obviously right, when the other is
obviously wrong.
Please provide examples where the float -> string -> int value doesn't give satisfaction.
Because for me, the float -> int cast NEVER gives satisfaction when there's a decimal number
involved.
The right way is the way giving the right solution, It is not the one giving the wrong answer for
the only pleasure to match impossibility theory.
In every case, please add to the documentation that the float -> int cast gives stupid results
because ti tends to respect a theoric lack orp precision, when the float -> string cast tends to
provide a more acceptable answer.
Previous Comments:
------------------------------------------------------------------------
[2013-08-20 09:53:17] nikic@php.net
I think you misunderstood how the casts work:
An (int) cast rounds DOWN to the nearest integer (in all sane languages).
A (string) cast just converts the float to a string representation. This representation has the
precision specified with the "precision" ini setting.
So what you are comparing here is a down-round ( floor($f) ) with a round-with-precision ( round($f,
$prec) ). Of course the result will be different.
------------------------------------------------------------------------
[2013-08-20 09:46:50] stephane at it-asia dot com
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.
------------------------------------------------------------------------
[2013-08-20 09:02:04] johannes@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