- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
exponential operator
I asked myself why Scala hasn't an exponential operator like '^' or '**'
as many other languages have.
I noticed that Scalas precedence rules will not work very well with such
operators because '^' has low precedence and '**' has the same
precedence than '*' and '/'.
Therefore I want to know if would be a good design decision to introduce
such an operator to the language (either to the libs as an implicit
conversion or as a built-in operator).
What yo you think? Do you appreciate such an operator or do you think
that there will be more problems than advantages?










Re: exponential operator
On Thu, Dec 22, 2011 at 08:10:50PM +0100, Antoras wrote:
> I asked myself why Scala hasn't an exponential operator like '^' or
> '**' as many other languages have.
>
> I noticed that Scalas precedence rules will not work very well with
> such operators because '^' has low precedence and '**' has the same
> precedence than '*' and '/'.
>
> Therefore I want to know if would be a good design decision to
> introduce such an operator to the language (either to the libs as an
> implicit conversion or as a built-in operator).
>
> What yo you think? Do you appreciate such an operator or do you
> think that there will be more problems than advantages?
The following ASCII characters have higher precedence than *:
# (# is unavailable, ## is used for hashing)
@ (@ is unavailable)
? (unused afaik)
\ (\ is used with xml and maybe other places?)
~ (unused afaik)
Any of these can be used to build an exponentiation operator with the
right precedence. You can test this in the REPL:
implicit def xyz(d:Double) = new { def ~**(e:Double) = math.pow(d, e) }
2.0 * 3.0 ~** 4.0 * 5.0
This will return 810.0 like you'd expect (incorrect operator precedence
might result in 6480.0, 6.973568802E9 or 3.656158440062976E15).
I really miss this operator from other language and wish Scala included
it. Besides the fact that I resent having to type math.pow, I
especially hate using it with Int (the most common case in my opinion)
as I have to convert back to Int from Double. Compare:
math.pow(3, 5).toInt // scala
with
3 ** 5 # python
Python is just plain better than us here. The Scala example contains so
much noise, I have a hard time visually parsing out what is actually
happening. At a minimum I'd like to see math.pow provide interfaces for
Int and Long. Ideally we'd see the introduction of an exponentiation
operator.
While it's not mentioned specifically in the proposal Tom Switzer and I
are working on, I really feel that Scala could be improved by going
beyond the "Java bare minimum" of numeric operators and types. There's
no reason we can't do as well as Python (especially since our primitive
types have better performance).
Re: exponential operator
On Thursday, December 22, 2011 at 21:03, Erik Osheim wrote:
> On Thu, Dec 22, 2011 at 08:10:50PM +0100, Antoras wrote:
> > I asked myself why Scala hasn't an exponential operator like '^' or
> > '**' as many other languages have.
> >
> > I noticed that Scalas precedence rules will not work very well with
> > such operators because '^' has low precedence and '**' has the same
> > precedence than '*' and '/'.
> >
> > Therefore I want to know if would be a good design decision to
> > introduce such an operator to the language (either to the libs as an
> > implicit conversion or as a built-in operator).
> >
> > What yo you think? Do you appreciate such an operator or do you
> > think that there will be more problems than advantages?
>
>
>
> The following ASCII characters have higher precedence than *:
>
> # (# is unavailable, ## is used for hashing)
> @ (@ is unavailable)
> ? (unused afaik)
> \ (\ is used with xml and maybe other places?)
> ~ (unused afaik)
>
> Any of these can be used to build an exponentiation operator with the
> right precedence. You can test this in the REPL:
>
> implicit def xyz(d:Double) = new { def ~**(e:Double) = math.pow(d, e) }
> 2.0 * 3.0 ~** 4.0 * 5.0
>
> This will return 810.0 like you'd expect (incorrect operator precedence
> might result in 6480.0, 6.973568802E9 or 3.656158440062976E15).
>
> I really miss this operator from other language and wish Scala included
> it. Besides the fact that I resent having to type math.pow, I
> especially hate using it with Int (the most common case in my opinion)
> as I have to convert back to Int from Double. Compare:
>
> math.pow(3, 5).toInt // scala
>
> with
>
> 3 ** 5 # python
>
> Python is just plain better than us here. The Scala example contains so
> much noise, I have a hard time visually parsing out what is actually
> happening. At a minimum I'd like to see math.pow provide interfaces for
> Int and Long. Ideally we'd see the introduction of an exponentiation
> operator.
>
> While it's not mentioned specifically in the proposal Tom Switzer and I
> are working on, I really feel that Scala could be improved by going
> beyond the "Java bare minimum" of numeric operators and types. There's
> no reason we can't do as well as Python (especially since our primitive
> types have better performance).
>
Re: exponential operator
On Thu, Dec 22, 2011 at 09:21:12PM +0100, Nate Nystrom wrote:
> I'd much rather the operator be just 'pow' than something like ~**.
> It could easily be made an infix operator like max and min and be made
> to work on Int and Long. Annoyingly though, it's left associative
> rather than right, like in python, and defining an operator named pow_:
> kind of defeats the purpose.
The left associativity is a bummer but given the "colon requirement"
for right-associativity it's maybe more trouble than it's worth.
A certain (sick?) part of me could imagine using ~**: or ~^: to get the
right precedence and the right associativity. But it certainly won't
help fight the perception that Scala has too much symbolic operator
soup. [1]
I agree that an Int-aware pow (with wrong precedence/associativity) is
better than what we have now, although I personally think a symbolic
operator (with correct precedence but wrong associativity) is even
better.
Re: exponential operator
I share your discomfort with the spec on precedence and fixity, associativity etc., and have a journeyman's understanding of why they're not customizable now and would be extremely difficult to make customizable in the future, and I like both of these ideas. -0xe1a
Re: exponential operator
def sqr(x: Int) = x * x def cube(x: Int) = x * x * x
def sqr(x: Double) = x * x def cube(x: Double) = x * x * x
In the rare case when you need an exponent other than two or three, just use the "pow" function.
I think the "sqr" and "cube" functions should be added to the standard math package for convenience.
--Russ P.
On Thu, Dec 22, 2011 at 11:10 AM, Antoras <mail [at] antoras [dot] de> wrote:
--
http://RussP.us
RE: exponential operator
Date: Thu, 22 Dec 2011 11:35:37 -0800
Subject: Re: [scala-debate] exponential operator
From: russ [dot] paielli [at] gmail [dot] com
I realized that a general exponentiation operator is not very important. In practice, the exponent is nearly always two (squaring) or, less often, three (cubing)