I have some Ruby math questions that need answering

  • Thread starter Älphä Blüë
  • Start date
Ã

Älphä Blüë

When handling percentage calculations:

Simple 99/100..

Will I always have to do 99.to_f/100.to_f in order to receive 0.99? Or
is there a simpler way of performing this task in ruby..

I notice that there doesn't appear to be a round method with decimal
places (or perhaps I can't find one) so I found this one which appears
to work:

class Float
def round_to(x)
(self * 10**x).round.to_f / 10**x
end

def ceil_to(x)
(self * 10**x).ceil.to_f / 10**x
end

def floor_to(x)
(self * 10**x).floor.to_f / 10**x
end
end

The question I have is can I expand upon divisor behavior and create
something that changes the way ruby behaves with integers/floats on a
more permanent basis?

I only ask because I'm working with standard deviation and some advanced
mathematics that revolve around calculating variance. I just don't want
to experience any gotchas halfway down my project.

Lastly, is there a good core site that houses all of the API for ruby
math functions or refined methods that deal with variance?

Many thanks.
 
R

Ralf Mueller

Älphä Blüë said:
When handling percentage calculations:

Simple 99/100..

Will I always have to do 99.to_f/100.to_f in order to receive 0.99? Or
is there a simpler way of performing this task in ruby..
or you use

require 'mathn'



and I bet, there is much stuff in the facets-pakage:
http://facets.rubyforge.org/
 
Ã

Älphä Blüë

Thanks everyone - I appreciate the information. This does help me out
going forward. I'll look into facets as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top