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 precision 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):
In PHP and JavaScript
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.5E7and get precisely3.125e+42because PHP and JavaScript will essentially multiply the groups of significant figures and add up the exponents. However, if you ADD those two values you get1.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 out120000000000000000000000000000000000 + 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.