List Info

Thread: the sign of a number is omitted when squaring it. -2**2 vs (-2)**2




the sign of a number is omitted when squaring it. -2**2 vs (-2)**2
user name
2006-11-05 19:21:45
Yukihiro Matsumoto wrote:
> Hi,
> 
> In message "Re: [ ruby-Bugs-6468 ] the sign of a
number is omitted when squaring it. -2**2 vs (-2)**2"
>     on Sun, 5 Nov 2006 04:23:30 +0900, Joel VanderWerf
<vjoelpath.berkeley.edu> writes:
> 
> |Any yet
> |
> |irb(main):002:0> -2.abs
> |=> 2
> |
> |So there are cases where the operation of
"concatenating characters to 
> |form a literal" has higher priority than an
operation on objects.
> 
> People with mathematical background demands precedence
for ** being
> higher than that of unary minus.  That's the reason.

Precedence isn't the whole story:

irb(main):001:0> x=2
=> 2
irb(main):002:0> -x**2
=> -4
irb(main):003:0> -2**2
=> -4
irb(main):004:0> -x.abs
=> -2
irb(main):005:0> -2.abs
=> 2

Tokenization works differently in different contexts (as it
should). 
Mathematicians need to learn this, when they read line 005
above.

-- 
       vjoel : Joel VanderWerf : path berkeley edu : 510 665
3407

the sign of a number is omitted when squaring it. -2**2 vs (-2)**2
user name
2006-11-05 23:05:13
Joel VanderWerf wrote:
> Yukihiro Matsumoto wrote:
>> Hi,
>>
>> In message "Re: [ ruby-Bugs-6468 ] the sign of
a number is omitted
>> when squaring it. -2**2 vs (-2)**2"
>>     on Sun, 5 Nov 2006 04:23:30 +0900, Joel
VanderWerf
>> <vjoelpath.berkeley.edu> writes:
>>
>> |Any yet
>> |
>> |irb(main):002:0> -2.abs
>> |=> 2
>> |
>> |So there are cases where the operation of
"concatenating characters
>> to |form a literal" has higher priority than
an operation on objects.
>>
>> People with mathematical background demands
precedence for ** being
>> higher than that of unary minus.  That's the
reason.
> 
> Precedence isn't the whole story:
> 
> irb(main):001:0> x=2
> => 2
> irb(main):002:0> -x**2
> => -4
> irb(main):003:0> -2**2
> => -4
> irb(main):004:0> -x.abs
> => -2
> irb(main):005:0> -2.abs
> => 2
> 
> Tokenization works differently in different contexts
(as it should).
> Mathematicians need to learn this, when they read line
005 above.
> 

I think I want to weigh in here as a mathematician and
long-time
scientific programmer. My view is that is the *programmer's*
*responsibility* *alone* to code mathematical formulas in a
manner such
that they are unambiguous, both to the compiler or
interpreter, and to
the readers of the code. Therefore, the correct code is
either

(-x)**2

or

-(x**2)

depending on which meaning the programmer intended. And I
can't for the
life of me understand why anyone would code

(-x).abs

the sign of a number is omitted when squaring it. -2**2 vs (-2)**2
user name
2006-11-05 23:42:31
M. Edward (Ed) Borasky wrote:
> depending on which meaning the programmer intended. And
I can't for the
> life of me understand why anyone would code
> 
> (-x).abs

Ok,

irb(main):006:0> -2.5.ceil
=> -2
irb(main):007:0> -(2.5.ceil)
=> -3
irb(main):008:0> x = 2.5
=> 2.5
irb(main):009:0> -x.ceil
=> -3
irb(main):010:0> -(x.ceil)
=> -3

To a mathematician (me) the operators appear to take
different 
precedence depending on whether you have a literal or a
variable. What's 
really happening is not fully explained by precedence. The
lexical rules 
are important too and they are not simple.

I'll agree with you on the importance of explicit
bracketing, especially 
if you have to communicate with scientists or mathematicians
who are not 
programmers.

-- 
       vjoel : Joel VanderWerf : path berkeley edu : 510 665
3407

[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )