Mmmmmmmm, PI...
Hey, recently I was making a little PI calculator, just for the fun of it. Anyway, it works and all, but because of the 15-digit limitation on doubles (at least with VC++), I can''t get all that accurate. So what I want to know is: how would I go about making a new data structure that could hold more digits than a double?
Pointers to articles or whatever would be helpful.
Excuse me whilst I conquer Earth...
Commander M
(a.k.a. Crazy Yank)
http://commanderm.8m.com
CmndrM@gdnmail.net
I think you can just go
long double my_double;
Wow I think I actually answered a question!!
Maketty
(Matthew FitzGerald)
Knightvision Games
The meaning of Life part 5:
Live organ transplants...
long double my_double;
Wow I think I actually answered a question!!
Maketty
(Matthew FitzGerald)
Knightvision Games
The meaning of Life part 5:
Live organ transplants...
Maketty (Matthew FitzGerald) The meaning of Life part 5:Live organ transplants...
You could probably transform the numbers into chars and store them as a string.
BTW...maybe you could show me your source...it sounds interesting.
Here is my e-mail.
Well, hope that helps.
-MSkinn99
What's done is done (Until you hit Undo)
Edited by - MSkinn99 on November 13, 2000 6:53:07 PM
BTW...maybe you could show me your source...it sounds interesting.
Here is my e-mail.
Well, hope that helps.
-MSkinn99
What's done is done (Until you hit Undo)
Edited by - MSkinn99 on November 13, 2000 6:53:07 PM
-MSkinn99What's done is done (Until you hit Undo) :)Two wrongs don't make a right; but 3 lefts do.You'd be paranoid too if everyone was out to get you.
Yet another game programming web page...
Yet another game programming web page...
Solving PI (for MSkinn99''s sake):
http://www.gdarchive.net/druidgames/
#define ACC 1000000.0 // Accuracy of PI#include <stdio.h>void main(void) { long double pi = 1.0; // 18 digits of accuracy for(long double i = 3.0; i<ACC; i+=4.0 ) { pi -= 1.0 / i; pi += 1.0 / (i + 2.0); } pi *= 4.0; printf("\n\n%f",pi);}
http://www.gdarchive.net/druidgames/
If you are using C++, you could make an almost infinite data type:
create a class that has a pointer and a unsigned long (as private members).
the long says how many characters or doubles (whichever you use) are pointed to, and the pointer points to an array of doubles
you can then overload the math''s operators you need (+, -, *, /) to perform operations on your new type (as public members)
your new functions would resize the array as needed, and handle overflows / possible rounding errors.
You can use any data type to represent the data - you may also want to write a function to print the new data type too
if you aren''t using C++ (such that you cant overload operators) you can make functions like ''add'', ''subtract'' etc etc.
Wyzfen
create a class that has a pointer and a unsigned long (as private members).
the long says how many characters or doubles (whichever you use) are pointed to, and the pointer points to an array of doubles
you can then overload the math''s operators you need (+, -, *, /) to perform operations on your new type (as public members)
your new functions would resize the array as needed, and handle overflows / possible rounding errors.
You can use any data type to represent the data - you may also want to write a function to print the new data type too
if you aren''t using C++ (such that you cant overload operators) you can make functions like ''add'', ''subtract'' etc etc.
Wyzfen
Actually, on VC++ anyway, long double is exactly the same as double (at least I only got 15 digits after the decimal point for both...).
Wyzfen - thanks, I think that helps a little. I''ll have to play around with it when I get a chance.
MSkinn99 - go to my page and go to the projects section. The source code''s up there if you want to take a look.
Excuse me whilst I conquer Earth...
Commander M
(a.k.a. Crazy Yank)
http://commanderm.8m.com
CmndrM@gdnmail.net
Wyzfen - thanks, I think that helps a little. I''ll have to play around with it when I get a chance.
MSkinn99 - go to my page and go to the projects section. The source code''s up there if you want to take a look.
Excuse me whilst I conquer Earth...
Commander M
(a.k.a. Crazy Yank)
http://commanderm.8m.com
CmndrM@gdnmail.net
This is from msdn:
The long double contains 80 bits: 1 for sign, 15 for exponent, and 64 for mantissa. Its range is +/–1.2E4932 with at least 19 digits of precision. Although long double and double are separate types, the representation of long double and double is identical.
So you should have at least 19 digits of precision when using long double in vc++.
Edited by - fredizzimo on November 14, 2000 6:48:05 PM
The long double contains 80 bits: 1 for sign, 15 for exponent, and 64 for mantissa. Its range is +/–1.2E4932 with at least 19 digits of precision. Although long double and double are separate types, the representation of long double and double is identical.
So you should have at least 19 digits of precision when using long double in vc++.
Edited by - fredizzimo on November 14, 2000 6:48:05 PM
Just be warned that generating PI is a very CPU intensive task, and to generate PI to about 10 d.p can take hours
If I were you, I would use base 10 Binary coded decimal (i.e. store one each digit (0 - 9) of the number in a seperate variable). This will allow you to avoid all the base 2 floating point accuracy errors. You will need to write your own add, and multiply functions but if you want to get PI to a very high accuracy this is the only way to do it. You could fit 2 BCD's into one char, but you may as well just use an int for each BCD so that the array is aligned in the cache, which I think will speed it up a little.
So say you want to generate pi to 30 d.p. you would have an array called int pi[31]; With the method that Null and Void posted the largest no. you can possibly get is 4, so pi[0] is your units column, pi[1] is tenths and so on.
EDIT: Actually, you don't need a multiply function, you will need a reciprocal function. Do a search on the net for the Newton-Raphson method.
-- Kazan - Fire Mountain Games --
Edited by - Kazan on November 14, 2000 7:12:31 PM
If I were you, I would use base 10 Binary coded decimal (i.e. store one each digit (0 - 9) of the number in a seperate variable). This will allow you to avoid all the base 2 floating point accuracy errors. You will need to write your own add, and multiply functions but if you want to get PI to a very high accuracy this is the only way to do it. You could fit 2 BCD's into one char, but you may as well just use an int for each BCD so that the array is aligned in the cache, which I think will speed it up a little.
So say you want to generate pi to 30 d.p. you would have an array called int pi[31]; With the method that Null and Void posted the largest no. you can possibly get is 4, so pi[0] is your units column, pi[1] is tenths and so on.
EDIT: Actually, you don't need a multiply function, you will need a reciprocal function. Do a search on the net for the Newton-Raphson method.
-- Kazan - Fire Mountain Games --
Edited by - Kazan on November 14, 2000 7:12:31 PM
quote: Original post by CmndrM
MSkinn99 - go to my page and go to the projects section. The source code''s up there if you want to take a look.
The link is somewhat dead. I say somewhat, as your server admits that the file exists, it just refuses to grant access to .cpp files.
doubles & long doubles are both 8 bytes in msvc
delphi might try to convince you otherwise...
there is a way to calculate digits of pi without actually storing and using all the previous digits. You can just calculate away and dump the known digits into a file and forget about them... sry i don''t remember the method''s name...
delphi might try to convince you otherwise...
there is a way to calculate digits of pi without actually storing and using all the previous digits. You can just calculate away and dump the known digits into a file and forget about them... sry i don''t remember the method''s name...
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement