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