cannam@95: (* cannam@95: * Copyright (c) 1997-1999 Massachusetts Institute of Technology cannam@95: * Copyright (c) 2003, 2007-11 Matteo Frigo cannam@95: * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology cannam@95: * cannam@95: * This program is free software; you can redistribute it and/or modify cannam@95: * it under the terms of the GNU General Public License as published by cannam@95: * the Free Software Foundation; either version 2 of the License, or cannam@95: * (at your option) any later version. cannam@95: * cannam@95: * This program is distributed in the hope that it will be useful, cannam@95: * but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@95: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@95: * GNU General Public License for more details. cannam@95: * cannam@95: * You should have received a copy of the GNU General Public License cannam@95: * along with this program; if not, write to the Free Software cannam@95: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA cannam@95: * cannam@95: *) cannam@95: cannam@95: (* The generator keeps track of numeric constants in symbolic cannam@95: expressions using the abstract number type, defined in this file. cannam@95: cannam@95: Our implementation of the number type uses arbitrary-precision cannam@95: arithmetic from the built-in Num package in order to maintain an cannam@95: accurate representation of constants. This allows us to output cannam@95: constants with many decimal places in the generated C code, cannam@95: ensuring that we will take advantage of the full precision cannam@95: available on current and future machines. cannam@95: cannam@95: Note that we have to write our own routine to compute roots of cannam@95: unity, since the Num package only supplies simple arithmetic. The cannam@95: arbitrary-precision operations in Num look like the normal cannam@95: operations except that they have an appended slash (e.g. +/ -/ */ cannam@95: // etcetera). *) cannam@95: cannam@95: open Num cannam@95: cannam@95: type number = N of num cannam@95: cannam@95: let makeNum n = N n cannam@95: cannam@95: (* decimal digits of precision to maintain internally, and to print out: *) cannam@95: let precision = 50 cannam@95: let print_precision = 45 cannam@95: cannam@95: let inveps = (Int 10) **/ (Int precision) cannam@95: let epsilon = (Int 1) // inveps cannam@95: cannam@95: let pinveps = (Int 10) **/ (Int print_precision) cannam@95: let pepsilon = (Int 1) // pinveps cannam@95: cannam@95: let round x = epsilon */ (round_num (x */ inveps)) cannam@95: cannam@95: let of_int n = N (Int n) cannam@95: let zero = of_int 0 cannam@95: let one = of_int 1 cannam@95: let two = of_int 2 cannam@95: let mone = of_int (-1) cannam@95: cannam@95: (* comparison predicate for real numbers *) cannam@95: let equal (N x) (N y) = (* use both relative and absolute error *) cannam@95: let absdiff = abs_num (x -/ y) in cannam@95: absdiff <=/ pepsilon or cannam@95: absdiff <=/ pepsilon */ (abs_num x +/ abs_num y) cannam@95: cannam@95: let is_zero = equal zero cannam@95: let is_one = equal one cannam@95: let is_mone = equal mone cannam@95: let is_two = equal two cannam@95: cannam@95: cannam@95: (* Note that, in the following computations, it is important to round cannam@95: to precision epsilon after each operation. Otherwise, since the cannam@95: Num package uses exact rational arithmetic, the number of digits cannam@95: quickly blows up. *) cannam@95: let mul (N a) (N b) = makeNum (round (a */ b)) cannam@95: let div (N a) (N b) = makeNum (round (a // b)) cannam@95: let add (N a) (N b) = makeNum (round (a +/ b)) cannam@95: let sub (N a) (N b) = makeNum (round (a -/ b)) cannam@95: cannam@95: let negative (N a) = (a = 1.0) then (f' -. (float (truncate f'))) else f' cannam@95: in let q = string_of_int (truncate(f2 *. 1.0E9)) cannam@95: in let r = "0000000000" ^ q cannam@95: in let l = String.length r cannam@95: in let prefix = if (f < 0.0) then "KN" else "KP" in cannam@95: if (f' >= 1.0) then cannam@95: (prefix ^ (string_of_int (truncate f')) ^ "_" ^ cannam@95: (String.sub r (l - 9) 9)) cannam@95: else cannam@95: (prefix ^ (String.sub r (l - 9) 9)) cannam@95: cannam@95: let to_string (N n) = approx_num_fix print_precision n cannam@95: cannam@95: let to_float (N n) = float_of_num n cannam@95: