[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Problem solving quadratic equations

Jason Bornhoft

1/5/2007 3:58:00 PM

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: jbornhoft@gmail.com

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

--
Posted via http://www.ruby-....

8 Answers

Christian Pohlmann

1/5/2007 4:13:00 PM

0

Hi,

On 1/5/07, Jay Bornhoft <jbornhoft@gmail.com> 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

Stefano Crocco

1/5/2007 4:18:00 PM

0

Alle 16:58, venerdì 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: jbornhoft@gmail.com
>
> 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

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

Robert Klemme

1/5/2007 4:24:00 PM

0

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

Drew Olson

1/5/2007 6:41:00 PM

0

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

--
Posted via http://www.ruby-....

Jason Bornhoft

1/5/2007 7:06:00 PM

0

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?

--
Posted via http://www.ruby-....

Stefano Crocco

1/5/2007 7:16:00 PM

0

Alle 20:05, venerdì 5 gennaio 2007, Jay Bornhoft 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-4*a*c) / 2*a
end


dblack

1/5/2007 8:56:00 PM

0

Drew Olson

1/5/2007 9:05:00 PM

0

Jay Bornhoft 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.

--
Posted via http://www.ruby-....