1

This is the first code I've ever written. Here's a part of my code which is meant to calculate the side lengths of a cube and tetrahedron after the user gives a value for the volume, however The output is incorrect. I'm pretty sure I have the equations correct, maybe I'm using the Math.pow method incorrectly?

System.out.println("Now, please enter a volume for side or diameter calculation, then press ENTER: ");

volume = input.nextDouble();
cubeSide = Math.pow(volume, (1/3));
sphereDiameter = Math.pow(volume / PI * 6, (1/3));
tetSide = SQRT_2 * Math.pow(3 * volume, (1/3));

System.out.println("");
System.out.println("The Side of your CUBE is : " + cubeSide);
System.out.println("");
System.out.println("The Diameter of your SPHERE is : " + sphereDiameter);
System.out.println("");
System.out.println("The Side of your TETRAHEDRON is : " + tetSide);

Any ideas on how to get correct outputs?

jww
  • 97,681
  • 90
  • 411
  • 885
Gregory
  • 13
  • 1
  • 4

2 Answers2

4

1/3 is 0 - when both dividend and divisor are integral, / performs integral division. You want 1.0 / 3 or 1 / 3.0 or 1.0 / 3.0, which evaluate to 0.3333333-ish.

Amadan
  • 191,408
  • 23
  • 240
  • 301
2

Your question is an instance of this one Division of integers in Java

Bassically, you need to cast the 1/3 part of your Math.pow() to double, because if you don't do that for default it will take the result as an Integer (always 0).

For example:

    double volume = 15.34;
    double fraction = (double) 1/3;
    double cubeSide = Math.pow(volume,fraction);
    System.out.println(cubeSide);

Output is

2.4847066359757295

Otherwise output is always 1.0. Which is the result of any number rised to the zero.

As stated in your comment, when the input is:

1000

the output should be a whole:

10

But actually its:

9.999999999999998

The simplest solution to that could be just:

float roundedValue = Math.round(cubeSide);

And say: that's not my problem. But we want to understand what that's happening. As most things in this world, you are not the first one to face this problem. Let's do some research and find that there in StackOverFlow it have been asked:

In the first link, its suggested to read What Every Computer Scientist Should Know About Floating-Point Arithmetic.

I wont repeat what those wise people whom know a lot more than me said, so I highly recommend to you to read the above links.

Community
  • 1
  • 1
Yayotrón
  • 1,759
  • 16
  • 27
  • 2
    Minor nitpick: `(double) 1/3` casts `1` to `double`, not `1/3`. If it cast `1/3`, it would be too late - you'd end up with `(double) 0`, i.e. `0.0`. – Amadan Jul 07 '15 at 02:26
  • Exactly what Amadan said. Doing 1/(double) 3 or 1.0/3 or 1/3.0 would all output the same result. – Yayotrón Jul 07 '15 at 02:28
  • I understand most of what you're saying. I now have this format : cubeSide = Math.pow(volume, (1.0/3.0)); This works much better, but If I were to enter 1000 as a volume, it returns 9.999999... This should be an even 10. What am I missing? – Gregory Jul 07 '15 at 02:36
  • Oh! You have encountered a new problem and its kinda complicated to understand! I will edit my answer for add some links and questions related to that problem. – Yayotrón Jul 07 '15 at 02:41