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