PDA

View Full Version : Define my own mathematical Operator?



Dwood15
April 21st, 2009, 02:32 AM
How do I create/define my own mathematical operator in java?

I want to make it so that the common ^ symbol can be used for what it's made for- Exponentiation.

Ideas?

Sinkingships7
April 21st, 2009, 03:01 AM
This is an un-researched reply, but you should read up on operator overloading.

EDIT: The post below mine is better advice.

Jeremy Jackins
April 21st, 2009, 03:01 AM
Just use

a = Math.exp(b);

it's not that much extra work, and it makes your code understandable to others.

Habbit
April 21st, 2009, 10:44 AM
There is no operator overloading / replacing in Java, limited support for in in C# and a much wider one in C++. However, you have to take into account that, even if you overload ^ in C++, the syntax you might end up adopting might not be that of "natural" (i.e. maths textbook) exponentiation, because the ^ operator has a defined precedende wrt other operators, and this cannot be changed. Thus, using an "exp" method may be the most natural choice indeed.

Dwood15
April 21st, 2009, 01:06 PM
Just use

a = Math.exp(b);

it's not that much extra work, and it makes your code understandable to others.

I know how to use that but this was just something I wanted to know.

@ Habbit- Thanks, I don't know much about C++ or C# I'm learning primarily Java atm.