Results 1 to 19 of 19
  1. #1
    cottoneyemoe is offline Newbies
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default Odd results from simple circle area/circumference calculation

    Hi all,

    I'm following a basic tutorial and trying to solve the related assignments using only what's been covered up to that point. The one I'm currently working on tasks you with finding the area and circumference of a circle given its radius, and with the value of pi set to 3.14.

    Having set up my variables thus:
    Java Code:
    double pi = 3.14, r, a, c
    presumably the following should give me what I need:
    Java Code:
    a = pi*r*r
    c = 2*pi*r
    and it works for most values of r, but not all. r=3, for instance, returns a = 28.259999999999998 instead of the expected a = 28.26. Various seemingly random numbers yield similar results. Between 1 and 50, they are: 3, 6, 12, 24, 29, 39, 43, 45 and 48. For the circumference, the following values of r in the same region return unexpected results: 5, 10, 19, 20, 38, 39 and 40.

    Changing the area calculation to:
    Java Code:
    a = pi*(r*r)
    corrects some cases (not all), but introduces new ones, and I don't understand why the brackets would make a difference in any case. I can't get the circumference calculation to change its behavior at all.

    Obviously I am missing something here, but I can't figure out what it is. Any ideas?

    (I know there are better ways of solving either calculation, but like I said, I'm trying to use only what's been covered in the tutorial thus far.)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Read this. b.t.w. your value for pi sucks ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    This is not a bug, it's not odd, and yes, you're missing something here, but fear not, for hopefully you'll understand soon enough. The key concept though is that digital computers cannot represent floating point numbers with 100% precision -- for example, it's simply impossible to represent 1/7 as a series of 0's and 1's. So what we all work with is the best precision possible and then to round our results when displaying them. Note that this is not a "Java" issue, but a general computing issue and is the same with any programming language. Note that you can increase the precision of your calculations by using BigDecimals instead of doubles, but the trade-off here is there is a significant speed and memory cost to use these, but if doing financial calculations, then the cost is worth the extra precision. For more on this, please look here:

    Java theory and practice: Where's your point?
    Floating point - Wikipedia, the free encyclopedia
    A P P E N D I X D - What Every Computer Scientist Should Know About Floating-Point Arithmetic
    Java: Floating-point

    Much luck!

  4. #4
    cottoneyemoe is offline Newbies
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    Thank you both, that makes sense. The links provided are certainly useful, and much appreciated.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    Note that you can increase the precision of your calculations by using BigDecimals instead of doubles [snip]
    Note that that doesn't help you one bit, e.g. 1/3 can't be represented with a BigDecimal number either; for a formal proof see Cantor's diagonalization trick. Only analog computers can represent numbers exactly but then the problem arises that we don't know what number they represent exactly at any moment in time ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    Note that that doesn't help you one bit, ...
    Gosh, I feel so violated, so, so... defenestrated!

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    Gosh, I feel so violated, so, so... defenestrated!
    And rightly so; everybody should feel smeared and defenestrated by Cantor's diagonalization trickery dickery :-P

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    ^^^^^ Eeeeew! What is that? I don't look like that! How can I get rid of that silly creature?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    ^^^^^ Eeeeew! What is that? I don't look like that! How can I get rid of that silly creature?
    Perhaps it's punishment for using Cantor's diagonalization trickery dickery willy-nilly and without concern for the thoughts of others.

    Either that or the new forum software is assigning default avatars if you haven't given yourself one.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    Perhaps it's punishment for using Cantor's diagonalization trickery dickery willy-nilly and without concern for the thoughts of others.

    Either that or the new forum software is assigning default avatars if you haven't given yourself one.
    There, I changed it; the default avatar looked like a deranged mutant snow goon; my eloquent creativity changed it and created a beautiful, arty, but not too arty, modern picture. Gugenheim, here I come!

    kind regards,

    Joa
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    And here I'd thought you'd use something like:



    or:


  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    And here I'd thought you'd use something like: [ snip ]
    I don't know that stuff, what is it?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    I don't know that stuff, what is it?
    Since this is a family-oriented forum, all that I'm allowed to say is that it ain't chit that I would touch much less drink.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    Since this is a family-oriented forum, all that I'm allowed to say is that it ain't chit that I would touch much less drink.
    I've got it! It's nutritious and full of vitamins and healthy and tasty and soft for your hands! I should try it once, thanks for the tip ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    JavaForums's Avatar
    JavaForums is offline Administrator
    Join Date
    Dec 2006
    Posts
    854
    Rep Power
    10

    Default

    Guys, are you, "senior members", spamming the forum? :P
    I changed the default avatar just for you Jos :D

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by JavaForums View Post
    Guys, are you, "senior members", spamming the forum? :P
    I changed the default avatar just for you Jos :D
    Fubarable started this all, I'm innocent as always. Can't you just remove the avatar entirely when a user specifies "no avatar"? b.t.w. I have a couple of other tastefull "no avatar"s in the making ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    JavaForums's Avatar
    JavaForums is offline Administrator
    Join Date
    Dec 2006
    Posts
    854
    Rep Power
    10

    Default

    Quote Originally Posted by JosAH View Post
    Can't you just remove the avatar entirely when a user specifies "no avatar"? b.t.w. I have a couple of other tastefull "no avatar"s in the making ;-)
    It was like that. If the user does not have an avatar, no picture was shown for that user inside threads.

    I noticed that the forum looks much more attractive/alive/interesting when users have different avatars.
    At first, i tried to find a plugin to set random avatars to "users without avatars" but could not find one.
    So i decided to change the default avatar.
    It looks like it is working. You are looking for a new avatar :)

    By the way that creature has a name: Duke. It can be dangerous to call him "creature" in a Java forum. :)

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Innocent my eye! I have proof of your subterfuge right here: WikiLeaks

    But I will dutifully delete my spam posts, ... in a bit. :)

  19. #19
    cottoneyemoe is offline Newbies
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    I posted a reply hours ago, but it still hasn't appeared, so I'm trying again - maybe I'll have more luck this time:
    Thank you both for clearing this up for me, it does make sense. The links provided are certainly useful, and much appreciated.

Similar Threads

  1. Simple calculation method problem.
    By carman12 in forum New To Java
    Replies: 11
    Last Post: 12-29-2010, 01:56 AM
  2. Replies: 3
    Last Post: 09-26-2010, 04:21 AM
  3. Parsing string and simple calculation
    By sapina007 in forum Advanced Java
    Replies: 4
    Last Post: 08-21-2009, 12:07 PM
  4. Area Calculation: Add, Substract, XOR
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:08 PM
  5. Replies: 0
    Last Post: 06-22-2008, 11:06 PM

Posting Permissions

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