```On 05.01.2007 16:58, Jay Bornhoft wrote:
> I wrote a small program to solve for x using the quadratic formula but
> instead of receiving a + and - result I am getting two identical
>
> Is the problem with my math or with my code?
>
> Also, how can I require the input to be a valid rational number?

You could use Float(gets.chomp) instead of gets.chomp.to_f:

irb(main):001:0> Float("10.2")
=> 10.2
irb(main):002:0> Float("x")
ArgumentError: invalid value for Float(): "x"
from (irb):2:in `Float'
from (irb):2
from :0
irb(main):003:0> "x".to_f
=> 0.0

Other than that you'll notice soon when the method tries to calculate
with improper values.  Additional hint: you should probably use 2.0
instead of 2 and 4.0 instead of 4 in order to make sure everything works
ok even if integers are used as arguments.

Btw, you can also use Ruby's feature to return multiple values to
calculate both results in one go - you'll notice that there is only one
solution if the second result is nil:

def sqr(x) x * x end

part = Math.sqrt( sqr(b) - 4.0 * a * c )
[(-b + part) / 2.0 * a, (-b - part) / 2.0 * a].uniq
end

irb(main):014:0> x, y = quad 1,20,0
=> [0.0, -20.0]
irb(main):015:0> x
=> 0.0
irb(main):016:0> y
=> -20.0
irb(main):017:0> x, y = quad 1,20,1
=> [-0.0501256289338006, -19.9498743710662]
irb(main):018:0> x
=> -0.0501256289338006
irb(main):019:0> y
=> -19.9498743710662
irb(main):020:0> x, y = quad 0,20,1
=> [0.0]
irb(main):021:0> x
=> 0.0
irb(main):022:0> y
=> nil

Kind regards

robert

```