As far as I know float can represent 14 numbers precisely.
So let's say we have
a = 564214623154
b = 54252
and we multiply this c=a*b and it should be 30609771735350808 but when compiled it shows me 3.0609771735351E+16 So as I understand it should lose some precision but when I divide c by a c/a I get 564214623154 exact result without any precision lost
another example lets say we have
c = 30609771735350808
d = 30609761111111111
e=c-d should be 10624239697 but when compiled it shows me 10624239696 so precision is lost
So is p开发者_运维技巧recision lost only when I subtract or add two numbers?
If it matters I use php
It is possible to lose precision with multiplication and division also. PHP and JavaScript store numbers in IEEE-754 format with 52 bits of mantissa and 11 bits of exponent. Some integers are represented exactly and some are not.
Let's try these:
In Real Math (generated with Ruby):
45345657434523 * 9347287748322342 / 74387422372 = 5697991604786167788
In PHP and JavaScript
45345657434523 * 9347287748322342 / 74387422372 = 5697991604786168000
So we lose precision with multiplication and division also.
EDIT: On revisiting the OP's question it seems like this was not a great answer, because the result contained over 15 decimal digits of precision. If the intent of the question is whether multiplying and dividing a bunch of numbers each of which was represented in 15 digits of precision or less, then the final result tends to keep a good deal of precision (provided you don't overflow or underflow). So you can multiply 1.25E35 * 2.5E7
and get precisely 3.125e+42
because PHP and JavaScript will essentially multiply the groups of significant figures and add up the exponents. However, if you ADD those two values you get 1.25E35 + 2.5E7 = 1.25E35
. That's right, you add 25 million to a number and it does not change! That is because, as the OP says, you only get 14 or 15 decimal digits of precision. Try adding those two values by hand by writing out 120000000000000000000000000000000000 + 25000000
. The 14-15 digits start counting from the left and you can't pick them all up.
Bottom line is precision problems are more likely to arise with addition and subtraction. Good to be aware of.
In your first case you lose no precision, PHP is just formatting the larger number as a float. (Internally the number is kept as a float.) Try this go get the "precise" output:
$a = 564214623154;
$b = 54252;
$c = $a * $b;
printf("%u, %u\n", $c, $c/$a);
Next up, in the case of c * d
, your two numbers individually already exceed the capacity of a standard IEEE-64-bit float (which is 53 bit, while you need at least 55), so precision is already lost when you store those numbers.
The problem of losing precision during addition/subtraction is called "cancellation": All the most-significant bits on which you spent all your storage canceled out, and you end up with not enough accurate bits to fill up the manitssa. C'est la vie.
Imagine you're sitting on the moon and you take two measurements of your brother's beard hair length in Worcester, UK. Comparing the two measurements suffers from your requirement to store a very large amount of precision.
精彩评论