Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- def sincos(x, n)
- if x < 0
- s, c = sincos(-x, n)
- return [-s + 0, c]
- end
- x -= 2 * Math::PI while x >= 2 * Math::PI
- case x
- when 0 .. Math::PI / 4
- return sincos_sub(x, n)
- when Math::PI / 4 .. Math::PI / 2
- s, c = sincos_sub(Math::PI / 2 - x, n)
- return [c, s]
- when Math::PI / 2 .. Math::PI
- s, c = sincos(Math::PI - x, n)
- return [s, -c]
- when Math::PI .. 2 * Math::PI
- s, c = sincos(2 * Math::PI - x, n)
- return [-s, c]
- end
- end
- def sincos_sub(x, n)
- sign = 1; pow = 1.0; fact = 1; sin = 0.0; cos = 0.0
- n.times do |i|
- cos += sign * pow / fact
- pow *= x
- fact *= 2 * i + 1
- sin += sign * pow / fact
- pow *= x
- fact *= 2 * i + 2
- sign = -sign
- end
- [sin, cos]
- end
- def main
- puts '#deg: sin(err) cos(err)'
- 0.step(by: 5, to: 360) do |i|
- x = i * Math::PI / 180.0
- s, c = sincos(x, 1000)
- next if s.nan? or c.nan?
- ds = (s - Math.sin(x)).abs
- dc = (c - Math.cos(x)).abs
- puts "#{i}: #{s}(#{ds}) #{c}(#{dc})"
- end
- end
- main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement