mirror of
https://github.com/cosinekitty/astronomy.git
synced 2025-12-28 10:09:26 -05:00
Fascinatingly, I still have discrepancies between a well-tested high-precision math package 'mpmath' and math.sin, math.cos. This happens running on my own hardware, which makes me think there is something wrong with my system, not GitHub's backend. Here is output comparing mpmath.sin with math.sin, and mpmath.cos with math.cos. PY Trigonometry: maxdiff=3.32359e-12, worst angle = 719.9 degrees: PASS I'm going to commit this to try it on other hardware/OS combinations.
49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
'''dontrig.py
|
|
Don Cross <cosinekitty@gmail.com>
|
|
https://github.com/cosinekitty/astronomy
|
|
|
|
These are my own hand-rolled replacements for sine and cosine
|
|
on platforms where I'm getting divergent results (order 1e-12).
|
|
I want to find out who is correct and who is wrong!
|
|
'''
|
|
import math
|
|
|
|
Debug = False
|
|
|
|
def xsin(angle:float) -> float:
|
|
angle = math.fmod(angle, 2*math.pi)
|
|
numerator = -(angle * angle)
|
|
sum = 0.0
|
|
term = angle
|
|
n = 1
|
|
while n <= 99:
|
|
# n = 1, 3, 5, ...
|
|
prev = sum
|
|
sum += term
|
|
if Debug:
|
|
print('xsin: n={:02d} term={:24.16e} sum={:20.16f} diff={:24.16e}'.format(n, term, sum, sum-prev))
|
|
if prev == sum:
|
|
return sum
|
|
term *= numerator / ((n+1) * (n+2))
|
|
n += 2
|
|
raise Exception('xsin({:0.16g}): failure to converge'.format(angle))
|
|
|
|
|
|
def xcos(angle:float) -> float:
|
|
angle = math.fmod(angle, 2*math.pi)
|
|
numerator = -(angle * angle)
|
|
sum = 0.0
|
|
term = 1.0
|
|
n = 0
|
|
while n <= 99:
|
|
# n = 0, 2, 4, ...
|
|
prev = sum
|
|
sum += term
|
|
if Debug:
|
|
print('xcos: n={:02d} term={:24.16e} sum={:20.16f} diff={:24.16e}'.format(n, term, sum, sum-prev))
|
|
if prev == sum:
|
|
return sum
|
|
term *= numerator / ((n+1) * (n+2))
|
|
n += 2
|
|
raise Exception('xcos({:0.16g}): failure to converge'.format(angle))
|