namespace = FNmathutils\cos

; $\FNmathutils\cos\in = value
; run = commandlist\FNmathutils\cos\run
; result = $\FNmathutils\cos\result

[Constants]
global $in
global $result

[CommandListRun]
local $HalfPI = 1.570796325
local $PI = 3.14159265

if $in == null || $in == NaN || $in == inf || $in == -inf
    $result = NaN
else
    local $mirrorFactor = ((($in + $HalfPI) / $PI) % 2) // 1
    local $direction = 1 - 2 * $mirrorFactor

    local $adjustedAngle = (($in + $HalfPI) % $PI) - $HalfPI
    local $squared = $adjustedAngle * $adjustedAngle

    local $termA = -0.5 * $squared
    local $termB = (1/24) * $squared * $squared
    local $termC = -(1/720) * $squared * $squared * $adjustedAngle * $adjustedAngle
    local $termD = (1/40320) * $squared^4
    local $termE = -(1/3628800) * $squared^5

    $result = $direction * (1 + $termA + $termB + $termC + $termD + $termE)
endif

$in = 0