# Thread: Odd results from simple circle area/circumference calculation

1. Newbies
Join Date
May 2011
Posts
3
Rep Power
0

## 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. Read this. b.t.w. your value for pi sucks ;-)

kind regards,

Jos

3. 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. Newbies
Join Date
May 2011
Posts
3
Rep Power
0
Thank you both, that makes sense. The links provided are certainly useful, and much appreciated.

5. Originally Posted by Fubarable
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

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

7. Originally Posted by Fubarable
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

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

kind regards,

Jos

9. Originally Posted by JosAH
^^^^^ 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. Originally Posted by Fubarable
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

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

or:

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

kind regards,

Jos

13. Originally Posted by JosAH
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. Originally Posted by Fubarable
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

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

16. Originally Posted by JavaForums
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

17. Originally Posted by JosAH
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. Innocent my eye! I have proof of your subterfuge right here: WikiLeaks

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

19. Newbies
Join Date
May 2011
Posts
3
Rep Power
0
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.

#### Posting Permissions

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