Extreme floating point values

From Rosetta Code
Revision as of 20:05, 15 July 2010 by rosettacode>Mwn3d (Added Java)
Extreme floating point values is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The IEEE floating point specification defines certain 'extreme' floating point values such as minus zero, -0.0, a value distinct from plus zero; not a number, NaN; and plus and minus infinity.

The task is to use expressions involving other 'normal' floating point values in your language to calculate these, (and maybe other), extreme floating point values in your language and assign them to variables. Print the values of these variables if possible; and show some arithmetic with these values and variables. If your language can directly enter these extreme floating point values then show it.


C.f:

Java

<lang java>public class Extreme {

   public static void main(String[] args) {
       double negInf = -1.0 / 0.0; //also Double.NEGATIVE_INFINITY
       double inf = 1.0 / 0.0; //also Double.POSITIVE_INFINITY
       double nan = 0.0 / 0.0; //also Double.NaN
       double negZero = -2.0 / inf;
       System.out.println("Negative inf: " + negInf);
       System.out.println("Positive inf: " + inf);
       System.out.println("NaN: " + nan);
       System.out.println("Negative 0: " + negZero);
       System.out.println("inf + -inf: " + (inf + negInf));
       System.out.println("0 * NaN: " + (0 * nan));
       System.out.println("Nan == NaN: " + (nan == nan));
   }

}</lang> Output:

Negative inf: -Infinity
Positive inf: Infinity
NaN: NaN
Negative 0: -0.0
inf + -inf: NaN
0 * NaN: NaN
Nan == NaN: false

Python

<lang python>>>> # Extreme values from expressions >>> inf = 1e234 * 1e234 >>> _inf = 1e234 * -1e234 >>> _zero = 1 / _inf >>> nan = inf + _inf >>> inf, _inf, _zero, nan (inf, -inf, -0.0, nan) >>> # Print >>> for value in (inf, _inf, _zero, nan): print (value)

inf -inf -0.0 nan >>> # Extreme values from other means >>> float('nan') nan >>> float('inf') inf >>> float('-inf') -inf >>> -0. -0.0 >>> # Some arithmetic >>> nan == nan False >>> inf + _inf nan >>> 0.0 * nan nan >>> nan * 0.0 nan >>> 0.0 * inf nan >>> inf * 0.0 nan</lang>

<lang python>>>> # But note! >>> 1 / -0.0

Traceback (most recent call last):

 File "<pyshell#106>", line 1, in <module>
   1 / -0.0

ZeroDivisionError: float division by zero >>> # (Not minus infinity)</lang>