Feature #7322
closedAdd a new operator name #>< for bit-wise "exclusive or"
Description
=begin
I propose to alias (({Fixnum#^})) and (({Bignum#^})), which stand for bit-wise "exclusive or", with a new operator name (({#><})).
Is it necessary to go along with C, Python, and Mathematica?
According to Wikipedia ( http://en.wikipedia.org/wiki/Exclusive_or#Alternative_symbols ), the "^" symbol is not used for XOR outside of programming context.
Here are some examples of mathematical notation: http://mathworld.wolfram.com/XOR.html (there are also examples in the Wikipedia article).
The Unicode symbol is "⊻".
If eventually (({Fixnum#^})) as XOR is deprecated, this will liberate this symbol for other uses, for example for all kinds of exponential notations (for me, "a^n" usually means "the n-th power of a").
=end
Updated by nathan.f77 (Nathan Broadbent) about 12 years ago
"a^n" usually means "the n-th power of a"
I agree, I think using "^" for exponents would be more consistent with
other languages. I don't have a strong opinion on a replacement for
'exclusive or', but some other approximations of "⊻" could be: "|<" or
"/<".
Updated by alexeymuranov (Alexey Muranov) about 12 years ago
Related: #6678.
Updated by trans (Thomas Sawyer) about 12 years ago
Given the rejection of #7336, I would ask this this issue be more seriously considered. Personally, I don't really care if #^ continues to mean bitwise xor for numerics, but at least raise its precedence to be the same as #**, and provide an alternative xor operator with the present precedence. Whether #><
is a good choice or not, I am mostly indifferent. (I suppose alternatives might be #~
or #!
which are unary operators, but have no binary definition.)
Updated by matz (Yukihiro Matsumoto) about 12 years ago
- Status changed from Open to Rejected
So what's your intention? To make '^' a power operator, and ruin innocent programs? Because you feel "natural"?
If Ruby were young and there were no (or few) Ruby programs out there, your proposal might be useful.
But I don't want to break compatibility for fashion.
"When in Rome, do as the Romans do".
Matz.
Updated by david_macmahon (David MacMahon) about 12 years ago
I fully agree that repurposing '^' to be a power operator is not a good idea.
One alternative idea that might not make anyone happy would be to create a new power operator "^^" that is the same as "**", but seems more like exponentiation to those too young to know (or too old to remember :-)) Fortran.
Please feel free to ignore this suggestion,
Dave
Updated by trans (Thomas Sawyer) about 12 years ago
@matz (Yukihiro Matsumoto) Have you ever read about issue I have with implementing Stick b/c of this? I have no Rome to be in. I am in Carthage!
@dvaid_macmahon Well, #^^ would be better than nothing.
Updated by matz (Yukihiro Matsumoto) about 12 years ago
No. There are so many "issues" out there. I don't have time to check them all.
Reference please.
Matz.
Updated by alexeymuranov (Alexey Muranov) about 12 years ago
Speaking of Fortran, there the binary *
has higher precedence than the unary -
, which has approximately the same as the binary one. (This is not about this issue, but about #7328 and #7331.)
I would say it is unfortunate that the main reason to reject is that Ruby is too old to change. (The changes would not need to be immediate, i think that aliases can be introduced or deprecated between major versions.) But i am not a heavy Ruby user, i was only proposing my ideas hoping that they could be useful for others too.
Updated by trans (Thomas Sawyer) about 12 years ago
=begin
@matz (Yukihiro Matsumoto)
Reference given below[1], but I can give very brief summary to save you time...
Stick is an SI units system for Ruby. While in pure math there is just one type of "power" (in Ruby provided by (({#**}))), in physical systems there is also units and units themselves can have "power", e.g. cubic meters. So any good units system therefore ((needs two power operators)), one for the value and one for the unit. The natural notation for these are, e.g.
10.m**3 (10 meters * 10 meters * 10 meters)
10.m^3 (10 cubic meters)
But precedence of (({#^})) is too low in Ruby to do this well, e.g.
10.m^2/2 => 10 meters, instead of 5.m^2
Nor is there some alternative operator to utilize.
Unit powers is a common real word application, so I think it is not unreasonable to seek that Ruby be able to accommodate such a use case.
In short, for me to create the best SI units system I can for Ruby, I need another power operator.
[1] https://github.com/trans/trans.github.com/wiki/2009-12-13-a-failure-of-precedence
=end
Updated by stomar (Marcus Stollsteimer) about 12 years ago
There is only one type of power operator in physics.
You would have to use parentheses:
(10 m)^3 (10 meters * 10 meters * 10 meters)
10 m^3 (10 cubic meters)
And I think you should also have to use them in the code.
From a physicist's point of view, I find the idea of distinguishing between different kinds of power operators strange at best, probably confusing to the user and maybe even dangerous.
Updated by alexeymuranov (Alexey Muranov) about 12 years ago
Thomas, i agree with @stomar (Marcus Stollsteimer).
Updated by trans (Thomas Sawyer) about 12 years ago
=begin
@stomer
Yes and no. The problem is in the Ruby notation. In physics, you can use parenthesis to make the distinction, but you can't in Ruby. e.g. Your example doesn't work in Ruby:
10.m**3
and
(10.m)**3
Can only produce the same result.
=end
Updated by stomar (Marcus Stollsteimer) about 12 years ago
=begin
@trans (Thomas Sawyer)
Imo the point is that using a method for the unit does not correctly model the mathematical relation (and hence the precedence) between the numerical value of the physical quantity and its unit, which simply is a multiplication.
Just a thought: how about using an argument for the power of the unit, like in
10.m**3 # (10 m)^3
10.m(3) # 10 m^3
But this is taking a very off-topic direction...
=end
Updated by claytrump (Clay Trump) about 12 years ago
Sorry, I didn't read the whole thread, but why not use the right operators?
g = 9.81 * m / s ** 2
--
Updated by trans (Thomas Sawyer) about 12 years ago
=begin
It might seem like it, but designating a unit is not multiplication.
(({9.81 * m})) actually means (({9.81 * 1 m}))
Implementation wise this approach is a bit messier too. It requires overriding the built-in #* method and adding a slew of type methods to Object (e.g. Object#m).
@stomer Using an argument for the unit power isn't a skeuomorphic ideal. But it might suffice. I need to give it some thought and take a look at the Stick code to make sure. I'll try to do that today and report back. Thanks.
Speaking of a skeuomorphic ideal, do you notice that in general contexts when people give ascii examples of "power" they always use (({^}))? I don't really understand why language designers are so bent on bucking the overwhelming commonality of this choice --all solely to cling to C's tradition of using (({^})) as XOR? How unfortunate. But anyway, I digress.
=end
Updated by trans (Thomas Sawyer) about 12 years ago
Hmm... "skeuomorphic" probably isn't the right word actually. I really meant something more like "reflecting reality". In this case, specifically, "best reflecting common mathematical notation".
Updated by alexeymuranov (Alexey Muranov) about 12 years ago
Thomas, let some physicist correct me, but i thought that designating units was multiplication: 9.81 m/s^2 = (9.81 * m)/(s^2). This should however be discussed in a different thread. :)