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 > Ruby math parenthesis wierdness
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Ruby math parenthesis wierdness

Réponse
 
LinkBack Outils de la discussion
Vieux 25/05/2008, 20h46   #1
Tom Verbeure
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Ruby math parenthesis wierdness

All,

The code below:

m = 5*(
(1.0)
- (1.0/2.0)
)
puts m
m = 5*(
(1.0) - (1.0/2.0)
)
puts m

prints out:

bash-3.2$ ./run.rb
-2.5
2.5

The second result is what I expect, but I don't understand the first
result.

I usually put operands at the end of the line instead of the
beginning, so I never ran into this before, but this was originally
Python code that I converted into ruby.

Any insights?

Thanks,
Tom
  Réponse avec citation
Vieux 25/05/2008, 21h27   #2
Phlip
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby math parenthesis wierdness

> m = 5*(
> (1.0)
> - (1.0/2.0)
> )


That's the same as 5*( 1.0 ; -(1.0/2.0) )

The linefeed, without a trailing operator before it, upgrades to a
statement break. And (a;b) returns b.

You just need this:

m = 5*(
(1.0) -
(1.0/2.0)
)
  Réponse avec citation
Vieux 25/05/2008, 21h38   #3
Gregg Kang
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby math parenthesis wierdness

I'm getting this type of issue too. It looks like any number you put
after the first paren '(' is ignored.

All these examples produce -20

m = 10*(123
-2)
puts m

----

m = 10*(10000000
-2)
puts m

---
m = 10*(-10000000
-2)
puts m

  Réponse avec citation
Vieux 25/05/2008, 21h41   #4
Mikael Høilund
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby math parenthesis wierdness


On May 25, 2008, at 20:49, Tom Verbeure wrote:

> All,
>
> The code below:
>
> m =3D 5*(
> (1.0)


This is the cause of the unexpected behavior. =46rom what I can see, =20
Ruby is interpreting the newline after (1.0) as a statement separator. =20=

Compare:

puts 5*(
(1.0);
- (1.0/2.0)
)

What's actually happening is that the third line (- (1.0/2.0)) is seen =20=

as a separate statement, not as a continuation on the second line. The =20=

minus sign in the beginning of the line is interpreted as the unary =20
negation operator, not the binary (as in relating to two numbers, not =20=

one) subtraction operator.

This works, however:

puts 5*(
(1.0) -
(1.0/2.0)
)

A quick irb session also illustrates my point:

>> 1.0

=3D> 1.0
>> -1.0/2.0

=3D> -0.5
>>
>> 1.0 -

?> 1.0/2.0

The fact that the last prompt is "?>", not ">>" is the key point here.

Hope this s
Mikael H=F8ilund



  Réponse avec citation
Vieux 25/05/2008, 21h41   #5
Gregg Kang
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby math parenthesis wierdness

I see. Thanks Phlip!
  Réponse avec citation
Vieux 25/05/2008, 21h45   #6
Tom Verbeure
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby math parenthesis wierdness


> Hope this s


Yes, definitely.

Thanks All!
Tom

  Réponse avec citation
Vieux 25/05/2008, 23h51   #7
Dave Bass
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby math parenthesis weirdness

If you make the line continuations explicit by adding backslashes, Ruby
gives the correct answer:

irb(main):005:0> m = 5*( \
irb(main):006:1* (1.0) \
irb(main):007:1* - (1.0/2.0) \
irb(main):008:1* )
=> 2.5

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

  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 18h18.


Édité par : vBulletin® version 3.7.4
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,11643 seconds with 15 queries