Skip to content

Instantly share code, notes, and snippets.

@stephanh42 stephanh42/sindg.py
Created Jun 12, 2018

Embed
What would you like to do?
"""Sine in degrees.
Copyright 2018 Stephan Houben
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import math
# These values are otherwise one ulp too low.
# Note that these fixups don't change the monotonicity
# around those values.
_sine_fixup_values = {
30: 0.5,
45: math.sqrt(0.5)
}
def sindg(angle):
# First reduce angle to [0,90] interval.
angle = math.fmod(angle, 360)
if angle < 0:
angle = -angle
negate = True
else:
negate = False
if angle >= 180:
negate = not negate
angle = angle - 180
if angle > 90:
angle = 180 - angle
# A few values we treat special.
result = _sine_fixup_values.get(angle)
if result is None:
# Use normal sine in radians.
result = math.sin(math.radians(angle))
if negate:
result = -result
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.