Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Precision in calculations

Precision in calculations
Thread Tools
Moderator
Join Date: May 2001
Location: Hilbert space
Status: Offline
Reply With Quote
Oct 30, 2002, 04:04 PM
 
So I started learning objective C and started with the Currency Converter tutorial.

After writing the easy code, I got the followin issue.

When I enter a number like 0,9812 for the exchange rate, and I enter 100 in the next field, my result is

98,1199951171875

If 98,11999999999 would be the answer, I remember hearing about that stuff. But there are many figures off, what happened?
I don't suffer from insanity, I enjoy every minute of it.
     
Professional Poster
Join Date: Apr 2001
Location: Long Beach, CA
Status: Offline
Reply With Quote
Oct 31, 2002, 12:01 AM
 
The problem is that many numbers that are easily representable in the base 10 number system are not easily representable in the base 2 number system. As such, there is rounding error. One way to limit the funkiness of such an effect is to have the system round values that are monetary. This can be set up rather easily in IB.

An example of something that doesn't convert well to base 10:

1/3 in base 3 is 0.1. In base 10, that comes out to about .3333333. In base 2, that comes out to about .010101010101...

If you need more precision, you can use double instead of float. However, for monetary values, you can just round at 2 decimal places.

ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
     
Moderator
Join Date: May 2001
Location: Hilbert space
Status: Offline
Reply With Quote
Oct 31, 2002, 04:11 AM
 
Originally posted by Detrius:
The problem is that many numbers that are easily representable in the base 10 number system are not easily representable in the base 2 number system. As such, there is rounding error. One way to limit the funkiness of such an effect is to have the system round values that are monetary. This can be set up rather easily in IB.

An example of something that doesn't convert well to base 10:

1/3 in base 3 is 0.1. In base 10, that comes out to about .3333333. In base 2, that comes out to about .010101010101...

If you need more precision, you can use double instead of float. However, for monetary values, you can just round at 2 decimal places.
I was aware of that, but thought just the last digit is prone to error, because the internal precision is higher.

But thanks.
I don't suffer from insanity, I enjoy every minute of it.
     
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
Oct 31, 2002, 03:10 PM
 
Another good way for dealing with number representation issues is to use Cocoa's NSDecimalNumber class, which offers several methods for performing arithmetic and can munge the underlying representation as needed to deliver correct results.
Rick Roe
icons.cx | weblog
     
Mac Elite
Join Date: Mar 2001
Location: Provo, UT
Status: Offline
Reply With Quote
Oct 31, 2002, 07:41 PM
 
There are also lots of math classes available on the web for doing arbitrary precision arithmetic. Whether they'd meet your needs depends upon what you are doing.

While I've never used it, gcc (Apple's compiler) comes with libg++ which purportedly has this.

Here's a nice list of other free libraries.

http://www.oonumerics.org/oon/#libraries
     
Moderator
Join Date: May 2001
Location: Hilbert space
Status: Offline
Reply With Quote
Nov 2, 2002, 05:04 PM
 
Thanks for all the stuff.

Another quick question: is the only way to get a power of another number pow(x,y) (= x^y)?
I don't suffer from insanity, I enjoy every minute of it.
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Nov 3, 2002, 10:23 AM
 
Originally posted by OreoCookie:
Thanks for all the stuff.

Another quick question: is the only way to get a power of another number pow(x,y) (= x^y)?
There are a couple other specific functions (constant^x) like scalbn() and exp() but if you just want x^y, I think pow is the way to go.
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
Moderator
Join Date: May 2001
Location: Hilbert space
Status: Offline
Reply With Quote
Nov 4, 2002, 05:33 AM
 
Originally posted by parallax:


There are a couple other specific functions (constant^x) like scalbn() and exp() but if you just want x^y, I think pow is the way to go.
Thanks. Wasn't sure, because every other programming language that I knew so far could calculate powers directly.
I don't suffer from insanity, I enjoy every minute of it.
     
Fresh-Faced Recruit
Join Date: Oct 2002
Location: St. Louis, MO
Status: Offline
Reply With Quote
Nov 4, 2002, 09:52 AM
 
This is drifting off topic, but uh, i don't care

The reason C doesn't have a built-in power function is because the language is kept intentionally trim. C is actually one of the simplest programming languages out there, with less than 30 keywords (i don't remember the exact number though, and my C book is all the way on the other side of the room ). For instance, C doesn't even include standard input and output, no special math functions (even common ones like pow() and sqrt()), etc. Everything is meant to be added on through libraries and modules. In C, nearly everything you write will have a few common includes, stdlib.h, stdio.h, math.h, etc. Because those aren't built into the language, its easier to keep C portable. Since objectiveC is built straight off of C, it too is reasonably simple, with barely enough additions to the language to support OO.

Peace,
phidauex
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:16 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2