Why Java's math expression (power) is so inconvenient and error prone?

S

Shawn

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!
 
L

Lionel

Shawn said:
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!

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.
 
S

Shawn

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.
 
L

Lionel

Shawn said:
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.

So if there is an exp method then why were you using pow in the first
place. Please read the documentation in future.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

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
 
I

Ingo Menger

Lionel said:
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
?
 
E

EJP

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top