Results 1 to 19 of 19
- 05-22-2011, 03:31 PM #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:
presumably the following should give me what I need:Java Code:double pi = 3.14, r, a, c
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.Java Code:a = pi*r*r c = 2*pi*r
Changing the area calculation to:
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.Java Code:a = pi*(r*r)
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.)
- 05-22-2011, 03:43 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
Read this. b.t.w. your value for pi sucks ;-)
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
-
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!
- 05-22-2011, 04:40 PM #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.
- 05-22-2011, 04:46 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
-
- 05-22-2011, 05:14 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
- 05-22-2011, 05:16 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
^^^^^ Eeeeew! What is that? I don't look like that! How can I get rid of that silly creature?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
-
- 05-22-2011, 06:02 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
-
And here I'd thought you'd use something like:
or:
- 05-22-2011, 07:56 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
-
- 05-22-2011, 08:40 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
- 05-22-2011, 08:56 PM #15
Guys, are you, "senior members", spamming the forum? :P
I changed the default avatar just for you Jos :D
- 05-22-2011, 09:02 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-22-2011, 09:24 PM #17
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. :)
-
Innocent my eye! I have proof of your subterfuge right here: WikiLeaks
But I will dutifully delete my spam posts, ... in a bit. :)
- 05-22-2011, 10:00 PM #19
Newbies
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Simple calculation method problem.
By carman12 in forum New To JavaReplies: 11Last Post: 12-29-2010, 01:56 AM -
Help with simple area/perimeter program for school
By icarus in forum New To JavaReplies: 3Last Post: 09-26-2010, 04:21 AM -
Parsing string and simple calculation
By sapina007 in forum Advanced JavaReplies: 4Last Post: 08-21-2009, 12:07 PM -
Area Calculation: Add, Substract, XOR
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:08 PM -
Area Calculation: Add, Subtract, Intersect, Exclusive Or
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:06 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks