954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Issues using Math class

Hi,
Has anyone had the following problem?

When I run the following code (just using Notepad and my DOS prompt):

public class MathFunctionsApp2
{
public static void main(String[] args)
{

int b = -50;
double x = 25.0;
double y = 3.0;
double z = 4.0;


System.out.println("cbrt(x) = "+ Math.cbrt(x));

System.out.println("hypot(y,z) = "+ Math.hypot(y,z));

System.out.println("log10(y) = "+ Math.log10(y));

System.out.println("signum(b) = "+ Math.signum(b));

}

}

It won't compile - it doesn't like SOME of the mathematical functions provided by the Math Class. (See error note below)

MathFunctionsApp2.java:13: cannot resolve symbol
symbol : method cbrt (double)
location: class java.lang.Math
System.out.println("cbrt(x) = "+ Math.cbrt(x));
^
MathFunctionsApp2.java:15: cannot resolve symbol
symbol : method hypot (double,double)
location: class java.lang.Math
System.out.println("hypot(y,z) = "+ Math.hypot(y,z));
^
MathFunctionsApp2.java:17: cannot resolve symbol
symbol : method log10 (double)
location: class java.lang.Math
System.out.println("log10(y) = "+ Math.log10(y));
^
MathFunctionsApp2.java:19: cannot resolve symbol
symbol : method signum (int)
location: class java.lang.Math
System.out.println("signum(b) = "+ Math.signum(b));
^
4 errors


However, it will accept some of the other Mathematical functions, such as sqrt, max, exp, etc. (I just didn't include them here - but they do work!)

Oh, and I also CAN get it to run if I just use TextPad.

Would love any advice I can get!
Thanks.

rpratt
Newbie Poster
14 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

Well, none of those are functions in the math class.
You can find a list here:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html

I can help you with a few of those:
To get the cube root:

Math.pow(base,3);

To get log base 10 of X:

int answer = Math.log(x)/Math.log(10);

To get hypotenuse a little more work is needed:

Math.sqr((Math.pow(a,2)+Math.pow(b,2));

I think that will work, not sure.

I don't know what you want on your last thing though.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

Well, none of those are functions in the math class. You can find a list here: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html

I can help you with a few of those: To get the cube root:

Math.pow(base,3);

To get log base 10 of X:

int answer = Math.log(x)/Math.log(10);

To get hypotenuse a little more work is needed:

Math.sqr((Math.pow(a,2)+Math.pow(b,2));

I think that will work, not sure.

I don't know what you want on your last thing though.


But here's the weird thing - it DOES work with TextPad. Also - the way that I found these was from a "Java for Dummies" book. Doesn't it seem strange that it would work with TextPad?

Thanks for the website though - very useful!

PS The signum returns the sign of the argument. I have no idea why this is useful. Just was trying out what the book suggested...

rpratt
Newbie Poster
14 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

Aha!! I figured it out!!

The math functions I was looking at are new to Java's version 1.5.0_04!!

For the very latest list:

java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.html

And.... it turns out that my path was still pointing to the old version of Java (1.4 etc), NOT 1.5.0!

Hope someone can make good use of this!

rpratt
Newbie Poster
14 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

I see what it is. TextPad must come with extra classes that make things easier. A lot of times you will get things like this. I know normally you'll find an easier class to help out with reading and writing data.

To get the sine of a number in the normal methods:

Math.sin(x);


You won't be able to use those methods you have unless TextPad actually came with them, or you download them. I would suggest using the normal classes though.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

I think you're right about TextPad. But, also, check out my above comment about the new version of Java - I think that's why the book was recommending these functions.

Thanks again for all your help!

rpratt
Newbie Poster
14 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

Sorry, we must have posted at the same time because I did'nt see that. But that's cool, I never checked 1.5's new Math features. It's good they added a few of those like log10.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You