Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Ruby's Math Module

Name: Anonymous 2009-08-24 21:10

Ruby hurrrrrrrrrrr


Anyway, I've got a task that requires a lot of use sine and cosine, and I've decided that speed in computation is far more important than accuracy, as there is a very small number of digits that are significant. So the solution that I came up with is to make a method that uses a polynomial approximation instead. Something like 'check for domain and subtract pi until in domain' then apply the polynomial. Would this compute faster or slower than the built in trig functions? Is there an easy way to test this?

Name: Anonymous 2009-08-24 21:16

The same way you'd test any other program: Using time-profiling code. I'm quite sure that Ruby allows access to time in some way.

Also, you're using Ruby, so the built-in functions may perform faster.

Name: Anonymous 2009-08-24 21:38

and I've decided that speed in computation is far more important than accuracy,
speed in computation
ruby
( ≖‿≖)

Name: Anonymous 2009-08-24 22:14

Instead of using polynomial approximations, you should use a small table (~1000 entries) and linear interpolation.

You can test it easily by running the functions you're testing from the shell.

Name: Anonymous 2009-08-24 23:38

I like caching the results first.  It's both fast and accurate.

import math

sin = {}
for angle in range(360):
    sin[angle]=math.sin(angle)

while 1:
    print sin[int(raw_input('Enter number to sin():'))%360]

Name: sage 2009-08-24 23:45

>>2
You were right, a 1 million operation loop showed that the built in function was much, much faster than what I had in mind; I guess the difference is that the polynomials and all their operations each have to be processed by ruby, where the cos operation only has to be dealt with once.

Not sure about using a table though, might be worth checking.

Thanks /prog/

Name: Anonymous 2009-08-24 23:48

YOUR WELCOME :D

Name: Anonymous 2009-08-25 1:06

lern2memoize?

Name: Anonymous 2009-08-25 2:45

>>6
It's unfortunate, but Ruby didn't get its reputation as slow as fuck from nowhere. :(

Name: Anonymous 2009-08-25 4:00

I think this calls for an C module

Name: Anonymous 2009-08-25 8:36

Stop using Ruby.

Just stop.

Ruby kills.

Name: Anonymous 2010-11-16 2:23

Don't change these.
Name: Email:
Entire Thread Thread List