PHP floating point calculation problem

巴扎黑
Release: 2016-11-22 11:06:58
Original
1575 people have browsed it

If you use PHP's +-*/ to calculate floating point numbers, you may encounter some problems with incorrect calculation results. For example, echo intval(0.58*100); will print 57 instead of 58. This is actually due to the inability of the underlying binary system of the computer. A bug in accurately representing floating point numbers

is cross-language. I also encountered this problem using python. So basically most languages ​​provide class libraries or function libraries for precise calculations. For example, PHP has a BC high-precision function library. Here are some commonly used BC high-precision functions.

php BC high-precision function library includes: addition, comparison, division, subtraction, remainder, multiplication, nth power, configure the default number of decimal points, and square. These functions are more useful when it comes to monetary calculations, such as e-commerce price calculations.

/**

* Compare two high-precision numbers

*

* @access global

* @param float $left

* @param float $right

* @param int $scale The number of decimal places to be accurate

*

* @return int $left==$right returns 0 | $left<$right returns -1 | $left>$right returns 1

*/

var_dump(bccomp($left=4.45, $right=5.54, 2));

// -1

/**

* Add two high-precision numbers

*

* @access global

* @param float $left

* @param float $right

* @param int $scale The number of decimal places to be accurate

*

* @return string

*/

var_dump(bcadd($left=1.0321456, $right=0.0243456, 2));

//1.04

/**

* Subtract two high-precision numbers

*

* @access global

* @param float $left

* @param float $right

* @param int $scale The number of decimal places to be accurate

*

* @return string

*/

var_dump(bcsub($left=1.0321456, $right=3.0123456, 2));

//-1.98

/**

* Divide two high-precision numbers

*

* @access global

* @param float $left

* @param float $right

* @param int $scale The number of decimal places to be accurate

*

* @return string

*/

var_dump(bcdiv($left=6, $right=5, 2));

/ /1.20

/**

* Multiply two high-precision numbers

*

* @access global

* @param float $left

* @param float $right

* @param int $scale The number of decimal places to be accurate

*

* @return string

*/

var_dump(bcmul($left=3.1415926, $right=2.4569874566, 2));

//7.71

/**

* Set the number of decimal points for the bc function

*

* @access global

* @param int $scale The number of decimal points to be accurate

*

* @return void

* /

bcscale(3);

var_dump(bcdiv('105', '6.55957'));

// 16.007

Note: Regarding the number of digits set, the excess is discarded, not Rounding


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!