Extreme floating point values: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|Oz}}: Impl. added)
(GNU Forth)
Line 66: Line 66:
Valid NaN is FALSE
Valid NaN is FALSE
</pre>
</pre>

=={{header|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>


=={{header|J}}==
=={{header|J}}==

Revision as of 21:14, 19 July 2010

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

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:

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

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>