On Thu, Mar 25, 2010 at 12:47 PM, Charlie Ca [email protected]
wrote:
Hi,
I am building a program that calculates the data points of a circle
based on 1 data point that the user enters. The user could enter the
radius circumference or area and get back the other properties of the
circle. I’m getting a ‘Circles.rb:5: warning: Float 3.14 out of range’
in this context;
value1 = gets.chomp.to_f
puts 'The circle is of radius ’ + [(value1/3.14)/2].to_s + ‘units’
The following works for me (I changed the 2 to 2.0 and used string
interpolation which calls to_s for you, also you don’t need and
array):
irb(main):010:0> value1 = 100.0
=> 100.0
irb(main):012:0> puts “The circle is of radius #{(value1/3.14)/2.0}
units”
The circle is of radius 15.9235668789809 units
Which number are you entering?
I’ve had this problem before. Another problem is a ‘odd number list for
hash’ error in this code. It happened 4 times in the code;
…ea ’ + {{[(value1/3.14)/2]*2} 3.14}.to_s + ’ square units’
Your usage of { and [ is kind of weird, I think you might be using it
to specify precedence as you do in mathematics, but in Ruby they are
used for very different things, namely creating hash and array
literals. For precedence you should use only parenthesis:
irb(main):013:0> ((((value1/3.14)/2)*2) 3.14)
=> 796.178343949044
(you probably also don’t need so many parens, anyway).
irb(main):018:0> (value1/3.14/2)*2 3.14
=> 796.178343949044
although if you think they make the expresion clearer go for them.
what should I do?
Another completely different suggestion after fixing the above: if you
have the same expression many times (more than 1) refactor it to a
method.
Jesus.