Hey all,
I was writing an expression evaluator for my Java class...but I've run into a problem. Java's power function seems to be like, inaccurate. Also, if it matters, I use Eclipse as my compiler. For example, if I do:
double x = Math.pow(512, (double) 1/3);
x comes out as 7.999...9 (15 decimal places). It should come out to 8...512^(1/3)--cubed root of 512. The inaccuracy is tiny, but still...I find this to be kind of strange. Also, if i do:
double y = Math.pow(x,3);
I should ostensibly get 512 back...but I don't. I get a number that's close, but not exactly 512. Repeating this process will cause y to diverge from 512 very very very gradually. Anyway, I was wondering if there are any ways to get around this problem? I realize that it's not hard to round up...but you couldn't do that for something like, pow(5,(double)3/7) because...lord knows what the "real" value of that is.
I realize that it's not a huge deal, but still, it just kind of bothers me. I mean, I have worked briefly with cpp, and the pow() function in math.h returns 8 for pow(512,(double)1/3).
I also tried to use a combination of natural log and e^x to calculate a power...which was even worse than using pow() (in Java).
Are there any other methods I can use?
Also...Java & cpp's pow() functions do not allow you to do like, pow(-8,(double)1/3)...should be -2, but both cpp & Java give you NaN. Generally...for a^b, if a is <=0, then b has to be a whole number. Is there anyway to fix this except to use like an if statement to evaluate the expression as if it were positive and put the negative back in the end (given that the power is an odd one)?
Thanks all,
-Eric
I was writing an expression evaluator for my Java class...but I've run into a problem. Java's power function seems to be like, inaccurate. Also, if it matters, I use Eclipse as my compiler. For example, if I do:
double x = Math.pow(512, (double) 1/3);
x comes out as 7.999...9 (15 decimal places). It should come out to 8...512^(1/3)--cubed root of 512. The inaccuracy is tiny, but still...I find this to be kind of strange. Also, if i do:
double y = Math.pow(x,3);
I should ostensibly get 512 back...but I don't. I get a number that's close, but not exactly 512. Repeating this process will cause y to diverge from 512 very very very gradually. Anyway, I was wondering if there are any ways to get around this problem? I realize that it's not hard to round up...but you couldn't do that for something like, pow(5,(double)3/7) because...lord knows what the "real" value of that is.
I realize that it's not a huge deal, but still, it just kind of bothers me. I mean, I have worked briefly with cpp, and the pow() function in math.h returns 8 for pow(512,(double)1/3).
I also tried to use a combination of natural log and e^x to calculate a power...which was even worse than using pow() (in Java).
Are there any other methods I can use?
Also...Java & cpp's pow() functions do not allow you to do like, pow(-8,(double)1/3)...should be -2, but both cpp & Java give you NaN. Generally...for a^b, if a is <=0, then b has to be a whole number. Is there anyway to fix this except to use like an if statement to evaluate the expression as if it were positive and put the negative back in the end (given that the power is an odd one)?
Thanks all,
-Eric
