Problem solving quadratic equations

J

Jay Bornhoft

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 Bornhoft
# Email: (e-mail address removed)

def sqr(a)
a * a
end

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

def quad_neg(a, b, c)
(-b - (Math.sqrt( sqr(b) - 4*a*c ))) / 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
 
S

Stefano Crocco

Alle 16:58, venerd=C3=AC 5 gennaio 2007, Jay Bornhoft ha scritto:
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 Bornhoft
# Email: (e-mail address removed)

def sqr(a)
a * a
end

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

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

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

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

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

puts " Enter the value of c: "
c =3D 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

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

As for checking the input, you can use a regexp:
=20
input=3Dgets.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=
=20
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
 
R

Robert Klemme

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
 
D

Drew Olson

Jay said:
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 + ((b**2 - 4*a*c)/(2*a))**(1/2)
=> -1
irb(main):005:0> -b - ((b**2 - 4*a*c)/(2*a))**(1/2)
=> -3
 
J

Jay Bornhoft

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?
 
S

Stefano Crocco

Alle 20:05, venerd=C3=AC 5 gennaio 2007, Jay Bornhoft ha scritto:
I have to remember to KISS!
=20
Thx!
=20
=20
Btw, is there a way to get the same 'def' to process both the positive=20
and negative 'x' values?
=20

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

dblack

Hi --

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 + ((b**2 - 4.0*a*c)/(2.0*a))**(0.5), -b - ((b**2 - 4.0*a*c)/(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, ((b**2 - 4*a*c)**(0.5))) / (a*2) }
end

:)


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
D

Drew Olson

Jay said:
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.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top