Skip to content

Commit

Permalink
Replaced my trig functions with mpmath package.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cosinekitty committed May 30, 2024
1 parent e1d4507 commit ad6cc89
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions generate/dontrig.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def xsin(angle:float) -> float:
prev = sum
sum += term
if Debug:
print('xsin: n={:02d} term={:24.16e} sum={:20.16f} fact={:g} diff={:24.16e}'.format(n, term, sum, fact, sum-prev))
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))
Expand All @@ -40,7 +40,7 @@ def xcos(angle:float) -> float:
prev = sum
sum += term
if Debug:
print('xcos: n={:02d} term={:24.16e} sum={:20.16f} fact={:g}'.format(n, term, sum, fact))
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))
Expand Down
19 changes: 16 additions & 3 deletions generate/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@
from itertools import chain
sys.path.append('../source/python')
import astronomy
from dontrig import xcos, xsin
#from dontrig import xcos, xsin
import mpmath

#-----------------------------------------------------------------------------------------------------------

mpmath.dps = 30

def xsin(angle: float) -> float:
return float(mpmath.sin(angle))

def xcos(angle: float) -> float:
return float(mpmath.cos(angle))

if os.getenv('GITHUB_JOB'):
# Super weird hack: re-define _sin and _cos to have exactly reproducible values.
print('test.py - NOTE: Detected GitHub job. Replacing trig functions...')
Expand Down Expand Up @@ -3375,6 +3384,7 @@ def Trigonometry() -> int:
tolerance = 3.5e-12 # Not very good yet!
maxDiff = 0.0
inFileName = 'trigonometry/trig.txt'
worstDeg = None
with open(inFileName, 'rt') as infile:
lnum = 0
for line in infile:
Expand All @@ -3392,13 +3402,16 @@ def Trigonometry() -> int:
sinCalc = xsin(rad)
cosDiff = abs(cosCalc - cosCorrect)
sinDiff = abs(sinCalc - sinCorrect)
maxDiff = max(maxDiff, cosDiff, sinDiff)
diff = max(cosDiff, sinDiff)
if diff > maxDiff:
maxDiff = diff
worstDeg = deg
if (cosDiff > tolerance) or (sinDiff > tolerance):
return Fail(
'Trigonometry({:s} line {:d})'.format(inFileName, lnum),
'EXCESS ERROR - deg={:0.1f}, cosDiff={:g}, sinDiff={:g}'.format(deg, cosDiff, sinDiff)
)
return Pass('Trigonometry: maxdiff={:g}'.format(maxDiff))
return Pass('Trigonometry: maxdiff={:g}, worst angle = {:0.1f} degrees'.format(maxDiff, worstDeg))

#-----------------------------------------------------------------------------------------------------------

Expand Down

0 comments on commit ad6cc89

Please sign in to comment.