Posts tagged as:

ruby

Raise a value to a negative power in Ruby

February 20, 2008

No Gravatar

I ran into a few kinks trying to raise some numbers to a negative power in Ruby. Ruby uses the ** operator for exponential calculations. Here’s the little hiccup I encountered:

1
2
3
4
5
6
7
8
# 2^2 = 4 
# here is the IRB dump
>> 2**-2
=> Rational(1, 4)
 
# but I don't want the actual number!
>> (2**-2).to_f
=> 0.25

{ Comments }