tags:

views:

530

answers:

10

I'm curious about infinite numbers in computing, in particular pi.

For a computer to render a circle it would have to understand pi. But how can it if it is infinite?

Am I looking too much into this? Would it just use a rounded value?

+6  A: 

An approximation is generally sufficient. To "render" a circle, the computer only needs to understand pi well enough to render accurately at whatever resolution (finite) is required.

Edit: as others have pointed out, you don't even need pi to render a circle. Still, the gist of the question was "how do computers deal with numbers like pi?" They use approximations, and whoever is using those approximations must decide whether they are precise enough for the given purpose.

Donnie DeBoer
To render a circle, you usually don't use pi at all.
Nosredna
+2  A: 
abelenky
PI does NOT have repeating decimal patterns as it is transcendental, which means that not only is it irrational (cannot be expressed as a fraction of two integers) it is also not algebraic (it isn't the solution to any rational polynomial equation).
spatz
Not repeating. It has an infinite number of *nonzero* decimal digits, but they don't repeat. (See the comments to the question for the debate about what this is actually called)
David Zaslavsky
doh! writing "repeating" was a total typo. Fixed now as "non-repeating".
abelenky
+1  A: 

I believe it rounds it to a very small number, and is most likely a constant. If you use PHP, this is how PI renders:

echo pi(); // 3.1415926535898
echo M_PI; // 3.1415926535898

Just like you only need 3.14159 in High School, computers only need so much to get it fairly accurate.

Chacha102
17 decimal digits is about all you can represent.
S.Lott
+1  A: 

Computers just use rounded values of pi, unless of course there is a special case such as scientific computing. For example, in python pi is represented as:

>>> import math
>>> math.pi
3.1415926535897931

You can test this out for yourself in IDLE, pythons interactive interpreter.

Javed Ahamed
Interesting: C# reports the value as 3.14159265358979323846.Note the digits: 97931 vs. 97932That looks like something more than a rounding error to me.
abelenky
Only about 17 of the digits are meaningful. The last digit is little more than noise coming back from the platform's binary-to-decimal conversion algorithm.
S.Lott
+1  A: 
duffymo
+11  A: 

Mathematically, computers are both finite and non-continuous and therefore can neither know PI completely, nor correctly render a circle.

However, in the digital realm neither of these exist anyway, so it is sufficient to approximate PI and then use that to approximately render the circle, resulting in exactly the same pixels that would have been calculated from an exact PI anyway.

Either way, the resulting pixels aren't really a circle either, because they are a finite collection of digital points and a circle is a curve made up of an infinite number of points, most with irrational values.

(It has been pointed out to me that PI is not normally used to plot a circle, which is true, however, the methods used to plot a circle are related to the formulas used to express and/or calculate the value of PI, which still have the same issues).

RBarryYoung
I'd be curious to know why the random downvote is for? If there's something wrong with my answer, please tell me. But AFAIK, it is precisely correct, both mathematically and wrt computation theory.
RBarryYoung
+1 Logic works where math fails.
WolfmanDragon
Indeed, this is a perfectly good high-level explanation. What makes it so good is that it doesn't just apply to circles, but is effectively universal.
Pavel Minaev
Humans are both finite and non-continuous...
Will Bickford
@WolfmanDragon Math is logic
Seth
Actually, since humans are both non-discrete and "real" they are technically considered continuous.
RBarryYoung
+1  A: 

Programming languages use a rounded constant for pi and similar "infinite" numbers.

In order to get higher precision you use iterative algorithms that are looped for as long as is required.

Console
+5  A: 

You don't need PI at all to draw a circle. There are many ways to draw a circle. The naive way is with sine and cosine.

The algorithm I saw most often on 8-bit machines was Bresenham's circle. You don't even need floating point math for that.

Nosredna
RBarryYoung
Sine and cosine are related to e, pi, and imaginary numbers, but I disagree that it's _typical_ that they are calculated from any of those, or from tables representing those. I'd say it's typical for Sine and Cosine to be calculated by a series expansion that converges quickly. But the C language doesn't say how they should be calculated. You can find Taylor expansions that calculate the circular functions without any reference to pi or e. Although, as I said, all these entities are inter-related.
Nosredna
My apologies, you are correct Nosredna. In my dotage, I seem to have forgotten how Taylor series, et al, are constructed.
RBarryYoung
No apology needed!
Nosredna
+1  A: 

Pi is not infinite it is irrational, what mean that you can not express it as quotient. It has infinite number of digits. http://en.wikipedia.org/wiki/Proof_that_π_is_irrational

About computing find some informations here. http://en.wikipedia.org/wiki/Computing_π

Nice page is also this http://3.141592653589793238462643383279502884197169399375105820974944592.com/

ralu
+2  A: 

Somewhere I saw a proof that to draw a circle around the universe to millimetre accuracy, you'd need less than 100 digits of pi, in other words, far fewer digits than have been calculated by people with too much time on their hands (or too much computing power...). Now, if only I could find that proof... (edit) found it

jford
Yeah, but what if you wanted to antialias it?
Nosredna