On 6/8/05, Anand Babu ab@gnu.org.in wrote:
,----[ Ramanraj K ramanraj.k@gmail.com ] | While doing arithmetic, how is infinity represented on a computer? `---- Most modern computers support the IEEE floating point standard, which provides for positive infinity and negative infinity as floating point values. It also provides for a class of values called NaN or "not-a-number"; numerical functions return such values in cases where there is no correct answer.
Thanks for the information. The current IEEE standard appears to be implemented in PHP and python too.
PHP users could try this: <?php $inf = 1.8e300; do { $inf = $inf * 10; echo "<p>$inf</p>"; } while (is_finite($inf)); # stops when $inf exceeds 1.8e307 ?> There is an is_infinite function also.
The standard provides for +∞ and -∞ but overlooks the overflow at the point of transition to the next whole number. The overflow there should be explicitly represented.
To avoid any confusion in this discussion, a few practical uses of these representations:
First, why infinity should be represented as integers. Take the question: On how many computers can a user install and run free software licensed under the GPL?
Answer: On an unlimited number of computers.
ie: $gnu_computers = infinite
Infinity should therefore be supported not only in floats, but also integers. Practically, we may do very little arithmetic with the infinity values themselves. Now, on how many computers can we install software licensed under the FreeBSD license? The answer again may be written as $bsd_computers = infinite. But, it makes no sense to add this infinity with the previous one. The question, on how many computers a user has actually installed free software is very different, and the answer would always be finite and countable. Much useful arithmetic could be done with such data, and we may never accept infinity as an answer here, and we would like to see a clear figure no matter how large or how difficult it is for programmers to represent it.
Secondly, the IEEE standard representing inf should be made more useful. As Dr. Sasi Kumar pointed out earlier, there are an infinite number of rational numbers between two integers, and floats on machines cannot represent all the values in between. It would be more useful to return INF at the point where the machine cannot represent a required float value. For example, we may list the steps in a procedure as follows: 1.1 - Step 1 1.2 - Step 2 If a new step is to be inserted between Steps 1 and 2, it could be inserted as Step 1.1 and the float representing it could be (1.1 + 1.2)/2 = 1.15 The new list of steps would be: 1.1 - Step 1 1.15 - Step 1.1 1.2 - Step 2
The float values help to sort the list when required. Though it is a bad idea to keep inserting new steps infinitely this way in the real world, in theory, this should be possible. But, there would be a point when the machine is unable to generate a float for inserting a new step where it would give a value after "rounding off" that is not between 1 and 2, which would defeat the purpose and design. It would be better if INF is returned instead of rounding off at this point, so that we may trap the overflow and handle it appropriately.
Positive infinity `.0e+INF' Negative infinity `.0e+NaN'. Not-a-number `.0e+NaN'.
Under GNU Guile (Scheme) (define +Inf (/ 1.0 0.0)) (define -Inf (- +Inf)) (define NaN (/ 0.0 0.0))
guile> +Inf +#.# guile> -Inf -#.# guile> NaN #.#
Under GNU Emacs (elisp) (setq +Inf (/ 1.0 0.0)) => 1.0e+INF (setq -Inf (- +Inf)) => -1.0e+INF (setq NaN (/ 0.0 0.0)) => -0.0e+NaN
Under GCC (C) #include <stdio.h> int main () { float p_inf=1e30000; /* causes overflow and sets to infinity */ float n_inf=-p_inf; float nan = p_inf/p_inf; printf ("%f %f %f\n", p_inf, n_inf, nan);
return (0);
} => inf -inf nan
And now you can do arithmetic using these symbols.
and with recursion :)