|
FAUST compiler
0.9.9.6b8
|
00001 /************************************************************************ 00002 ************************************************************************ 00003 FAUST compiler 00004 Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale 00005 --------------------------------------------------------------------- 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00019 ************************************************************************ 00020 ************************************************************************/ 00021 00022 00023 00024 #include <stdio.h> 00025 #include <string.h> 00026 #include "doc_Text.hh" 00027 #include "compatibility.hh" 00028 #include <string> 00029 #include <vector> 00030 #include <iostream> 00031 #include <sstream> 00032 #include <assert.h> 00033 00034 #include "floats.hh" 00035 00036 extern bool gInternDoubleSwitch; 00037 string scientific2tenpow (double n); 00038 00039 00040 00041 #if 0 00042 00048 static void zdel(char* c) 00049 { 00050 int l = strlen(c) - 1; 00051 bool f = (c[l] == 'f'); 00052 00053 if (f) c[l--] = 0; // remove trailing if any f 00054 while ( l>1 && c[l-1] != '.' && c[l] == '0') c[l--] = 0; 00055 if (f) c[++l] = 'f'; // restaure trailing f if needed 00056 } 00057 #endif 00058 00059 string docT (char* c) { return string(c); } 00060 string docT (int n) { char c[64]; snprintf(c, 63, "%d",n); return string(c); } 00061 string docT (long n) { char c[64]; snprintf(c, 63, "%ld",n); return string(c); } 00062 00063 #if 0 00064 string docT (float n) 00065 { 00066 char c[64]; 00067 if (n < 0.1 && n > -0.1 && n != 0.0) { 00068 //snprintf(c, 63, "%e", n);//f", n); 00069 string s = scientific2tenpow(n); 00070 snprintf(c, 63, "%s", s.c_str()); 00071 } else { 00072 snprintf(c, 63, "%f", n);//f", n); 00073 zdel(c); 00074 } 00075 return string(c); 00076 } 00077 #endif 00078 00084 string docT (double n) 00085 { 00086 // char c[64]; 00087 // if (n < 0.1 && n > -0.1 && n != 0.0) { 00088 // //snprintf(c, 63, "%e", n); //, inumix()); 00091 // snprintf(c, 63, "%g", n); 00092 // } else { 00093 // snprintf(c, 63, "%g", n); 00094 // //zdel(c); 00095 // } 00096 // return string(c); 00097 return scientific2tenpow(n); 00098 } 00099 00100 string scientific2tenpow (double n) 00101 { 00102 char tmp[64]; 00103 string entree = " * 10^{"; 00104 char sortie = '}'; 00105 string s; 00106 string::size_type ps; 00107 00108 snprintf(tmp, 63, "%.15g", n); // Warning: over 15 decimals, results are wrong !! 00109 // snprintf(tmp, 63, "%f", n); 00110 00111 // cerr << "doc_Text.cpp : scientific2tenpow : " << n << " -> \"" << tmp << "\"" << endl; 00112 s = tmp; 00113 ps = s.find('e'); 00114 00115 if (ps != string::npos) { 00116 s.replace(ps, 1, ""); 00117 s.insert(ps, entree); 00118 s += sortie; 00119 } 00120 else { 00121 //cerr << "doc_Text.cpp : scientific2tenpow : \'e\' non trouvé" << endl; 00122 } 00123 00124 return s; 00125 }
1.7.5.1