簡體   English   中英

浮點“錯誤”是PHP 64位

[英]Floating point 'error' is php 64bit

使用以下代碼:

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

在32位計算機上,我得到:

255744.12340511

在64位計算機上,我得到了。

255744.123405

我(幾乎)確保與32位和64位計算機無關,而是要在某個地方進行設置。
我也不認為這是浮點問題,而是php取整問題。

我希望我的精度達到16位小數。

我怎樣才能達到那種精度?

只是回答我自己的問題以避免混淆,正如Amal Murali正確指出的那樣,設置是:

在ini文件中:

; 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

或直接在您的腳本中

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

如果您有權訪問配置,則可能會稍快一些。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM