Bug #79355 [Nab]: number round + jscon_decode generate mistery numbers

From: Date: Mon, 09 Mar 2020 08:16:25 +0000
Subject: Bug #79355 [Nab]: number round + jscon_decode generate mistery numbers
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-225961@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79355&edit=1 ID: 79355 Updated by: nikic@php.net Reported by: php at cylancer dot net Summary: number round + jscon_decode generate mistery numbers Status: Not a bug Type: Bug Package: *Math Functions Operating System: Debian Buster 64Bit PHP Version: 7.3.15 Block user comment: N Private report: N New Comment: You likely have serialize_precision set to some unnecessarily high value. Please leave serialize_precision at the default of -1, unless you really know what you're doing. Previous Comments: ------------------------------------------------------------------------ [2020-03-08 23:26:00] requinix@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-03-08 23:21:06] php at cylancer dot net Description: ------------ I use the PHP Version 7.3.14-1~deb10u1 After Json decoding, rounded numbers have many unexpected decimal places. Test script: --------------- $a = 4; $b = 6; $c = $a * 100 / $b; echo '$c = '. $c." \n\n"; $d = round($c, 1); // the second parameter is important echo '$d = round($c, 1);'."\n\n"; echo '$d = '.$d. ' <--- is rounded $c'."\n\n"; echo 'if i decode my result to json... '."\n\n"; echo json_encode($d)."\n\n"; echo 'then my number is not rounded.'."\n\n"; The output is: $c = 66.6666666667 $d = round($c, 1); $d = 66.7 <--- is rounded $c if i decode my result to json... 66.7000000000000028421709430404007434844970703125 then my number is not rounded. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=79355&edit=1

« previous php.bugs (#225961) next »