Bug #75920 [Nab]: precision problem with decimals read from sql server

From: Date: Tue, 06 Feb 2018 09:18:33 +0000
Subject: Bug #75920 [Nab]: precision problem with decimals read from sql server
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-213828@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75920&edit=1 ID: 75920 Updated by: requinix@php.net Reported by: km dot wrona at gmail dot com Summary: precision problem with decimals read from sql server Status: Not a bug Type: Bug Package: PDO DBlib Operating System: Alpine PHP Version: 7.1.14 Block user comment: N Private report: N New Comment: 1. 1.6 written literally in PHP is also imprecise. https://3v4l.org/IMCc5 2. FLOAT is a double. With a high enough precision setting you will see it's not exactly 1.6 either. 3. DECIMAL(10,2) = 10 significant digits. In PHP that corresponds to precision=10. Anything lower will round, anything higher will be unpredictable. Floating-point problems are not specific to PHP. If you want to PHP to act a certain way then you need to learn how it all works and change your php.ini settings and/or code to suit. Previous Comments: ------------------------------------------------------------------------ [2018-02-06 08:59:42] km dot wrona at gmail dot com Thank you for your explanation, except I am not sure if you've read my post. 1) That 1.6 and any other works OK and is represented properly when defined in php code 2) That 1.6 is represented properly when the column in SqlServer table is defined as float (or when a value is casted to float) 3) That 1.6 IS NOT represented properly when the column in SqlServer table is defined as decimal(10,2) (or when a value is casted to decimal) ------------------------------------------------------------------------ [2018-02-06 06:30:22] rasmus@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. 1.6 can't be represented exactly as a floating point value. ------------------------------------------------------------------------ [2018-02-05 13:29:42] km dot wrona at gmail dot com Description: ------------ The number is not represented properly when the column is defined as decimal. Test script: --------------- var_dump($this->selectRaw('CAST(\'1.6\' as decimal(10,2)) as dec, CAST(\'1.6\' as float) as flo')->limit(1)->get()); exit; array(1) { [0]=> object(stdClass)#789 (2) { ["dec"]=> float(1.5999999999999) ["flo"]=> float(1.6) } } //default behaviour ["dec"]=> float(1.5999999999999) //with //ini_set('precision', 25); //ini_set('serialize_precision', 25); ["dec"]=> float(1.599999999999909050529823) Expected result: ---------------- 1.6 no matter if decimal or float ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=75920&edit=1

« previous php.bugs (#213828) next »