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