File: doc_Text.cpp

package info (click to toggle)
faust 0.9.46-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 15,256 kB
  • ctags: 9,961
  • sloc: cpp: 47,746; sh: 2,254; ansic: 1,503; makefile: 1,211; ruby: 950; yacc: 468; objc: 459; lex: 200; xml: 177
file content (268 lines) | stat: -rw-r--r-- 6,506 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/************************************************************************
 ************************************************************************
    FAUST compiler
	Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
    ---------------------------------------------------------------------
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 ************************************************************************
 ************************************************************************/



#include <stdio.h>
#include <string.h>
#include "doc_Text.hh"
#include "compatibility.hh"
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <assert.h>
#include <cmath>

#include "floats.hh"

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

#ifndef M_PI_2
#define M_PI_2 1.57079632679489661923
#endif

#ifndef M_PI_4
#define M_PI_4 0.785398163397448309616
#endif

#ifndef M_E
#define M_E 2.71828182845904523536
#endif

extern bool gInternDoubleSwitch;
const string symbolicNumber (double n);



#if 0
/**
 * Suppress trailing zero in a string representating a floating point number.
 * example : 1.00000  -> 1.0
 * example : 1.00000f -> 1.0f
 */

static void zdel(char* c)
{
    int     l = strlen(c) - 1;
    bool    f = (c[l] == 'f');

    if (f) c[l--] = 0;      // remove trailing if any f
    while ( l>1 && c[l-1] != '.' && c[l] == '0')  c[l--] = 0;
    if (f) c[++l] = 'f';    // restaure trailing f if needed
}
#endif

string docT (char* c) 	{ return string(c); }
string docT (int n) 	{ char c[64]; snprintf(c, 63, "%d",n);  return string(c); }
string docT (long n) 	{ char c[64]; snprintf(c, 63, "%ld",n); return string(c); }
string docT (double n) { return symbolicNumber(n); }


//
//*****************************SYMBOLIC NUMBER REPRESENTATION*******************
//

/**
 * Compute the smallest float representable
 * difference epsilon such that 1 != 1+epsilon
 */
float fltEpsilon()
{
   float machEps = 1.0f;
   do {
      machEps /= 2.0f;
   } while ((float)(1.0 + (machEps/2.0)) != 1.0);
   return machEps;
}


/**
 * Compute the smallest double representable
 * difference epsilon such that 1 != 1+epsilon
 */
double dblEpsilon()
{
   double machEps = 1.0f;
   do {
      machEps /= 2.0f;
   } while ((1.0 + (machEps/2.0)) != 1.0);
   return machEps;
}


/**
 * Check if two floating point numbers are (almost) equal
 * Abs(x-y) < epsilon
 */
static bool AlmostEqual(double A, double B)
{
    double maxRelativeError = 2*dblEpsilon();
    double maxAbsoluteError = maxRelativeError;


    if (fabs(A - B) < maxAbsoluteError)
        return true;
    double relativeError;
    if (fabs(B) > fabs(A))
        relativeError = fabs((A - B) / B);
    else
        relativeError = fabs((A - B) / A);
    if (relativeError <= maxRelativeError)
        return true;
    return false;
}


/**
 * Return true if n>0 is equal to PI^k for some small integer k.
 * k = log(n)/log(pi) is integer => n = exp(int(k)*log(pi))
 * The latex representation \pi^{k} is returned in string s
 */
bool isPiPower (double n, string& s)
{
    assert(n>0);
    stringstream ss (stringstream::out|stringstream::in);
    int k = floor(log(n)/log(M_PI));
    if ( AlmostEqual(n, exp(k * log(M_PI))) && (k!=0) && (abs(k)<5.0) ) {
        ss << "\\pi";
        if (k!=1)  ss << "^{"<< k <<"}";
        s = ss.str();
        return true;
    } else {
        return false;
    }
}


/**
 * Return true if n>0 is equal to e^k for some small integer k.
 * The latex representation e^{k} is returned in string s
 */
bool isExpPower (double n, string& s)
{
    assert(n>0);
    stringstream ss (stringstream::out|stringstream::in);
    int k = floor(log(n));
    if ( AlmostEqual(n, exp(k)) && (k!=0) && (abs(k)<5.0) ) {
        ss << "e";
        if (k!=1)  ss << "^{"<< k <<"}";
        s = ss.str();
        return true;
    } else {
        return false;
    }
}


/**
 * Return true if n>0 is equal to e^k or PI^k for some integer k
 * The symbolic latex representation is returned in string s
 */
bool isSymbolicPower (double n, string& s)
{
    assert(n>0);
    if (isPiPower(n,s)) {
        return true;
    } else if (isExpPower(n,s)) {
        return true;
    } else {
        return false;
    }
}


/**
 * Return exp or num.exp, or exp/denom, or num/denom.exp
 */
const string addFraction (int num, int denom, const string& exp)
{
    stringstream ss (stringstream::out|stringstream::in);

    if ((num==1) & (denom==1)) {
        ss << exp;
    } else if ((num==1) & (denom!=1)) {
        ss << "\\frac{"<< exp <<  "}{" << denom << "}";
    } else if ((num!=1) & (denom==1)) {
        ss << num << "*" << exp;
    } else {
        ss << "\\frac{"<< num <<  "}{" << denom << "}*" << exp;
    }
    return ss.str();
}


/**
 * Return symbolic or numerical representation of n>0
 */
const string positiveSymbolicNumber (double n)
{
    string s;
    assert(n>0);

    // Try to find a symbolic representation

    for (int i=1;i<10;i++) {
        for(int j=1;j<10;j++) {
            if (isSymbolicPower(i*n/j,s)) {
                return addFraction(j,i,s);
            }
        }
    }

    // No symbolic representation,
    // Then numerical representation x.10^k

    char tmp[64];
    string entree = " * 10^{";
    char sortie = '}';
    string::size_type ps;

    snprintf(tmp, 63, "%.15g", n); // Warning: over 15 decimals, results are wrong !!
    s = tmp;
    ps = s.find('e');

    if (ps != string::npos) {
        s.replace(ps, 1, "");
        s.insert(ps, entree);
        s += sortie;
    }

    return s;

}


/**
 * Return symbolic or numerical representation of n
 */
const string symbolicNumber (double n)
{
    if (n>0.0) {
        return positiveSymbolicNumber(n);
    } else if (n<0.0) {
        return string("-") + positiveSymbolicNumber(-n);
    } else {
        return "0";
    }
}