[Fsf-friends] [OT]Doing arithmetic with computers

Anand Babu ab@gnu.org.in
Wed Jun 8 20:54:30 IST 2005


,----[ 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. 

   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.

-- 
Anand Babu 
GPG Key ID: 0x62E15A31
Personal Blog  [http://freedom.freeshell.org]
The GNU Operating System [http://www.gnu.org]



More information about the Fsf-friends mailing list