Results 1 to 19 of 19
 05222011, 03:31 PM #1Newbies
 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
Java Code:a = pi*r*r c = 2*pi*r
Changing the area calculation to:
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.)
 05222011, 03:43 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
Read this. b.t.w. your value for pi sucks ;)
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.

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 tradeoff 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 FloatingPoint Arithmetic
Java: Floatingpoint
Much luck!
 05222011, 04:40 PM #4Newbies
 Join Date
 May 2011
 Posts
 3
 Rep Power
 0
Thank you both, that makes sense. The links provided are certainly useful, and much appreciated.
 05222011, 04:46 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
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,
JosThe only person who got everything done by Friday was Robinson Crusoe.

 05222011, 05:14 PM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
 05222011, 05:16 PM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
^^^^^ Eeeeew! What is that? I don't look like that! How can I get rid of that silly creature?
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.

 05222011, 06:02 PM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
The only person who got everything done by Friday was Robinson Crusoe.

And here I'd thought you'd use something like:
or:
 05222011, 07:56 PM #12
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24

 05222011, 08:40 PM #14
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
 05222011, 08:56 PM #15
Guys, are you, "senior members", spamming the forum? :P
I changed the default avatar just for you Jos :D
 05222011, 09:02 PM #16
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
The only person who got everything done by Friday was Robinson Crusoe.
 05222011, 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. :)
 05222011, 10:00 PM #19Newbies
 Join Date
 May 2011
 Posts
 3
 Rep Power
 0
Similar Threads

Simple calculation method problem.
By carman12 in forum New To JavaReplies: 11Last Post: 12292010, 02:56 AM 
Help with simple area/perimeter program for school
By icarus in forum New To JavaReplies: 3Last Post: 09262010, 04:21 AM 
Parsing string and simple calculation
By sapina007 in forum Advanced JavaReplies: 4Last Post: 08212009, 12:07 PM 
Area Calculation: Add, Substract, XOR
By Java Tip in forum java.awtReplies: 0Last Post: 06222008, 11:08 PM 
Area Calculation: Add, Subtract, Intersect, Exclusive Or
By Java Tip in forum java.awtReplies: 0Last Post: 06222008, 11:06 PM
Bookmarks