Skip to main content
29 of 39
Rollback to Revision 26
Οurous
  • 8.1k
  • 1
  • 16
  • 38

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>()
    var jitexempt as String
    def main
        .jitexempt = .compute(111)
        .time.reset
        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
        #for angles > 360, insert `degrees %= 360` here
        .time.start
        cos as float = .cos(degrees)
        if degrees % 180, sin as float = Math.sqrt(1 - (cos * cos)) * (((degrees - 180) * -1) / Math.abs(degrees - 180))
        else, sin 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')
    def cos(degrees as float) as float
        if degrees % 180 <> 90
            rad as float = degrees * .mod
            two as float = rad * rad
            cos as float = 1
            cos -= (rad *= rad) / 2
            cos += (rad *= two) / 24
            cos -= (rad *= two) / 720
            cos += (rad *= two) / 40320
            cos -= (rad *= two) / 3628800
            cos += (rad *= two) / 479001600
            cos -= (rad *= two) / 87178291200
            cos += (rad *= two) / 20922789888000
            cos -= (rad *= two) / 6402373705728000
            cos += (rad *= two) / 2432902008176640000
            cos -= (rad *= two) / 1124000727777607680000f
            cos += (rad *= two) / 620448401733239439360000f
            cos -= (rad *= two) / 403291461126605635584000000f
            cos += (rad *= two) / 304888344611713860501504000000f
            return cos
        else, return 0

Compile it with cobra filename -turbo. Although if you can install Xamarin Studio and use this plugin, it'll provide times that are 2%-5% faster than even the -turbo flag. I'm not sure why.

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 525ms (vs 190ms)

  • The 4-entry test used by the Python and Java answers runs in 0.4µs (vs 30µs, 3µs)

  • The 1000 random angle test used by the Fortran answer runs at 100ns per angle (vs 10µs)

Οurous
  • 8.1k
  • 1
  • 16
  • 38