


PHP floating point calculation comparison and inaccurate solutions to rounding, inaccurate rounding_PHP tutorial
Jul 13, 2016 am 09:45 AMSolution to inaccurate floating-point calculation comparison and rounding in PHP. Inaccurate rounding
An interesting phenomenon in PHP that should be seen in many programming languages. This is due to the problem of the computer’s own recognition of floating point numbers....
[php] view plaincopy - $f = 0.58;
- var_dump(intval($f * 100 *100)); //result 5799
- var_dump((float)($f * 100 *100)); //result 5800
- echo (int)((0.1 0.7)*10); //Result 7
- echo (float)((0.1 0.7)*10); //Result 8
[php] view plaincopy
- <?php
- $a = 0.1;
- $b = 0.7;
- var_dump(($a $b) == 0.8);
- //The printed value is actually boolean false
- <?php
- $a = 0.1;
- $b = 0.7;
- var_dump(bcadd($a,$b,2) == 0.8);// bool true
When calculating floating point numbers, remember not to convert floating point numbers into integers, otherwise unpredictable errors will occur.
So never trust that a floating point number result is accurate to the last digit, and never compare two floating point numbers to see if they are equal.

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon
