out of range problems and odd number hashs

C

Charlie Ca

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'

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'
^

...{{[(value1/3.14)/2]**2}* 3.14}.to_s + ' square units'
^

...{[math.sqrt(value3/3.14)] * 2}.to_s + ' units, and circumfer...
^

...{[math.sqrt(value3/3.14)] * 2} * 3.14).to_s + ' units.'
^


what should I do?
 
J

Jesús Gabriel y Galán

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 =3D gets.chomp.to_f
=A0puts '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 =3D 100.0
=3D> 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)
=3D> 796.178343949044

(you probably also don't need so many parens, anyway).

irb(main):018:0> (value1/3.14/2)**2* 3.14
=3D> 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.
 
P

Peter Hickman

[Note: parts of this message were removed to make it a legal post.]

Can we see more context for this code. For the life of me I can't see why
you have and '{' or '}'s in you code at that point.
 
P

Peter Hickman

[Note: parts of this message were removed to make it a legal post.]

Additionally

puts 'The circle is of radius ' + [(value1/3.14)/2].to_s + 'units'

Can be rewritten as

puts "The circle is of radius #{(value1/3.14)/2} units"

Converting things to a list just to be able to convert them to a string is
'code smell'. If you are calling the to_X methods all over the place you are
probably doing something wrong. Image someone pointing at you code and
saying "why did you do that?". If your answer is something like "dunno, it
works" then you have a problem.

Ruby code tends to be simpler and shorter than other languages. You will
often find yourself saying "what! is that it?"
 
R

Robert Klemme

2010/3/25 Peter Hickman said:
Additionally

puts 'The circle is of radius ' + [(value1/3.14)/2].to_s + 'units'

Btw, no need for "3.14" - there is

irb(main):001:0> Math::pI
=> 3.14159265358979

Kind regards

robert
 

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
474,000
Messages
2,570,252
Members
46,848
Latest member
CristineKo

Latest Threads

Top