PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Newbie Question
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Newbie Question

Réponse
 
LinkBack Outils de la discussion
Vieux 25/02/2008, 14h18   #1
Christian Rubio
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Newbie Question

I am completely new to ruby and to OO programming. Why-does my code
not compile??

class Quadratic

def findroots()
b = (ARGV[0])
c = (ARGV[1])
$stdout.print("You entered " , ARGV[1], ARGV[0], "\n")
discriminant = b * b - 4.0 * c
sqroot = Math.sqrt(discriminant)

root1 = (-b + sqroot) / 2.0
root2 = (-b - sqroot) / 2.0

$stdout.print(root1)
$stdout.print(root2)
end

end

if __FILE__ == $0
qf = Quadratic.new
qf.findroots
end


  Réponse avec citation
Vieux 25/02/2008, 14h33   #2
Jano Svitok
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie Question

On Mon, Feb 25, 2008 at 3:18 PM, Christian Rubio
<chris.rubio.osv@fedex.com> wrote:
> I am completely new to ruby and to OO programming. Why-does my code
> not compile??
>
> class Quadratic
>
> def findroots()
> b = (ARGV[0])
> c = (ARGV[1])
> $stdout.print("You entered " , ARGV[1], ARGV[0], "\n")
> discriminant = b * b - 4.0 * c
> sqroot = Math.sqrt(discriminant)
>
> root1 = (-b + sqroot) / 2.0
> root2 = (-b - sqroot) / 2.0
>
> $stdout.print(root1)
> $stdout.print(root2)
> end
>
> end
>
> if __FILE__ == $0
> qf = Quadratic.new
> qf.findroots
> end


Hi,

First, please, next time post the exact error message. You'll save
people that want to you some investigation.

If the folowing message is similar to your error:
W:/var/x.rb:7:in `*': can't convert String into Integer (TypeError)
from W:/var/x.rb:7:in `findroots'
from W:/var/x.rb:21

then you're trying to multiply numbers by strings.
Replace
> b = (ARGV[0])
> c = (ARGV[1])

by
b = ARGV[0].to_f
c = ARGV[1].to_f
to_f will convert string to float. For more info, read the docs of
String#to_f and possibly the whole String class
(e.g. using RI or http://ruby-doc.org/core/)

If the message is different, please post it and will see.

Jano

  Réponse avec citation
Vieux 25/02/2008, 14h53   #3
Christian Rubio
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie Question

Jano Svitok wrote:
> On Mon, Feb 25, 2008 at 3:18 PM, Christian Rubio
> <chris.rubio.osv@fedex.com> wrote:
>
>> I am completely new to ruby and to OO programming. Why-does my code
>> not compile??
>>
>>

> Replace
>
>> b = (ARGV[0])
>> c = (ARGV[1])
>>

> by
> b = ARGV[0].to_f
> c = ARGV[1].to_f
> to_f will convert string to float. For more info, read the docs of
> String#to_f and possibly the whole String class
> (e.g. using RI or http://ruby-doc.org/core/)
>
> If the message is different, please post it and will see.
>
> Jano
>
>

Hi Thanks for your and directions. I made the above substitutions
and now I get the following error

% ruby quadratic.rb 1.0 1.0
You entered 1.0 1.0
quadratic.rb:17:in `sqrt': Numerical argument out of domain - sqrt
(Errno::EDOM)
from quadratic.rb:17:in `findroots'
from quadratic.rb:30
Here is my program,

#!/usr/bin/env ruby

###########################
##
## program that solve quadratic equation
##
###########################

class Quadratic

def findroots()
b = (ARGV[0].to_f)
c = (ARGV[1].to_f)
$stdout.print("You entered " , ARGV[1]," ", ARGV[0], "\n")
discriminant = b * b - 4.0 * c
sqroot = Math.sqrt(discriminant)

root1 = (-b + sqroot) / 2.0
root2 = (-b - sqroot) / 2.0

$stdout.print(root1)
$stdout.print(root2)
end

end

if __FILE__ == $0
qf = Quadratic.new
qf.findroots
end



  Réponse avec citation
Vieux 25/02/2008, 15h01   #4
Jano Svitok
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie Question

On Mon, Feb 25, 2008 at 3:53 PM, Christian Rubio
<chris.rubio.osv@fedex.com> wrote:
> Jano Svitok wrote:
> > On Mon, Feb 25, 2008 at 3:18 PM, Christian Rubio
> > <chris.rubio.osv@fedex.com> wrote:
> >
> >> I am completely new to ruby and to OO programming. Why-does my code
> >> not compile??
> >>
> >>

>
> > Replace
> >
> >> b = (ARGV[0])
> >> c = (ARGV[1])
> >>

> > by
> > b = ARGV[0].to_f
> > c = ARGV[1].to_f
> > to_f will convert string to float. For more info, read the docs of
> > String#to_f and possibly the whole String class
> > (e.g. using RI or http://ruby-doc.org/core/)
> >
> > If the message is different, please post it and will see.
> >
> > Jano
> >
> >

> Hi Thanks for your and directions. I made the above substitutions
> and now I get the following error
>
> % ruby quadratic.rb 1.0 1.0
> You entered 1.0 1.0
> quadratic.rb:17:in `sqrt': Numerical argument out of domain - sqrt
> (Errno::EDOM)
> from quadratic.rb:17:in `findroots'
> from quadratic.rb:30


That means you passed a number that has no real square root, for example -1.
Refresh your high school math and check the discriminant before you
call Math.sqrt ;-)
Otherwise, require 'complex' ;-)

Jano

  Réponse avec citation
Vieux 25/02/2008, 15h02   #5
Sebastian Hungerecker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie Question

Christian Rubio wrote:
> quadratic.rb:17:in `sqrt': Numerical argument out of domain - sqrt
> (Errno::EDOM)
> =A0 =A0 =A0 =A0 from quadratic.rb:17:in `findroots'
> =A0 =A0 =A0 =A0 from quadratic.rb:30


This means that your argument to sqrt is negative.
What values for b and c did you supply?


HTH,
Sebastian
=2D-=20
NP: Anathema - Cries On The Wind
Jabber: sepp2k@jabber.org
ICQ: 205544826

  Réponse avec citation
Vieux 25/02/2008, 15h15   #6
Christian Rubio
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie Question

Sebastian Hungerecker wrote:
> Christian Rubio wrote:
>
>> quadratic.rb:17:in `sqrt': Numerical argument out of domain - sqrt
>> (Errno::EDOM)
>> from quadratic.rb:17:in `findroots'
>> from quadratic.rb:30
>>

>
> This means that your argument to sqrt is negative.
> What values for b and c did you supply?
>
>
> HTH,
> Sebastian
>

Thanks all for your replies. The input was invalid. I need to check for
legal input.
But I am new and just learning. Still trying to figure out whats going
on here.
The docs are hard to understand. So I am programming very basically.

Thanks so much
Chris

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h02.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13070 seconds with 14 queries