For the first time today, for the first time in the online industry for 15 years
I want 30,000 yen + consumption tax (8%)
An extremely simple formula that seems to be common
alert (30000 * 1.08);
The calculation result is of course ¥ 32,400
I found the
bugspecification for compliance with the IEEE754 floating point arithmetic standard
And here is the question
The precision of floating point numbers is finite. Depending on the system, PHP normally uses the IEEE 754 double-precision format. This format introduces errors in rounding on the order of 1.11e-16. If i do complex arithmetic, the error will be even greater. And of course, errors must be taken into account when combining several operations.
In addition, rational numbers that can be represented in decimal numbers with an exact decimal number, such as 0.1 and 0.7, cannot be represented exactly as binary floating-point numbers. This is the same no matter how large the mantissa is. Therefore, when converting it to an internal binary representation, the accuracy is inevitably reduced. As a result, it can cause strange results. For example, the result of floor ((0.1 + 0.7) * 10) will usually be 7. Perhaps i am expecting 8, but that is not the case. This is because the internal value of this calculation is 7.9999999999999991118 ...
So don't trust the last digit of the decimal, and don't compare the decimals directly to see if they are equal. If i need higher precision, use an arbitrary precision math function or the gmp function instead.
The same calculation with PHP (5.3.3) as a trial
echo 30000 * 1.08;
The result seemed to be a normal value of "32400" (is the same result as a general calculator more accurate?)
Is the IEEE754 compliance ambiguous?
Please tell me who i am busy, but understand
Answer # 1
When I did the same calculation in PHP (5.3.3), the result seemed to be a normal value of "32400" (is the same result as a general calculator more accurate?)
It'slooks only. In fact,
var_dump (30000 * 1.08 === 32400.0);will display
Answer # 2
The result calculated by the calculator is only the result obtained by decimal calculation
The result obtained by floating point calculation is the result obtained by binary calculation
This is not a floating-point calculation error, but a result error that occurs when the result is converted to decimal
It's bad if you don't recognize it properly.
- regarding the addition of the blog category function in the cakephp3 tutorial, the selected category is not reflected in the con
- php - regarding error handling when stripe settlement fails
- php - regarding the matter that the screen transition in the inquiry form returns
- regarding and post processing in php
- php - regarding the judgment of whether csrf measures are necessary
- css3 - [html&css] regarding misalignment of sentences after line breaks
- i want to do web scraping using php, phamtomjs, phpquery
- php - i can't get the id name of mceu_13 on the reply screen of bbpress
- php - i want to create a task display/hide function using js
- php - please tell me how to apply when multiple style sheets are applied
- i want to get the value of a textbox with jquery and pass the value to php
- php - about checking method of data check in web application
- php - how to apply style from child to reply part visual of bbpress
- php - i'm making a game that uses a database