Bug #68065 [Opn->Nab]: In some cases intval after floatval gives unexpected results

From: Date: Sat, 20 Sep 2014 19:47:54 +0000
Subject: Bug #68065 [Opn->Nab]: In some cases intval after floatval gives unexpected results
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-187628@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68065&edit=1 ID: 68065 Updated by: rasmus@php.net Reported by: kstirn at gmail dot com Summary: In some cases intval after floatval gives unexpected results -Status: Open +Status: Not a bug Type: Bug Package: Math related Operating System: Windows PHP Version: Irrelevant 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. . Previous Comments: ------------------------------------------------------------------------ [2014-09-20 19:10:21] kstirn at gmail dot com Description: ------------ See test script - after multiplication and floatval, intval gives unexpected (wrong) result. This only seems to happen with some special initial $a floats, not in all cases. I know floating point precision cannot be trusted, but in this case it may be hard to blame it? Tested on Windows 7 and Linux (CentOS 6) with PHP 5.3.29 and 5.6.0 Test script: --------------- $a = 1.2812; $a = $a * 10000; echo "$a<br>"; $a = floatval($a); echo "$a<br>"; $a = intval($a); echo "$a<br>"; Expected result: ---------------- 12812 12812 12812 Actual result: -------------- 12812 12812 12811 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=68065&edit=1

« previous php.bugs (#187628) next »