This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-05-22 19:20:14

matthew8092001
Scratcher
Registered: 2010-09-12
Posts: 100+

[Java] Loss of precision error.

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.

Code:

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;
        }

    }
}

http://1.bp.blogspot.com/_GVA115I1I8Y/TMGwbJ056kI/AAAAAAAABHs/kpvg0oCKV2g/s1600/happy.gif
lol

Offline

 

#2 2012-05-22 19:26:22

veggieman001
Scratcher
Registered: 2010-02-20
Posts: 1000+

Re: [Java] Loss of precision error.

I would assume that that means switch can't take a double, perhaps?


Posts: 20000 - Show all posts

Offline

 

#3 2012-05-22 21:37:47

SJRCS_011
Scratcher
Registered: 2011-02-07
Posts: 1000+

Re: [Java] Loss of precision error.

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)


http://i.imgur.com/vQqtH.png
Learning to Program in a Nutshell:  "You're missing a closing parentheses" - LS97

Offline

 

#4 2012-05-22 21:45:14

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: [Java] Loss of precision error.

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,

Code:

System.out.println("The surface of your rect. prism is "+ 2*(length*width*height));

change it to this instead:

Code:

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)


http://www.blocks.scratchr.org/API.php?action=random&return=image&link1=http://i.imgur.com/OZn2RD3.png&link2=http://i.imgur.com/duzaGTB.png&link3=http://i.imgur.com/CrDGvvZ.png&link4=http://i.imgur.com/POEpQyZ.png&link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#5 2012-05-23 07:40:09

ZeroLuck
Scratcher
Registered: 2010-02-23
Posts: 500+

Re: [Java] Loss of precision error.

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"


http://3.bp.blogspot.com/-oL2Atzp0Byw/T465vIQ36dI/AAAAAAAAADo/1vqL4PvhkM0/s1600/scratchdachwiki.png

Offline

 

#6 2012-05-23 16:47:25

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: [Java] Loss of precision error.

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.  tongue

Not that you should use doubles when you don't need them, but it's definitely not worth worrying about.


http://www.blocks.scratchr.org/API.php?action=random&return=image&link1=http://i.imgur.com/OZn2RD3.png&link2=http://i.imgur.com/duzaGTB.png&link3=http://i.imgur.com/CrDGvvZ.png&link4=http://i.imgur.com/POEpQyZ.png&link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#7 2012-05-23 20:07:51

matthew8092001
Scratcher
Registered: 2010-09-12
Posts: 100+

Re: [Java] Loss of precision error.

Fixed it simply by changing the "choice" type to integer, thank you everyone  tongue
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.)


http://1.bp.blogspot.com/_GVA115I1I8Y/TMGwbJ056kI/AAAAAAAABHs/kpvg0oCKV2g/s1600/happy.gif
lol

Offline

 

#8 2012-05-24 09:42:19

ZeroLuck
Scratcher
Registered: 2010-02-23
Posts: 500+

Re: [Java] Loss of precision error.

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:

Code:

System.out.println((float) (Integer.MAX_VALUE) == (double) (Integer.MAX_VALUE));

The result is "false"...  hmm

Code:

        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)


http://3.bp.blogspot.com/-oL2Atzp0Byw/T465vIQ36dI/AAAAAAAAADo/1vqL4PvhkM0/s1600/scratchdachwiki.png

Offline

 

#9 2012-05-24 10:47:38

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: [Java] Loss of precision error.

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.  tongue

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

 

#10 2012-05-25 00:52:26

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: [Java] Loss of precision error.

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"...  hmm

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.  hmm )

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?  wink


http://www.blocks.scratchr.org/API.php?action=random&amp;return=image&amp;link1=http://i.imgur.com/OZn2RD3.png&amp;link2=http://i.imgur.com/duzaGTB.png&amp;link3=http://i.imgur.com/CrDGvvZ.png&amp;link4=http://i.imgur.com/POEpQyZ.png&amp;link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

Board footer