# Forum: Ruby Problem solving quadratic equations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
on 2007-01-05 16:58
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?

Thanks

Code:
# 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 "
---------------------------\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
on 2007-01-05 19:41
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?

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
on 2007-01-05 20:05
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?
on 2007-01-05 22:05
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.
on 2007-01-06 19:30
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
on 2007-01-06 19:30
On 1/5/07, Jay Bornhoft <jbornhoft@gmail.com> 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:

[   -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
on 2007-01-06 19:30
Hi --

On Sat, 6 Jan 2007, Bryan Weatherly wrote:

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

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

:-)

David
on 2007-01-06 19:30
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
>
> 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
on 2007-01-06 19:30
Alle 16:58, venerdÃ¬ 5 gennaio 2007, Jay Bornhoft 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:

Stefano
on 2007-01-06 19:30
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

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
This topic is locked and can not be replied to.