Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compute the sine of huge numbers

For several days, I've been wondering how it would be possible of computing the sine of huge numbers with magnitude around 100000! (radians). The factorial is just an example the number itself can be any not just a factorial product ...) I obviously don't use double but cpp_rational from the boost multiprecision library. But I can't simply do 100000! mod 2pi and then use the builtin function sinl (I don't need more than 10 decimal digits..) as I'd need several million digits of pi to do this accurately.

Is there any way to achieve this?

like image 965
borchero Avatar asked Jul 04 '16 21:07

borchero


Video Answer


2 Answers

This is in general a non trivial task, as it has many similarities with the Discrete Logarithm Problem, which implies in its turn a computationally intensive calculation.
That said, your calculation could be easier if you consider the logarithm of 100000!/pi, as it reduces to the sum of logs of all positive integers equal or less than 100000, and a subtraction: log(N!/pi) = \sum_{i=0}^N (log i) - log(pi). If you exponentiate this number, you have an approximate evaluation of (N!/pi). Subtract the integer part, and multiply the result by pi. This is the estimate of your N! mod pi.
In formula:

As you may notice, I used many times the word approximate. This is due to the following considerations:

  • you have to calculate many logs, which have some cost and errors
  • you may want to change the base of your log, according to your problem size; this again is going to affect the accuracy and the precision of your result
  • you have to exponentiate back: small errors may lead to large ones
  • subtract large numbers: may lead to large cancellations
  • multiply by pi and evaluate the sin: again errors

If you think it may be beneficial, consider using the Stirling's approximation.

As a final remark, there is not an easy solution to these kind of problems, you always has to deal with them case by case.

like image 98
fedino Avatar answered Sep 28 '22 04:09

fedino


Note: = pi

To calculate sin of a very big number in radians (Change them to multiples of  by dividing by 3.1415)
1. Observe this: sin 0 = 0, sin 0.5pi = 1, sin pi = 1, sin1.5pi = -1, sin 2pi=0
2. Even or odd integer values in front of pi, the sin is 0
3. For real values (those with decimal points), for even numbers before the decimal point, take it as the 0. something as the value of the sine, for odd, then take the 1. something as the value of the sine.
4. See examples *Note that sin and cosine are periodic in nature, that is why it is possible to do it in this way for big or small numbers. :)

Eg. (Use your calculators to check out the calculations)

1.0 In radians: sin 100 = -0.506
Divide by 3.1415
Do in deg
Sin 31.831pi (31.831 is a real value) = sin1.831(180) =-0.506, check

2.0 In radians: sin 50 = -0.2623
Divide by 3.1415
Do in deg
Sin 15.9155pi = sin1.9155 (180) =-0.2623

3.0 In radians: sin 700 = 0.5439
Divide by 3.1415
Do in deg
Sin 222.8169pi = sin0.8169 (180) =-0.5440, check

4.0 In radians: sin 15000 = 0.8934
Divide by 3.1415
Do in deg
Sin 4774.6483pi = sin0.6483 (180) = 0.893, check

You can see that all the answers checked out with the direct calculation of the values using the calculator in radian mode. Hope this is helpful.

If you want to write out a compute program, best of luck in figuring out the algorithm.

like image 39
Norlida Kamarulzaman Avatar answered Sep 28 '22 04:09

Norlida Kamarulzaman