Results 1 to 13 of 13

Thread: Rounding error

  1. #1
    oceanstorm42 is offline Member
    Join Date
    Jul 2015
    Posts
    5
    Rep Power
    0

    Default Rounding error

    Hello:)

    Here is my code:


    import java.util.Scanner;

    public class Population
    {
    public static void main(String[] args){
    double organism;
    int days;
    System.out.print("Enter the number of days the organisms will multiply: ");
    days = input.nextInt();
    while(days < 1){
    System.out.print("Invalid. Enter 1 or more: ");
    days = input.nextInt();
    }
    System.out.println("Day Organisms");
    System.out.println("-----------------------------");
    System.out.println("1"+ " " +organism);
    for( int i = 2; i <= days; i++){
    organism = organism*(increase + 1);
    System.out.print(i+" "+organism);
    System.out.println();
    }
    }
    } double increase;
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the starting number organisms: ");
    organism = input.nextDouble();
    while(organism < 2){
    System.out.print("Invalid. Must be at least 2. Re-enter: ");
    organism = input.nextDouble();
    }
    System.out.print("Enter the daily increase: ");
    increase = input.nextDouble();
    while(increase < 0){
    System.out.print("Invalid. Enter a non-negative number: ");
    increase = input.nextDouble();
    }

    The error is At 3 days the expected output is 39.2
    My out put is 39.2
    Please help:)

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Rounding error

    The error is At 3 days the expected output is 39.2
    My out put is 39.2
    Did you type that correctly? It says the expected output is the same as the actual output. They are both 39.2???


    Please edit your post and wrap the code in code tags.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    oceanstorm42 is offline Member
    Join Date
    Jul 2015
    Posts
    5
    Rep Power
    0

    Default Re: Rounding error

    Hello
    :)
    what is wrap code in code tags?

    expected 39.2
    actual 39.19999999999999999996

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Rounding error

    Quote Originally Posted by oceanstorm42 View Post
    what is wrap code in code tags?
    BB Code List - Java Programming Forum - Learn Java Programming

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Rounding error

    Quote Originally Posted by oceanstorm42 View Post
    expected 39.2
    actual 39.19999999999999999996
    What Every Computer Scientist Should Know About Floating-Point Arithmetic

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Rounding error

    That's pretty close to 39.2

    Take a look at this: What Every Computer Scientist Should Know About Floating-Point Arithmetic
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    oceanstorm42 is offline Member
    Join Date
    Jul 2015
    Posts
    5
    Rep Power
    0

    Default Re: Rounding error

    I retyped the code like this

    import java.util.Scanner;

    public class Population
    {
    public static void main(String[] args){
    double organism;
    int days;
    double increase;
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the starting number organisms: ");
    organism = input.nextDouble();
    while(organism < 2){
    System.out.print("Invalid. Must be at least 2. Re-enter: ");
    organism = input.nextDouble();
    }
    System.out.print("Enter the daily increase: ");
    increase = input.nextDouble();
    while(increase < 0){
    System.out.print("Invalid. Enter a non-negative number: ");
    increase = input.nextDouble();
    }
    System.out.print("Enter the number of days the organisms will multiply: ");
    days = input.nextInt();
    while(days < 1){
    System.out.print("Invalid. Enter 1 or more: ");
    days = input.nextInt();
    }
    System.out.println("Day Organisms");
    System.out.println("-----------------------------");
    System.out.println("1"+ " " +organism);
    for( int i = 2; i <= days; i++){
    organism = organism*(increase + 1);
    System.out.print(i+" "+organism);
    System.out.println();
    }
    }
    }

    Expected
    1&rarr&rarr20.0↵
    2&rarr&rarr28.0↵
    3&rarr&rarr39.2↵
    4&rarr&rarr54.88↵
    5&rarr&rarr76.83200000000001↵

    Actual
    1ииииииии20.0↵
    2иииии28.0↵
    3иииии39.199999999999996↵
    4иииии54.87999999999999↵
    5иииии76.83199999999998↵

    I am learning that cut and paste is better, I'm not sure how to wrap...maybe could you show me with an example from my code?

    I read the floating decimal point...I'm not sure which part of my code to change. I am confused because there is more than one piece of code for days and number of organisms. Thank you for reading and replying.
    I really appreciate the help

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Rounding error

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    oceanstorm42 is offline Member
    Join Date
    Jul 2015
    Posts
    5
    Rep Power
    0

    Default Re: Rounding error

    I think I got a good start...I tried to line it up better...but I'm not sure how to "preserve highlighting"



    Java Code:
    import java.util.Scanner;
    
    public class Population
    {
       public static void main(String[] args){
       double organism;
       int days;
       double increase;
       Scanner input = new Scanner(System.in);
       System.out.print("Enter the starting number organisms: ");
       organism =  input.nextDouble();
       while(organism < 2)
    
    {
         
            System.out.print("Invalid. Must be at least 2. Re-enter: ");
            organism =  input.nextDouble();
     }
    
      System.out.print("Enter the daily increase: ");
      increase = input.nextDouble();
      while(increase < 0)
    {
           System.out.print("Invalid. Enter a non-negative number: ");
            increase = input.nextDouble();
     }
      
     System.out.print("Enter the number of days the organisms will multiply: ");
       days = input.nextInt();
       while(days < 1)
    {
           System.out.print("Invalid. Enter 1 or more: ");
            days = input.nextInt();
     }
        System.out.println("Day     Organisms");
               System.out.println("-----------------------------");
               System.out.println("1"+ "        " +organism);
               for( int i = 2; i <= days; i++){
                   organism = organism*(increase + 1);
                   System.out.print(i+"     "+organism);
                   System.out.println();
        }
    }
    }

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Rounding error

    how to "preserve highlighting"
    I'm not sure what you are asking. Your code now has highlighting (see the Strings) and its formatting is preserved. However, the formatting needs some work. The indentations are not right and the { and } are not properly aligned.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    oceanstorm42 is offline Member
    Join Date
    Jul 2015
    Posts
    5
    Rep Power
    0

    Default Re: Rounding error

    Java Code:
    import java.util.Scanner;
     
    public class Population
    {
       public static void main(String[] args){
       double organism;
       int days;
       double increase;
       Scanner input = new Scanner(System.in);
       System.out.print("Enter the starting number organisms: ");
       organism =  input.nextDouble();
       while(organism < 2)
     
    {
          
            System.out.print("Invalid. Must be at least 2. Re-enter: ");
            organism =  input.nextDouble();
    }
     
            System.out.print("Enter the daily increase: ");
             increase = input.nextDouble();
             while(increase < 0)
    {
           System.out.print("Invalid. Enter a non-negative number: ");
            increase = input.nextDouble();
    }
       
     System.out.print("Enter the number of days the organisms will multiply: ");
       days = input.nextInt();
       while(days < 1)
    {
           System.out.print("Invalid. Enter 1 or more: ");
            days = input.nextInt();
    }
               System.out.println("Day     Organisms");
               System.out.println("-----------------------------");
               System.out.println("1"+ "        " +organism);
               for( int i = 2; i <= days; i++){
                   organism = organism*(increase + 1);
                   System.out.print(i+"     "+organism);
                   System.out.println();
    }
    }
    }

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Rounding error

    The indentations and the placement of the { and } are not correct.

    There are lots of other threads here on the forum that show correct indentations and placement of {}s.

    Some IDEs have options to format code. Check if yours has one.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Rounding error

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Rounding?!! Need help!
    By aejames23 in forum New To Java
    Replies: 2
    Last Post: 11-04-2012, 01:10 AM
  2. I need help rounding
    By MrPosotive in forum New To Java
    Replies: 2
    Last Post: 09-27-2012, 04:59 PM
  3. Rounding decimal error
    By NixasMuraki in forum New To Java
    Replies: 3
    Last Post: 06-01-2011, 01:07 AM
  4. Compile Error (decimal rounding attempt)
    By mylosol in forum New To Java
    Replies: 2
    Last Post: 05-08-2011, 11:13 PM
  5. Need help rounding. =/
    By yo1mcool in forum New To Java
    Replies: 1
    Last Post: 10-07-2008, 05:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •