#42658 [Opn->Bgs]: floats not converted to ints correctly

From: Date: Tue, 18 Sep 2007 11:01:15 +0000
Subject: #42658 [Opn->Bgs]: floats not converted to ints correctly
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-117638@lists.php.net to get a copy of this message
ID: 42658 Updated by: jani@php.net Reported By: contax at phrenetic dot org -Status: Open +Status: Bogus -Bug Type: Unknown/Other Function +Bug Type: Math related Operating System: Darwin PHP Version: 5.2.4 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://docs.sun.com/source/806-3568/ncg_goldberg.html Thank you for your interest in PHP. Previous Comments: ------------------------------------------------------------------------ [2007-09-13 14:06:19] contax at phrenetic dot org Description: ------------ When converting specific numbers to integers via strings things seem to go a little wrong. (I have also tested this on 5.2.0 on both FreeBSD and Debian) Reproduce code: --------------- <?php $no1 = '18.74'; $no2 = $no1 * 100; $no3 = (int) $no2; $no4 = intval($no2); var_dump($no1); var_dump($no2); var_dump($no3); var_dump($no4); ?> Expected result: ---------------- string(5) "18.74" float(1874) int(1874) int(1874) Actual result: -------------- string(5) "18.74" float(1874) int(1873) int(1873) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=42658&edit=1

« previous php.bugs (#117638) next »