Skip to main content

# add ^ operator for power of two

5 replies [Last post]
jsbean
Offline
Joined: 2005-02-15

I often use Math.pow((expression), n) or exp * exp for simpler expressions.
It would be nice to be able to write (expression)^n or exp^2 instead

ex: r = Math.sqrt((a2 - a1)^2 + (b2 - b1)^2)

So I think ^ should be added as a mathematical operator, along with +, -, *, / and %

The priority should be same as 'new' (below ++, above *)

## Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ulfzibis
Offline
Joined: 2005-02-18

Why not more simple:
[code]
String bar2 = "-" * 40;
[/code]

markf
Offline
Joined: 2005-01-20

As an addendum to this, I might note that an exponentiation operator could have non-numeric uses, such as String repetition.
[code]
String bar1 = "----------------------------------------";
String bar2 = "-" ** 40;
assert bar1.equals(bar2);
[/code]
This would fit in very well the mathematical notation used with strings and languages. The only potential source of confusion might be the behaviour of an exponentiation operator on characters.
[code]
char squareOf65 = 'A' ** 2;
assert squareOf65 == 4225;

String twoAs = 'A' ** 2;
assert twoAs == "AA";
[/code]
One of these assertions must fail. Consistency with the fact that char is a numeric type dictates that the former assertion succeed and the latter fail, but I doubt that's what people will expect. And then they'll show up on the Sun Java forums, furrowing their brows in a vain attempt to understand the situation.

Anyway, it's just an idea, and I'm throwing it out there for consideration.

jsbean
Offline
Joined: 2005-02-15

> As an addendum to this, I might note that an
> exponentiation operator could have non-numeric uses,
> such as String repetition.
> [code]
> String bar1 =
> "----------------------------------------";
> String bar2 = "-" ** 40;
> assert bar1.equals(bar2);
> [/code]

Shouldn't we use '*' for this though? Multiplication instead of exponent

> The only potential
> source of confusion might be the behaviour of an
> exponentiation operator on characters.
> [code]
> char squareOf65 = 'A' ** 2;
> assert squareOf65 == 4225;
>
> String twoAs = 'A' ** 2;
> assert twoAs == "AA";
> [/code]
> One of these assertions must fail.

I don't see why? Applying the operator to 'A' gives an int with value 4225 (or maybe the unicode character for 4225, or the char for 4225 % MAX_CHAR_CODE), while applying it to a string gives a different behaviour. Why would this lead to confusion?

Other possible String operators:
[code]
String Mippi = "Mississippi" - "ssi";
assert Mippi.equals("Mippi");

String[] frag = Mippi / 'p';
assert frag.equals(new String[] {"Mi", "", "i");
[/code]

and similar behaviour for +=, -=, *=, /=

markf
Offline
Joined: 2005-01-20

Unfortunately, ^ is already in use as a bit operator. If a new operator for raising to powers is to be added, why not go with the python choice of using **?
[code]
int mynum = 4**2;
assert 16 == mynum;

double rootTwo = 2.0 ** 0.5;
assert Math.sqrt(2.0) == rootTwo;
[/code]

ulfzibis
Offline
Joined: 2005-02-18

> Unfortunately, ^ is already in use as a bit operator.
> If a new operator for raising to powers is to be
> e added, why not go with the python choice of using
> **?

Also the + operator has two meanings (for numbers and strings).
Is there a problem I've overlooked ?