Extreme floating point values

Revision as of 06:09, 20 July 2010 by rosettacode>Stormneedle (Mumps)

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.

Task
Extreme floating point values
You are encouraged to solve this task according to the task description, using any language you may know.

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:

Ada

The language specifies model floating-point numbers independent of the underlying hardware. Even if the machine numbers are IEEE 754, the user-defined floating-point numbers are guaranteed to have no IEEE 754 semantics. In particular, their values do not include any non-numeric ideals. Constraint_Error exception is propagated when the result of a numeric operation assigned to a floating-point variable is not in the range (the range is always numeric).

For performance reasons, the built-in floating-point types like Float and Long_Float are allowed to have IEEE 754 semantics if the machine numbers are IEEE 754. But the language provides means to exclude all non numbers from these types by defining a subtype with an explicit range: <lang Ada> subtype Consistent_Float is Float range Float'Range; -- No IEEE ideals </lang> In general in properly written Ada programs variables may not become invalid when standard numeric operations are applied. The language also provides the attribute 'Valid to verify values obtained from unsafe sources e.g. from input, unchecked conversions etc.

As stated above on a machine where Float is implemented by an IEEE 754 machine number, IEEE 754 is permitted leak through. The following program illustrates how this leak can be exploited: <lang Ada> with Ada.Text_IO; use Ada.Text_IO;

procedure IEEE is -- Non portable, bad, never do this!

  Zero  : Float := 0.0;
  PInf  : Float := 1.0 / Zero;
  NInf  : Float := -PInf;
  PZero : Float := 1.0 / PInf;
  NZero : Float := 1.0 / NInf;
  NaN   : Float := 0.0 / Zero; 

begin

  Put_Line (" -oo = " & Float'Image (NInf));
  Put_Line (" +oo = " & Float'Image (PInf));
  Put_Line (" NaN = " & Float'Image (NaN));
  Put_Line ("  -0 = " & Float'Image (NZero));
  Put_Line (" -oo < first " & Boolean'Image (NInf < Float'First));
  Put_Line (" +oo > last  " & Boolean'Image (PInf > Float'Last));
  Put_Line (" NaN = NaN   " & Boolean'Image (NaN = NaN));
  Put_Line ("  -0 = 0     " & Boolean'Image (NZero = 0.0));
  Put_Line ("  +0 = 0     " & Boolean'Image (PZero = 0.0));
  Put_Line ("  +0 < least positive   " & Boolean'Image (PZero < Float'Succ (Zero)));
  Put_Line ("  -0 > biggest negative " & Boolean'Image (NZero > Float'Pred (Zero)));
     -- Validness checks
  Put_Line ("Valid -oo is " & Boolean'Image (NInf'Valid));
  Put_Line ("Valid +oo is " & Boolean'Image (PInf'Valid));
  Put_Line ("Valid NaN is " & Boolean'Image (NaN'Valid));

end IEEE; </lang> The expression -1.0 / 0.0 were non-numeric and thus could not be used. To fool the compiler the variable Zero is used, which circumvents type checks giving desired broken result. Sample output:

 -oo = -Inf*******
 +oo =  +Inf*******
 NaN = NaN********
  -0 = -0.00000E+00
 -oo < first TRUE
 +oo > last  TRUE
 NaN = NaN   FALSE
  -0 = 0     TRUE
  +0 = 0     TRUE
  +0 < least positive   TRUE
  -0 > biggest negative TRUE
Valid -oo is FALSE
Valid +oo is FALSE
Valid NaN is FALSE

Forth

Works with: GNU Forth

<lang forth> 1e 0e f/ f. \ inf -1e 0e f/ f. \ inf (output bug: should say "-inf") -1e 0e f/ f0< . \ -1 (true, it is -inf)

0e 0e f/ f.     \ nan

-1e 0e f/ 1/f f0< . \ 0 (false, can't represent IEEE negative zero)</lang>

J

Extreme values <lang j> Inf=: _

  NegInf=: __
  NB. Negative zero can not be represented in J. 
  NaN=. _.</lang>

The numeric atom _. (Indeterminate) is provided as a means for dealing with NaN in data from sources outside J. J itself generates NaN errors rather than NaN values and recommends that _. be removed from data as soon as possible because, by definition, they will produce inconsistent results in contexts where value is important.

Extreme values from expressions <lang j> (1 % 0) , (_1 % 0) _ __

  (1e234 * 1e234) , (_1e234 * 1e234)

_ __

  _ + __         NB. generates NaN error, rather than NaN

|NaN error | _ +__

  _ - _          NB. generates NaN error, rather than NaN

|NaN error | _ -_</lang>

Some arithmetic <lang j> _ + _ _

  __ + __

__

  Inf + 0

_

  NegInf * 0

0</lang>

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

MUMPS

ANSI MUMPS

The 1995 Standard MUMPS (X11.1–1995) implementations do not deal with floating point numbers following IEEE 754. Attempting to use a number over the precision of the system results in a <MAXNUMBER> error:

USER>write 3e145
30000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
USER>write 3e146
 
<MAXNUMBER>

Intersystems Caché

Caché has the function $DOUBLE which complies with the IEEE 754 standard. The negative zero is indistinguishable from positive zero by operations. The special values evaluate to 0 when converted to a number in a later operation. <lang MUMPS> EXTREMES

 NEW INF,NINF,ZERO,NOTNUM,NEGZERO
 SET INF=$DOUBLE(3.0E310),NINF=$DOUBLE(-3.0E310),ZERO=$DOUBLE(0),NOTNUM=$DOUBLE(INF-INF),NEGZERO=$DOUBLE(ZERO*-1)
 WRITE "Infinity: ",INF,!
 WRITE "Infinity ",$SELECT($ISVALIDNUM(INF):"is a number",1:"is not a number"),!
 WRITE "Negative Infinity: ",NINF,!
 WRITE "Negative Infinity ",$SELECT($ISVALIDNUM(NINF):"is a number",1:"is not a number"),!
 WRITE "Zero: ",ZERO,!
 WRITE "Zero ",$SELECT($ISVALIDNUM(ZERO):"is a number",1:"is not a number"),!
 WRITE "Negative Zero: ",NEGZERO,!
 WRITE "Negative Zero ",$SELECT($ISVALIDNUM(NEGZERO):"is a number",1:"is not a number"),!
 WRITE "Not a Number: ",NOTNUM,!
 WRITE "Not a Number ",$SELECT($ISVALIDNUM(NOTNUM):"is a number",1:"is not a number"),!
 KILL INF,NINF,ZERO,NONNUM,NEGZERO
QUIT

</lang>

Output:

USER>d EXTREMES^ROSETTA
Infinity: INF
Infinity is not a number
Negative Infinity: -INF
Negative Infinity is not a number
Zero: 0
Zero is a number
Negative Zero: 0
Negative Zero is a number
Not a Number: NAN
Not a Number is not a number

OCaml

<lang ocaml># infinity;; - : float = infinity

  1. neg_infinity;;

- : float = neg_infinity

  1. nan;;

- : float = nan

  1. -0.;;

- : float = -0.

  1. -. 0.;;

- : float = -0.

  1. 1. /. 0.;;

- : float = infinity

  1. -1. /. 0.;;

- : float = neg_infinity

  1. -. infinity;;

- : float = neg_infinity

  1. infinity +. neg_infinity;;

- : float = nan

  1. 0. /. 0.;;

- : float = nan

  1. infinity /. infinity;;

- : float = nan

  1. nan = nan;;

- : bool = false

  1. nan == nan;;

- : bool = true

  1. 0. *. infinity;;

- : float = nan

  1. 0. = -0.;;

- : bool = true

  1. 0. == -0.;;

- : bool = false</lang>

Oz

<lang oz>declare Inf = 1.0e234 * 1.0e234 MinusInf = 1.0e234 * ~1.0e234 Zero = 1.0 / Inf MinusZero = 1.0 / MinusInf NaN = 0.0 / 0.0

{System.showInfo "infinite: "#Inf} {System.showInfo "-infinite: "#MinusInf} {System.showInfo "0: "#Zero} {System.showInfo "-0: "#MinusZero}  %% seems to be identical to Zero {System.showInfo "NaN: "#NaN}

{System.showInfo "inf + -inf: "#Inf+MinusInf} {System.showInfo "NaN * 0: "#NaN*0.0} {System.showInfo "0 * NaN: "#0.0*NaN} {System.showInfo "inf * 0: "#Inf*0.0} {System.showInfo "0 * inf: "#0.0*Inf}

{Show NaN == NaN}  %% shows 'true' ! {Show Zero == MinusZero}

{Show 1.0/0.0 == Inf} %% true {Show 1.0/~0.0 == MinusInf} %% true</lang>

Output: <lang oz>infinite: 1.#INF -infinite: -1.#INF 0: 0.0 -0: 0.0 NaN: -1.#IND inf + -inf: -1.#IND NaN * 0: -1.#IND 0 * NaN: -1.#IND inf * 0: -1.#IND 0 * inf: -1.#IND true true true true</lang>

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 >>> nan is nan True >>> 0. == -0. True >>> 0. is -0. 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>

Tcl

Tcl includes support for all IEEE “extreme” values except for NaN, which it throws a catchable exception on encountering numerically. For example, see this log of an interactive session: <lang tcl>% package require Tcl 8.5 8.5.2 % set inf_val [expr {1.0 / 0.0}] Inf % set neginf_val [expr {-1.0 / 0.0}] -Inf % set negzero_val [expr {1.0 / $neginf_val}] -0.0 % expr {0.0 / 0.0} domain error: argument not in valid range % expr nan domain error: argument not in valid range % expr {1/-inf} -0.0</lang> It is possible to introduce a real NaN though, by using the mechanisms for dealing with external binary data (it being judged better to just deal with it in that case rather than throwing an exception): <lang tcl>% binary scan [binary format q nan] q nan 1 % puts $nan NaN % # Show that it is a real NaN in there % expr {$nan+0} can't use non-numeric floating-point value as operand of "+"</lang>