It says "loss of precision" or something along those lines when I try to compile it, and the errors occur at each "switch" line.
Please help.
package Java; import java.util.*; public class Geometry { public static void main (String[]args){ Scanner scnr = new Scanner(System.in); System.out.println("Which shape are you dealing with?"); System.out.println("1 for rectangular prism, 2 for cube, 3 for sphere."); double choice = 0; double choice2 = 0; double edge; double radius; double length; double height; double width; double sa; double volume; choice = scnr.nextDouble(); switch (choice){ case 1: System.out.println("Okay, now type in what you want to calculate: Surface area or volume."); System.out.println("1 for surface area, 2 for volume."); choice2=scnr.nextDouble(); switch (choice2){ case 1: System.out.println("Type in the length."); length = scnr.nextDouble(); System.out.println("Type in the width."); width = scnr.nextDouble(); System.out.println("Type in the height."); height = scnr.nextDouble(); System.out.println("The surface of your rect. prism is "+ 2*(length*width*height)); break; case 2: System.out.println("Type in the length."); length = scnr.nextDouble(); System.out.println("Type in the width."); width = scnr.nextDouble(); System.out.println("Type in the height."); height = scnr.nextDouble(); System.out.println("The volume of your rect. prism is " + length*width*height); break; } break; case 2: System.out.println("Okay, now type in what you want to calculate: Surface area or volume."); System.out.println("1 for surface area, 2 for volume."); choice2=scnr.nextDouble(); switch (choice2){ case 1: System.out.println("Type in one edge length."); edge = scnr.nextDouble(); System.out.println("The surface area of your cube is "+ 6*edge*edge); break; case 2: System.out.println("Type in one edge length."); edge = scnr.nextDouble(); System.out.println("The volume of your cube is "+ Math.pow(edge,3)); break; } break; case 3: System.out.println("Okay, now type in what you want to calculate: Surface area or volume."); System.out.println("1 for surface area, 2 for volume."); choice2=scnr.nextDouble(); switch(choice2){ case 1: System.out.println("Type in the radius."); radius = scnr.nextDouble(); System.out.println("The surface area of your sphere is "+ radius*radius*Math.PI*4); break; case 2: System.out.println("Type in the radius."); radius = scnr.nextDouble(); System.out.println("The volume of your sphere is "+ radius*radius*radius*Math.PI*4/3); break; } break; } } }
Offline
I would assume that that means switch can't take a double, perhaps?
Offline
veggieman001 wrote:
I would assume that that means switch can't take a double, perhaps?
yeah, try making the doubles all ints
It shouldn't be the package name, but I'd advise against using "Java". You may not be able to call any classes in it due to java.* being final, meaning they overrule any packages you make.
Last edited by SJRCS_011 (2012-05-22 21:38:05)
Offline
Usually loss of precision errors occur when you try to store a value in a variable that's less precise (can store less data), such as going from a double to a float. Something unfortunate about Java is that if you give it a plain number in the code, it'll use a less precise type for that number if it can. As a result, all those constants you have are being interpreted as integers and are causing a loss of precision in all your calculations. Add a .0 to the end of all of them and that should fix it.
So for example, instead of this line,
System.out.println("The surface of your rect. prism is "+ 2*(length*width*height));
change it to this instead:
System.out.println("The surface of your rect. prism is "+ 2.0*(length*width*height));
Last edited by Harakou (2012-05-22 21:46:41)
Offline
ZeroLuck wrote:
Try this:
Code:
switch ( (int) choice) { // ... }Or better: Don't use "double" if you don't have to:
- "double" needs 2x more RAM than an "int".
- "double" is using the FPU => slower than "int"
In an age where most computers have at the very least 1GB RAM, I don't think 4 extra bytes to store a double as opposed to an int is going to make a noticeable difference.
Not that you should use doubles when you don't need them, but it's definitely not worth worrying about.
Offline
Fixed it simply by changing the "choice" type to integer, thank you everyone
By the way, my computer has 18 GB RAM so I hardly think that the 4B matters (I run a minecraft server and I allocate 12GB to it.)
Offline
Harakou wrote:
Not that you should use doubles when you don't need them, but it's definitely not worth worrying about.
There are some more problems.
- The FPU is slower. (not a very big problem for simple programs)
- Problems with "=="
- Using always "double" isn't good programming style.
- quiet NaNs
Try this:
System.out.println((float) (Integer.MAX_VALUE) == (double) (Integer.MAX_VALUE));
The result is "false"...
long startTimer = 0; int sum1 = 0; double sum2 = 0; startTimer = System.currentTimeMillis(); for (int i = 0; i < 1000000000; i++) { sum1 += i; } System.out.println(System.currentTimeMillis() - startTimer + ": " + sum1); startTimer = System.currentTimeMillis(); for (int i = 0; i < 1000000000; i++) { sum2 += i; } System.out.println(System.currentTimeMillis() - startTimer + ": " + sum2);
The result is on my computer: CPU (Integer) 1843ms
FPU (Double) 5157ms
Last edited by ZeroLuck (2012-05-24 09:44:34)
Offline
Harakou wrote:
ZeroLuck wrote:
Try this:
Code:
switch ( (int) choice) { // ... }Or better: Don't use "double" if you don't have to:
- "double" needs 2x more RAM than an "int".
- "double" is using the FPU => slower than "int"In an age where most computers have at the very least 1GB RAM, I don't think 4 extra bytes to store a double as opposed to an int is going to make a noticeable difference.
Not that you should use doubles when you don't need them, but it's definitely not worth worrying about.
However, it does add up. In large projects, it might cost quite a bit of RAM and several crashes.
Offline
ZeroLuck wrote:
Harakou wrote:
Not that you should use doubles when you don't need them, but it's definitely not worth worrying about.
There are some more problems.
- The FPU is slower. (not a very big problem for simple programs) No, you're right. I never said it wasn't.
- Problems with "==" see below
- Using always "double" isn't good programming style. Oh it's absolutely poor style. I know that.
- quiet NaNs I'm not really qualified to talk about this. ^^
ZeroLuck wrote:
Try this:
Code:
System.out.println((float) (Integer.MAX_VALUE) == (double) (Integer.MAX_VALUE));The result is "false"...
Print them out to separate lines. Oddly enough, they're ever-so-slightly different. Rounding because of the level of precision I guess? Cast to the same type if it's such a concern. (Yeah, more annoying stuff that you have to watch for. That's programming for you I suppose. )
So yeah, I'm not disagreeing with you. Just saying that if you need something more precise than ints, you should use it.
Anyway, I don't think this was the intended topic of this thread anyway, hmm?
Offline