In a footnote to the earlier put up, I stated that Python’s math library can calculate the logarithm of extraordinarily massive numbers however not the cosine. This put up will broaden on that remark.
On this put up I’ll use n = 200! as my instance relatively than 1000! nbecause this worth of N is bigger than the most important representable floating level quantity however sufficiently small to be extra handy to work with.
Suppose somebody calculates 200! for you:
78865786736479050355236321393218506229513597768717326329474253324435 94499634033429203042840119846239041772121389196388302576427902426371 05061926624952829931113462857270763317237396988943922445621451664240 25403329186413122742829485327752424240757390324032125740557956866022 60319041703240623517008587961789222227896237038973747200000000000000 00000000000000000000000000000000000
You can now calculate log(n) utilizing
n = 7.886578673647905 × 10374
and so
log(n) = log(7.886578673647905 × 10374)
= log(7.886578673647905) + 374 log(10) = 863.2319871924055.
The important thing factor that makes this doable is that the least important digits of n solely impact the least important digits of log(n). Within the calculation above I saved the primary 16 digits of n. Python couldn’t make use of any extra digits, and had no want of any extra digits, in an effort to produce the logarithm to machine precision.
Cosine doesn’t work that means. The cosine of n will depend on the rest when n is split by 2π, and that the rest will depend on each single digit of n. I’ll illustrate that under.
Utilizing bc -l and setting the dimensions to 400, I can calculated n then calculate
cos(n + 10i)
for i operating from 0 to 374, tweaking every digit one after the other. (Besides when a digit is a 9 and the addition leads to a carry.)
n = 1
for (i = 1; i <= 200; i++) n *= i
scale = 400
for (i = 1; i <= 374; i++) {
x = c(n+10^i)
scale = 16
print x/1, "n"
scale = 400
}
Right here’s what a plot of the outcomes seem like.
The worth of cos(n) is about −0.985, however the values above are everywhere in the map. We are able to take a look at the vary by projecting all of the factors over to the left edge then rotating 1 / 4 flip:
![]()
The outstanding factor about this picture is that there are a couple of gaps, i.e. a couple of values the cosine does not tackle.
Right here’s a extra refined means to take a look at it. The sequence 10i mod 2π is dense in [0, 2π], and so by going far sufficient out within the sequence, we will discover a worth that shifts the part of n by any desired quantity inside any given tolerance.
Each digit in n issues, and altering any digit can change the worth of cosine to be primarily any worth. You can not calculate the cosine of an unlimited quantity with out utilizing some form of prolonged precision arithmetic. There are intelligent vary discount algorithms that reduce the quantity of prolonged arithmetic crucial, however prolonged arithmetic can’t be utterly eradicated.
