浮动值的问题 [英] problem with floating values

查看:166
本文介绍了浮动值的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ array_test [test] = round($ value, 2); //首先我取整一个值
echo $ array_test [test]; // 0,66
$ s_array_test = serialize($ array_test); //序列化数组
var_dump($ s_array_test)//(...)s:4:test; d:0.66000000000000003108624468950438313186168670654296875;}(...)


这很烦人,因为序列化的数组存储在数据库中,使用更多的空间...

如何解决这个问题?



thx

解决方案

全部阅读手册 http://php.net/manual /en/language.types.float.php


浮点数的精确度有限。虽然它取决于系统,但是PHP通常使用IEEE 754双精度格式,由于按照1.11e-16的顺序舍入,所以会产生最大的相对误差。非基本的算术运算可能会给出更大的错误,当然,当多个运算复合时,必须考虑错误编程。

另外,有理数可以精确地表示为浮点在基数为10的点数(如0.1或0.7)中,无论尾数大小如何,都没有在基数2中使用的浮点数。因此,他们不能被转换成他们的内部二进制对应,没有一点精确度的损失。这可能会导致混乱的结果:例如,floor((0.1 + 0.7)* 10)通常会返回7而不是预期的8,因为内部表示将类似于7.9999999999999991118。 b
因此,不要将浮点数结果信任到最后一位数字,也不要将浮点数相比较。如果需要更高的精度,可以使用任意的精度数学函数和gmp函数。

您可以使用 sprintf ()(而不是 round())将float转换为一个固定大小的字符串

  sprintf('%。2f',$ float); 

但我建议您创建并使用真正的数据库模式。如果你只是把非结构化的字符串放在里面,你根本不需要数据库。您可以使用简单的平面文件。


I ve got a strange php behaviour with floating value

$array_test["test"]= round($value,2); //first I round up a value
echo $array_test["test"]; //0,66
$s_array_test= serialize($array_test); //serializing the array
var_dump($s_array_test) // (...)s:4:"test";d:0.66000000000000003108624468950438313186168670654296875;}(...)

This is pretty annoying cause the serialized array is stored into a db a use more space...

How to fix this ?

thx

解决方案

First of all read the section about floats in the manual http://php.net/manual/en/language.types.float.php

Floating point numbers have limited precision. Although it depends on the system, PHP typically uses the IEEE 754 double precision format, which will give a maximum relative error due to rounding in the order of 1.11e-16. Non elementary arithmetic operations may give larger errors, and, of course, error progragation must be considered when several operations are compounded.

Additionally, rational numbers that are exactly representable as floating point numbers in base 10, like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2, which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118....

So never trust floating number results to the last digit, and never compare floating point numbers for equality. If higher precision is necessary, the arbitrary precision math functions and gmp functions are available.

You may use sprintf() (instead of round()) to convert the float into a fixed-sized string

sprintf('%.2f', $float);

But I suggest you to create and use a real database schema. You don't need a database at all, if you just put unstructured strings in it. You can use simple flatfiles instead.

这篇关于浮动值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆