Bug #80241 [Com]: Casts double to int

From: Date: Thu, 15 Oct 2020 14:40:25 +0000
Subject: Bug #80241 [Com]: Casts double to int
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-229649@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80241&edit=1 ID: 80241 Comment by: dmitrijlin9 at gmail dot com Reported by: dmitrijlin9 at gmail dot com Summary: Casts double to int Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: Windows 10 PHP Version: 7.4.11 Block user comment: N Private report: N New Comment: Ok, but why if we have variable $a = '5.99' the answer is correct? Previous Comments: ------------------------------------------------------------------------ [2020-10-15 14:35:50] danack@php.net fyi, using https://github.com/Danack/FloatHex and running echo float_compare(216699, 2166.99 * 100); it's possible to see the rounding error between these two numbers is a single bit. ┌──────┬─────────────┬──────────────────────────────────────────────────────┐ │ Sign │ Exponent │ Mantissa │ │ 0 │ 10000010000 │ 1010011100111101100000000000000000000000000000000000 │ │ 0 │ 10000010000 │ 1010011100111101011111111111111111111111111111111111 │ │ - │ ----------- │ ----------------xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx │ └──────┴─────────────┴──────────────────────────────────────────────────────┘ ------------------------------------------------------------------------ [2020-10-15 14:33:35] girgias@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. ------------------------------------------------------------------------ [2020-10-15 14:30:35] dmitrijlin9 at gmail dot com Description: ------------ I'm trying to cast double variable to int, but i have wrong answer Test script: --------------- $a = '2166.99'; echo (int)($a *100); Expected result: ---------------- 216699 Actual result: -------------- 216698 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=80241&edit=1

« previous php.bugs (#229649) next »