Cobra
class Trig
const mod as float = 0.0174532925199433f #0.017453292519943295769236907684886127134 = tau/360
var time as System.Diagnostics.Stopwatch = System.Diagnostics.Stopwatch()
var output as List<of String> = List<of String>()
def main
for line in File.readLines('trig.in'), .output.add(.compute(float.parse(line)) + '\n')
File.writeAllLines('trig.out', .output)
print .time.elapsed
def compute(degrees as float) as String
.time.start
if degrees % 180
rad as float = sin as float = degrees * .mod
two as float = rad * rad
sin -= (rad *= two) / 6
sin += (rad *= two) / 120
sin -= (rad *= two) / 5040
sin += (rad *= two) / 362880
sin -= (rad *= two) / 39916800
sin += (rad *= two) / 6227020800
sin -= (rad *= two) / 1307674368000
sin += (rad *= two) / 355687428096000
sin -= (rad *= two) / 121645100408832000
sin += (rad *= two) / 51090942171709440000f
sin -= (rad *= two) / 25852016738884976640000f
sin += (rad *= two) / 15511210043330985984000000f
sin -= (rad *= two) / 10888869450418352160768000000f
sin += (rad *= two) / 8841761993739701954543616000000f
else, sin as float = 0
if degrees % 180 <> 90, cos as float = Math.sqrt(1 - sin * sin) * ((Math.abs(degrees - 180) - 90) / Math.abs(Math.abs(degrees - 180) - 90))
else, cos as float = 0
tan as float = sin / cos
.time.stop
return sin.toString('0.000000E+0') + ' ' + cos.toString('0.000000E+0') + ' ' + tan.toString('0.000000E+0')
Compile it with cobra filename -turbo
The output is now 100% accurate to the specified number of sigfigs,
and is almost as fast as the inbuilt functions (but more accurate).
Tests: AMD FX6300 @5.1GHz
The 360 * 10000 test used by the C answer runs in 652ms (vs 190ms)
The 4-entry test used by the Python answer runs in 4.6µs (vs 50µs)
The 1000 random angle test used by the Fortran answer runs at 180ns per angle (vs 10µs)