OSDev.org

The Place to Start for Operating System Developers
It is currently Mon May 06, 2024 7:10 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Precision (C++)
PostPosted: Thu Mar 27, 2008 1:11 am 
Offline
Member
Member
User avatar

Joined: Tue May 22, 2007 2:36 pm
Posts: 1069
Well, as you might have guessed, it's all about precision.
I want to do a very simple calculation, but with maximum precision.
What I am talking about is what I think called... well I really have no idea what it's called in english, but it doesn't matter, heres an example:

(1- 1/2)(1 - 1/3)(1 - 1/5)(1 - 1/7)...
or
(1 - 1/p1)(1 - 1/p2)(1 - 1/p3)(1 - 1/p4)...

As you see primes is the key element here, but that is not my problem, i've allready written a fairly inginious prime class ;)

The problem, of course, is that if i do the above calculation straight forward, I will soon run in to some serious precision issues, so instead i want to do it like this:

Code:
p1-1   p2-1   p3-1   p4-1
---- * ---- * ---- * ---- ...
p1     p2     p3     p4


The first obious advantage is that i can multiply the nominators and denominators thus ending up with a single division, but allso another trick is left:

Code:
1   2   4   6
- * - * - * - ...
2   3   5   7


This can easily be converted in such a way that... well i can't explain it, sorry for my poor english.

Code:
1    2/2   4   6/3
--- * --- * - * --- ...
2/2   3/3   5    7

equaling:
Code:
1   1   4   2
- * - * - * - ...
1   1   5   7


thus our final nominator and denominator will be as small as possible but still yelding the same result.

I have tryed to figure out a smart way to im pliment all this, but I'm simply not experienced enough, so I was hoping that someone would give it a try.

Nb.
This is not something important, the result isn't very important and the reason I started this little experiment was really only for the sake of the experiment, so if nobody other than me finds it of interested, then be it.

Thanks anyway for reading ;)

_________________
This was supposed to be a cool signature...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 27, 2008 1:25 am 
Offline
Member
Member
User avatar

Joined: Tue Jul 10, 2007 5:27 am
Posts: 2935
Location: York, United Kingdom
Uuuuhh, sorry, but:

Code:
1       1
-  != ----
2      2/2


2/2 = 1, not 2!

Also, your starting and ending expressions are *completely different*! Just get out your calculator and check! You've managed to do:

Code:
1/2 * 1/3 = 1


Which, I grant you is pretty impressive, but wholly wrong! ;)

_________________
Horizon - a framework and language for SAS-OS development
Project 'Pedigree'
Practical x86 OSDev tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 27, 2008 1:52 am 
Offline
Member
Member
User avatar

Joined: Tue May 22, 2007 2:36 pm
Posts: 1069
JamesM wrote:
Uuuuhh, sorry, but:

Code:
1       1
-  != ----
2      2/2


2/2 = 1, not 2!

but i didn't say that.
JamesM wrote:
Also, your starting and ending expressions are *completely different*! Just get out your calculator and check! You've managed to do:

Code:
1/2 * 1/3 = 1


Which, I grant you is pretty impressive, but wholly wrong! ;)


ok, here goes....
( 1 * 2 * 4 * 6 ) / ( 2 * 3 * 5 * 7 )
==
48 / 210
==
0,22857142857142857142857142857143
==
( 1 * ( 2 / 2 ) * 4 * ( 6 / 3 ) ) / ( ( 2 / 2 ) * ( 3 / 3 ) * 5 * 7 )
==
8 / 35
==
0,22857142857142857142857142857143
i did do the math, just to be on the safe side.

_________________
This was supposed to be a cool signature...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 27, 2008 7:44 am 
Offline
Member
Member
User avatar

Joined: Wed Feb 07, 2007 1:45 pm
Posts: 1401
Location: Eugene, OR, US
There is an entire class of mathematics software called "arbitrary precision arithmetic." In order to do this sort of calculation, you need such a package. If you just google around a bit with that search term (or go to wikipedia) I think you will find many freeware open-source packages.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 27, 2008 8:01 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
why not simply write
Code:
0
:twisted:

_________________
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 27, 2008 10:19 am 
Offline
Member
Member
User avatar

Joined: Tue May 22, 2007 2:36 pm
Posts: 1069
bewing wrote:
There is an entire class of mathematics software called "arbitrary precision arithmetic." In order to do this sort of calculation, you need such a package. If you just google around a bit with that search term (or go to wikipedia) I think you will find many freeware open-source packages.


But then i wouldn't learn anything...

anyway, i'll take yet another wach at it when i get the time.

_________________
This was supposed to be a cool signature...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 27, 2008 1:48 pm 
Offline
Member
Member
User avatar

Joined: Wed Feb 07, 2007 1:45 pm
Posts: 1401
Location: Eugene, OR, US
If you want to learn, then the wikipedia article will give you hints on how to write your own.
Or, you can always take apart other people's code, to learn.

Basically, you need to understand how floating point arithmetic is done in a machine. There is an IEEE standard for it. What the software does is extend the "exponent" and the "mantissa" to be infinitely long, by allocating new dwords/qwords to handle the extra bits when they overflow.
And then the standard package of arithmetic functions are modified to use arbitrary length exponents and mantissas.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group