Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Shawn - 30 Oct 2006 20:02 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.