Hi,
My program need a lot of calculation of power. In many programming
languages,
2**3 = 8;
The syntax is clean and easy. But in Java,
Math.pow(2, 3) = 8; //It is so long, and complicated and error prone
Again, in many languages,
EXP(1) = 2.7 //e value
But in Java,
Math.pow(Math.E, 1) = 2.7 //You see, so complicated
Normally, in one calculation, 2**3 or EXP(3.5) is only part of
expression, like "a + b**3 + EXP(-a)". In Java, it will be very long and
error prone!
Lionel - 30 Oct 2006 22:26 GMT
> Hi,
>
[quoted text clipped - 18 lines]
> expression, like "a + b**3 + EXP(-a)". In Java, it will be very long and
> error prone!
Error prone? All the above answers look correct. Can you tell us how it
is error prone?
It looks like you have come from C and are expecting non-object-oriented
code. Simple, get over it.
If it is so inconvenient then why don't you write a wrapper around the
Math class to make things a little easier. For example:
public class MathFunctions {
public static double exp(double someVal) {
return Math.pow(Math.E, someVal);
}
}
Now all you have to do is call MathFunctions.exp(1);
You can shorten the class name if you want.
If that is still too difficult I suggestion you learn about the
advantages of an Object-Oriented programming language.
Lionel.
Arne Vajhøj - 31 Oct 2006 02:46 GMT
>> My program need a lot of calculation of power. In many programming
>> languages,
[quoted text clipped - 12 lines]
>>
>> Math.pow(Math.E, 1) = 2.7 //You see, so complicated
> Error prone? All the above answers look correct. Can you tell us how it
> is error prone?
I agree with that.
> It looks like you have come from C and are expecting non-object-oriented
> code. Simple, get over it.
Actually C is very similar to Java. C does not have an
exponentation operator like Fortran and VB.
> If that is still too difficult I suggestion you learn about the
> advantages of an Object-Oriented programming language.
Hm.
I would not consider the Math static methods so fantastic
object oriented ...
Arne
Ingo Menger - 31 Oct 2006 13:18 GMT
> If that is still too difficult I suggestion you learn about the
> advantages of an Object-Oriented programming language.
One of them is to have no operator overloading? Come on. This has
nothing to do with OO or not OO.
BTW, do you write
new StringBuffer().append("foo").append(i).toString()
instead of
"foo" + i
?
Shawn - 30 Oct 2006 22:49 GMT
I appreciate Mark Thornton's reply:
import static java.lang.Math.*;
Then pow(2,3) or exp(1) is ready for service. Obviously, I didn't know
this trick.
In comparison, a wrapper class is a waste at all.
Lionel - 30 Oct 2006 22:53 GMT
> I appreciate Mark Thornton's reply:
>
[quoted text clipped - 4 lines]
>
> In comparison, a wrapper class is a waste at all.
So if there is an exp method then why were you using pow in the first
place. Please read the documentation in future.
EJP - 02 Nov 2006 01:09 GMT
I must admit that I prefer exponentiation as an operator rather than a
function call. It suits the mathematicians better, of whom I was one,
and it also guarantees the correct right-associativity of the operator,
which is otherwise up to the programmer in function-call land.