src/Tools/float.ML
author wenzelm
Tue Sep 18 16:08:00 2007 +0200 (2007-09-18)
changeset 24630 351a308ab58d
parent 24584 01e83ffa6c54
child 30161 c26e515f1c29
permissions -rw-r--r--
simplified type int (eliminated IntInf.int, integer);
haftmann@24584
     1
(*  Title:      Tools/float.ML
haftmann@23251
     2
    ID:         $Id$
haftmann@23251
     3
    Author:     Steven Obua, Florian Haftmann, TU Muenchen
haftmann@23251
     4
haftmann@23251
     5
Implementation of real numbers as mantisse-exponent pairs.
haftmann@23251
     6
*)
haftmann@23251
     7
haftmann@23251
     8
signature FLOAT =
haftmann@23251
     9
sig
wenzelm@24630
    10
  type float = int * int
haftmann@23251
    11
  val zero: float
haftmann@23251
    12
  val eq: float * float -> bool
haftmann@23520
    13
  val ord: float * float -> order
haftmann@23520
    14
  val sign: float -> order
haftmann@23251
    15
  val min: float -> float -> float
haftmann@23251
    16
  val max: float -> float -> float
haftmann@23251
    17
  val add: float -> float -> float
haftmann@23251
    18
  val sub: float -> float -> float
haftmann@23251
    19
  val neg: float -> float
haftmann@23251
    20
  val mult: float -> float -> float
haftmann@23251
    21
  val positive_part: float -> float
haftmann@23251
    22
  val negative_part: float -> float
haftmann@23251
    23
end;
haftmann@23251
    24
haftmann@23251
    25
structure Float : FLOAT =
haftmann@23251
    26
struct
haftmann@23251
    27
wenzelm@24630
    28
type float = int * int;
haftmann@23251
    29
wenzelm@23297
    30
val zero: float = (0, 0);
haftmann@23251
    31
haftmann@23251
    32
fun add (a1, b1) (a2, b2) =
wenzelm@24630
    33
  if b1 < b2 then
wenzelm@24630
    34
    (a1 + a2 * Integer.square (b2 - b1), b1)
haftmann@23251
    35
  else
wenzelm@24630
    36
    (a1 * Integer.square (b1 - b2) + a2, b2);
haftmann@23251
    37
haftmann@23251
    38
fun sub (a1, b1) (a2, b2) =
wenzelm@24630
    39
  if b1 < b2 then
wenzelm@24630
    40
    (a1 - a2 * Integer.square (b2 - b1), b1)
haftmann@23251
    41
  else
wenzelm@24630
    42
    (a1 * Integer.square (b1 - b2) - a2, b2);
haftmann@23251
    43
wenzelm@24630
    44
fun neg (a, b) = (~ a, b);
haftmann@23251
    45
wenzelm@24630
    46
fun mult (a1, b1) (a2, b2) = (a1 * a2, b1 + b2);
haftmann@23251
    47
haftmann@23520
    48
fun sign (a, b) = Integer.sign a;
haftmann@23251
    49
haftmann@23520
    50
fun ord (r, s) = sign (sub r s);
haftmann@23251
    51
haftmann@23520
    52
fun eq (r, s) = ord (r, s) = EQUAL;
haftmann@23251
    53
haftmann@23520
    54
fun min r s = case ord (r, s) of LESS => r | _ => s;
haftmann@23520
    55
fun max r s = case ord (r, s) of LESS => s | _ => r;
haftmann@23251
    56
wenzelm@24630
    57
fun positive_part (a, b) = (Int.max (0, a), b);
wenzelm@24630
    58
fun negative_part (a, b) = (Int.min (0, a), b);
haftmann@23251
    59
haftmann@23251
    60
end;