 |
 |
Precision in calculations
|
 |
|
 |
|
Moderator 
Join Date: May 2001
Location: Hilbert space
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Mar 2001
Location: Provo, UT
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|