#
# canola - canon canola 1614p emulator
# Copyright (C) 2011 Peter Miller
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see .
#
# ---------- Card-Pocket-Begin -------------------------------------------
# Title: MS-2: cos(x)
# Conditions:
# -10**16 < x < 10**16
# x: Degree
# Accuracy: 10 digits
# Style: blank
# ---------- Card-Pocket-End ---------------------------------------------
;
;
; This uses a Taylor series expansion of cos(x)
; See http://en.wikipedia.org/wiki/Trigonometric_functions#Series_definitions
; for more information.
;
; x**2 x**6 x**6
; cos(x) = 1 - ---- + ---- - ---- + ...
; 2! 4! 6!
;
; initial X in degrees is in the X register
; M14 contains the series sum
; M13 contains the (2n)! being constructed
; M12 contains the x**(2n) being constructed
;
; Note that it doesn't bring the initial value into the -pi..pi range,
; so it is going to converge slowly for large values.
SFJ $202
/ ; convert degrees 0..360 to 0..1
360
*
SM14
1
FLOOR 0 ; next -= will truncate to zero decimals
-=
RM14
+= ; x = 0..<1
* ; convert to tradians
6.28318530718 ; first source of inaccuracy
+= ; x register now contains the x of cos(x)
*
-=
SM13 ; m13 = -x**2
1
SM14 ; m14 is used for Taylor series sum
SM12 ; m12 is the running factorial denominator
RV ; x-register is x**2, y-register is 1 (previous term)
FJ $201
RV ; x-register is numerator, y-register is previous term
*
RM13 ; work numerator
/
RM12
+= ; first part of denominator
1
M12
RV
/
RM12
+= ; second part of denominator
M14 ; add term to series sum
1
M12
RV ; x-register is most recent term, y-register is 1
*
.1
+= ; vanished yet? (y-register is most recent term)
MJ $201
CS
MJ $201
RM14 ; return series sum in x-register
SRJ