简体   繁体   中英

Floating point 'error' is php 64bit

Using the following code:

<?php
  $balance = (float)255744.12340511001247912645339965820312;
  print_r($balance);
?>

On a 32 bit machine I get:

255744.12340511

On a 64 bit machine I get.

255744.123405

I am, (almost), sure that it has nothing to do with 32 vs 64 bit machine but rather that it is a setting somewhere.
I also don't think it is a floating point issue but rather a php rounding issue.

I would like my precision to go to 16 decimals.

How can I get that level of precision?

Just to answer my own question to avoid confusion, as rightly pointed out by, Amal Murali the setting is:

in the ini file:

; The number of significant digits displayed in floating point numbers.
; http://php.net/precision
precision = 32

; When floats & doubles are serialized store serialize_precision significant
; digits after the floating point. The default value ensures that when floats
; are decoded with unserialize, the data will remain the same.
serialize_precision = 32

Or directly in your script

ini_set('precision', 32);
ini_set( 'serialize_precision', 32 );

If you have access to the configuration it might be slightly faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM