Find the answer to your Linux question:
Results 1 to 5 of 5
I'm trying to work myself through an intro to java book, and one of the exercises is to find the area and circumference of a circle. Should be no problem ...
  1. #1
    Linux Enthusiast meton_magis's Avatar
    Join Date
    Oct 2006
    Location
    arizona
    Posts
    665

    java programming error.

    I'm trying to work myself through an intro to java book, and one of the exercises is to find the area and circumference of a circle. Should be no problem I though, but I'm getting data that shouldn't be in there.


    Code:
    public class test {
     public static void main (String args[])
     {
     String number = "10";
     final float PI = 3.14f;
     float radius = 0.0f;
     float areaCirc = 0.0f;
     float prmtrCirc = 0.0f;
    
     radius = Integer.parseInt( number);
     areaCirc = PI * ( radius * radius); // PIr^2
     prmtrCirc = PI * (radius + radius); // 2PIr
    
     System.out.println( "number is " + number +", radius is " + radius + ", area is " + areaCirc + ", perimeter is " + prmtrCirc );
    
    
     }
    }
    the output of this program is
    "number is 10, radius is 10.0, area is 314.0, perimeter is 62.800003"

    where does the extra 0.000003 come from? I've made this same program using C++, and it prints out the expected 62.8. Is there some hidden secret to java that I'm not getting?

    Thanks
    New to the internet, technical forums, or the hacker / open source community??
    Read this to learn good posting habits http://www.catb.org/~esr/faqs/smart-questions.html

    RHCE for RHEL version 5
    RHCT for RHEL version 4

  2. #2
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    121
    Quote Originally Posted by meton_magis View Post
    where does the extra 0.000003 come from? I've made this same program using C++, and it prints out the expected 62.8. Is there some hidden secret to java that I'm not getting?
    That looks like a perfectly normal floating-point error - nothing to worry about.

    Just remember that when you use floating-point numbers, you'll pretty much always get errors like these, because computers can't represent some (arguably "most", but that's from a pedantic mathematician's viewpoint) numbers exactly.

    It usually shouldn't concern you. There are format classes you can use in Java to neaten them up when you present them to the user (I'll let you google for that) and Java has, I think, the BigDecimal class if you really need to be concerned (and if you're just learning Java, you really don't).

    C++ may well be making the same errors, but just not printing them out.

  3. #3
    Linux Enthusiast meton_magis's Avatar
    Join Date
    Oct 2006
    Location
    arizona
    Posts
    665
    JohnGraham,

    That makes sense to me. I remember that point about floats from when I was studying C++, but I never found them while programming, so it kinda slipped my mind. I did get in the habit of truncating my decimals in C++, but didn't think that would be an issue here, since I'm using at most 2 decimals (I was only figuring on testing it for whole numbers.)

    Thanks for your help, I appreciate it.
    New to the internet, technical forums, or the hacker / open source community??
    Read this to learn good posting habits http://www.catb.org/~esr/faqs/smart-questions.html

    RHCE for RHEL version 5
    RHCT for RHEL version 4

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    This is what is commonly referred to as a "rounding error". Floats (32bit values) are only accurate to about 6 digits. You can format the output to strip off anything beyond that. IE. you answer is only accurate to 62.8000. Anything after that should be thrown away. A double will be more accurate (to about 15 digits precision), but you will still have rounding errors. So, this is where the old expression "the computer can't be wrong" is ... just plain wrong!
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Linux Enthusiast meton_magis's Avatar
    Join Date
    Oct 2006
    Location
    arizona
    Posts
    665
    I figured there would be a function that could truncate the float to only so many of the most significant digits, but I haven't gotten that far in the book yet. It wasn't a huge concern of mine, I was just wondering where the numbers came from. I should have known better, since I remember reading about the rounding error w/ floats when I did C++ work.
    New to the internet, technical forums, or the hacker / open source community??
    Read this to learn good posting habits http://www.catb.org/~esr/faqs/smart-questions.html

    RHCE for RHEL version 5
    RHCT for RHEL version 4

Posting Permissions

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