Extreme floating point values: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|J}}: Elaborate & extend)
Line 39: Line 39:
=={{header|J}}==
=={{header|J}}==


'''Extreme values'''
Negative zero can not be represented in J.
<lang j> Inf=: _
NegInf=: __
NB. Negative zero can not be represented in J.
NaN=. _.</lang>
The numeric atom <code>_.</code> ([[j:Essays/Indeterminate|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 <code>_.</code> 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'''
:Infinity is represented by _
<lang j> (1 % 0) , (_1 % 0)
:Negative infinity is represented by __
_ __
:NaN is represented by _. (and, by definition, produces inconsistent results in contexts where value is important).
(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>


=={{header|Java}}==
=={{header|Java}}==

Revision as of 01:43, 17 July 2010

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:

Ada

The language specifies model floating-point numbers independent on the underlying hardware. In particular, 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 value 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 for 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 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;

begin

  Put_Line ("-oo = " & Float'Image (-1.0 / Zero));
  Put_Line ("+oo = " & Float'Image (1.0 / Zero));
  Put_Line ("NaN = " & Float'Image (0.0 / Zero));
  Put_Line (" -0 = " & Float'Image (1.0 / (-1.0 / Zero)));

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

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>

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>