This Mathematica tutorial notes some strange behaviour with calculations such as Sin[10^50]
:
N[Sin[10^50]]
> -0.4805
Sin[10.0^50]
> 0.92455
Wait, what?
I know of the infamous $0.1+0.2\not=0.3$ in floating point arithmetic - but I didn't think that would apply for a number like 10.0
, surely?
For example, Python doesn't have such a discrepancy:
>>> import math
>>> math.sin(10**50)
-0.4805001434937588
>>> math.sin(10.0**50)
-0.4805001434937588
I then noticed that, if I type in 10.0^50
and hit shift-enter, then copy-paste the result, I see:
9.999999999999997`*^49
Also:
10^50 - 10.0^50
> 4.15384*10^34
$10^{34}$? That's quite a difference. Roughly 41 decillion
according to IntegerName[Round[10^50-10.0^50]]
.
So my main question is: Why does this inaccuracy happen for an integer expressed in machine-precision, rather than it only happening for decimal numbers? Usually issues like $0.2+0.1=0.30000000000000004$ only happen because you can't express $0.1$ in binary - but $10.0$ can be expressed accurately in binary, right? So how does 10.0^50
go so wrong?
10.^50
that can't be represented exactly as machine number (although it's indeed an integer).2^50
or4^50
, however, can. $\endgroup$IntegerDigits[10^50, 2]
will show that one cannot get the "exact" representation in base 2 using only the 53 bits of a machine double mantissa. The error is, not surprisingly, on the order of machine epsilon, which explains the approx 10^34 difference in that subtraction. $\endgroup$