I have some Ruby math questions that need answering

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 * 10x).round.to_f / 10x
end

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

def floor_to(x)
(self * 10x).floor.to_f / 10x
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.

On Jul 16, 2009, at 11:55 PM, Älphä Blüë wrote:

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…

99.0/100 is sufficient here

Älphä Blüë wrote:

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/

99.0/100 or 99/100.0 will also work! You would have to keep one floating
point to get a floating point result!

regards,
Vikas Sarin
Senior Software Quality Engineer
QA Infotech
Email: [email protected]
Mob#: +919810319641

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs