Problem solving quadratic equations

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
answers…

Is the problem with my math or with my code?

Also, how can I require the input to be a valid rational number?

Thanks

Code:

Name: quadratic-jb.rb

Date: January 2006

Author: Jason B.

Email: [email protected]

def sqr(a)
a * a
end

def quad_pos(a, b, c)
(-b + (Math.sqrt( sqr(b) - 4ac ))) / 2*a
end

def quad_neg(a, b, c)
(-b - (Math.sqrt( sqr(b) - 4ac ))) / 2*a
end

print "
Solving Quadratic Equations
---------------------------\n"

puts "Enter the value of a: "
a = gets.chomp.to_f

puts "Enter the value of b: "
b = gets.chomp.to_f

puts " Enter the value of c: "
c = gets.chomp.to_f

puts "The formula is " + a.to_s + "x^2 + " + b.to_s + "x + " + c.to_s
puts “and the values of x are:”
puts quad_pos(a, b, c).to_s + " and " + quad_neg(a, b, c).to_s

Jay B. 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
answers…

Is the problem with my math or with my code?

Seems to work for me. Remember, in ruby you can use ** for
exponentiation.

irb(main):001:0> a = 1
=> 1
irb(main):002:0> b = 2
=> 2
irb(main):003:0> c = 3
=> 3
irb(main):004:0> -b + ((b2 - 4ac)/(2*a))(1/2)
=> -1
irb(main):005:0> -b - ((b2 - 4ac)/(2*a))(1/2)
=> -3

I have to remember to KISS!

Thx!

Btw, is there a way to get the same ‘def’ to process both the positive
and negative ‘x’ values?

Jay B. wrote:

I have to remember to KISS!

Thx!

Btw, is there a way to get the same ‘def’ to process both the positive
and negative ‘x’ values?

Sure. I’d detemine both values, place them in an array and return the
array.

On 1/5/07, Jay B. [email protected] wrote:

Btw, is there a way to get the same ‘def’ to process both the positive
and negative ‘x’ values?

You could have it return an array, where the first element is the 1st
solution and the second element is the 2nd solution:

def quad(a,b,c)
[ -b + ((b2 - 4.0ac)/(2.0*a))(0.5), -b - ((b2 -
4.0ac)/(2.0*a
))
(0.5) ]
end

Alle 20:05, venerdì 5 gennaio 2007, Jay B. ha scritto:

I have to remember to KISS!

Thx!

Btw, is there a way to get the same ‘def’ to process both the positive
and negative ‘x’ values?

def solve(a,b,c,sign) #pass +1 or -1 as sign
-b +(sign)Math.sqrt(b**2-4ac) / 2a
end

Hi,

On 1/5/07, Jay B. [email protected] 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
answers…

Is the problem with my math or with my code?

I don’t know for which values you tested your code, but if the
discriminent (term which you apply sqrt to) equals 0, your equation
only has one solution. That’s why you get two identical values.
Btw: You should check for a negative discriminent (that happens if
your equation isn’t solvable at all)

Cheers,
Christian

Alle 16:58, venerdì 5 gennaio 2007, Jay B. ha scritto:

---------------------------\n"
puts "The formula is " + a.to_s + "x^2 + " + b.to_s + "x + " + c.to_s
puts “and the values of x are:”
puts quad_pos(a, b, c).to_s + " and " + quad_neg(a, b, c).to_s

Are you sure? I run your program with the values a=1, b=3 and c=1 and
got the
correct results. Are you sure you didn’t try with the coefficients of
the
square of a bynomial (for instance a=1, b=2, c=1)?

As for checking the input, you can use a regexp:

input=gets.chomp
if !input.match /^\d+(.\d*)?$/
puts “This is not a number”
exit
end

By the way, in ruby you can use operator ** to raise to a power, there’s
no
need to define the sqr method: use b**2.

There’s also a better method to print the result:

puts “The formula is #{a} x^2 + #{b} x + #{c}\nand the values of x are:
\n#{quad_pos(a,b,c)} and #{quad_neg(a,b,c)}”

Stefano

Hi –

On Sat, 6 Jan 2007, Bryan W. wrote:

[ -b + ((b2 - 4.0ac)/(2.0*a))(0.5), -b - ((b2 - 4.0ac)/(2.0*a
))
(0.5) ]
end

Or, if you don’t want to type stuff twice:

def quad(a,b,c)
[:+,:-].map {|sign| -b.send(sign, ((b2 - 4ac)(0.5))) / (a*2) }
end

:slight_smile:

David

On 05.01.2007 16:58, Jay B. 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
answers…

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

def quad(a, b, c)
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