FAUST compiler  0.9.9.6b8
Macros | Functions | Variables
doc_Text.cpp File Reference
#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 <stdlib.h>
#include "floats.hh"
Include dependency graph for doc_Text.cpp:

Go to the source code of this file.

Macros

#define M_PI   3.14159265358979323846
 
#define M_PI_2   1.57079632679489661923
 
#define M_PI_4   0.785398163397448309616
 
#define M_E   2.71828182845904523536
 

Functions

const string symbolicNumber (double n)
 Return symbolic or numerical representation of n. More...
 
string docT (char *c)
 
string docT (int n)
 
string docT (long n)
 
string docT (double n)
 
float fltEpsilon ()
 Compute the smallest float representable difference epsilon such that 1 != 1+epsilon. More...
 
double dblEpsilon ()
 Compute the smallest double representable difference epsilon such that 1 != 1+epsilon. More...
 
static bool AlmostEqual (double A, double B)
 Check if two floating point numbers are (almost) equal Abs(x-y) < epsilon. More...
 
bool isPiPower (double n, string &s)
 Return true if n>0 is equal to PI^k for some small integer k. More...
 
bool isExpPower (double n, string &s)
 Return true if n>0 is equal to e^k for some small integer k. More...
 
bool isSymbolicPower (double n, string &s)
 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. More...
 
const string addFraction (int num, int denom, const string &exp)
 Return exp or num.exp, or exp/denom, or num/denom.exp. More...
 
const string positiveSymbolicNumber (double n)
 Return symbolic or numerical representation of n>0. More...
 

Variables

bool gInternDoubleSwitch
 

Macro Definition Documentation

#define M_E   2.71828182845904523536

Definition at line 51 of file doc_Text.cpp.

#define M_PI   3.14159265358979323846

Definition at line 39 of file doc_Text.cpp.

Referenced by isPiPower().

#define M_PI_2   1.57079632679489661923

Definition at line 43 of file doc_Text.cpp.

#define M_PI_4   0.785398163397448309616

Definition at line 47 of file doc_Text.cpp.

Function Documentation

const string addFraction ( int  num,
int  denom,
const string &  exp 
)

Return exp or num.exp, or exp/denom, or num/denom.exp.

Definition at line 199 of file doc_Text.cpp.

Referenced by positiveSymbolicNumber().

200 {
201  stringstream ss (stringstream::out|stringstream::in);
202 
203  if ((num==1) & (denom==1)) {
204  ss << exp;
205  } else if ((num==1) & (denom!=1)) {
206  ss << "\\frac{"<< exp << "}{" << denom << "}";
207  } else if ((num!=1) & (denom==1)) {
208  ss << num << "*" << exp;
209  } else {
210  ss << "\\frac{"<< num << "}{" << denom << "}*" << exp;
211  }
212  return ss.str();
213 }
Definition: num.hh:58

Here is the caller graph for this function:

static bool AlmostEqual ( double  A,
double  B 
)
static

Check if two floating point numbers are (almost) equal Abs(x-y) < epsilon.

Definition at line 119 of file doc_Text.cpp.

References dblEpsilon().

Referenced by isExpPower(), and isPiPower().

120 {
121  double maxRelativeError = 2*dblEpsilon();
122  double maxAbsoluteError = maxRelativeError;
123 
124 
125  if (fabs(A - B) < maxAbsoluteError)
126  return true;
127  double relativeError;
128  if (fabs(B) > fabs(A))
129  relativeError = fabs((A - B) / B);
130  else
131  relativeError = fabs((A - B) / A);
132  if (relativeError <= maxRelativeError)
133  return true;
134  return false;
135 }
double dblEpsilon()
Compute the smallest double representable difference epsilon such that 1 != 1+epsilon.
Definition: doc_Text.cpp:105

Here is the call graph for this function:

Here is the caller graph for this function:

double dblEpsilon ( )

Compute the smallest double representable difference epsilon such that 1 != 1+epsilon.

Definition at line 105 of file doc_Text.cpp.

Referenced by AlmostEqual().

106 {
107  double machEps = 1.0f;
108  do {
109  machEps /= 2.0f;
110  } while ((1.0 + (machEps/2.0)) != 1.0);
111  return machEps;
112 }

Here is the caller graph for this function:

string docT ( char *  c)

Definition at line 77 of file doc_Text.cpp.

Referenced by DocCompiler::compileLateq(), DocCompiler::generateCode(), DocCompiler::generateIota(), DocCompiler::getFreshID(), and DocCompiler::prepareIntervallicUI().

77 { return string(c); }

Here is the caller graph for this function:

string docT ( int  n)

Definition at line 78 of file doc_Text.cpp.

78 { char c[64]; snprintf(c, 63, "%d",n); return string(c); }
string docT ( long  n)

Definition at line 79 of file doc_Text.cpp.

79 { char c[64]; snprintf(c, 63, "%ld",n); return string(c); }
string docT ( double  n)

Definition at line 80 of file doc_Text.cpp.

References symbolicNumber().

80 { return symbolicNumber(n); }
const string symbolicNumber(double n)
Return symbolic or numerical representation of n.
Definition: doc_Text.cpp:260

Here is the call graph for this function:

float fltEpsilon ( )

Compute the smallest float representable difference epsilon such that 1 != 1+epsilon.

Definition at line 91 of file doc_Text.cpp.

92 {
93  float machEps = 1.0f;
94  do {
95  machEps /= 2.0f;
96  } while ((float)(1.0 + (machEps/2.0)) != 1.0);
97  return machEps;
98 }
bool isExpPower ( double  n,
string &  s 
)

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

Definition at line 163 of file doc_Text.cpp.

References abs(), and AlmostEqual().

Referenced by isSymbolicPower().

164 {
165  assert(n>0);
166  stringstream ss (stringstream::out|stringstream::in);
167  int k = (int)floor(log(n));
168  if ( AlmostEqual(n, exp(k)) && (k!=0) && (abs(k)<5.0) ) {
169  ss << "e";
170  if (k!=1) ss << "^{"<< k <<"}";
171  s = ss.str();
172  return true;
173  } else {
174  return false;
175  }
176 }
static bool AlmostEqual(double A, double B)
Check if two floating point numbers are (almost) equal Abs(x-y) < epsilon.
Definition: doc_Text.cpp:119
interval abs(const interval &x)
Definition: interval.hh:226

Here is the call graph for this function:

Here is the caller graph for this function:

bool isPiPower ( double  n,
string &  s 
)

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 ^{k} is returned in string s

Definition at line 143 of file doc_Text.cpp.

References abs(), AlmostEqual(), and M_PI.

Referenced by isSymbolicPower().

144 {
145  assert(n>0);
146  stringstream ss (stringstream::out|stringstream::in);
147  int k = (int)floor(log(n)/log(M_PI));
148  if ( AlmostEqual(n, exp(k * log(M_PI))) && (k!=0) && (abs(k)<5.0) ) {
149  ss << "\\pi";
150  if (k!=1) ss << "^{"<< k <<"}";
151  s = ss.str();
152  return true;
153  } else {
154  return false;
155  }
156 }
static bool AlmostEqual(double A, double B)
Check if two floating point numbers are (almost) equal Abs(x-y) < epsilon.
Definition: doc_Text.cpp:119
interval abs(const interval &x)
Definition: interval.hh:226
#define M_PI
Definition: doc_Text.cpp:39

Here is the call graph for this function:

Here is the caller graph for this function:

bool isSymbolicPower ( double  n,
string &  s 
)

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.

Definition at line 183 of file doc_Text.cpp.

References isExpPower(), and isPiPower().

Referenced by positiveSymbolicNumber().

184 {
185  assert(n>0);
186  if (isPiPower(n,s)) {
187  return true;
188  } else if (isExpPower(n,s)) {
189  return true;
190  } else {
191  return false;
192  }
193 }
bool isPiPower(double n, string &s)
Return true if n>0 is equal to PI^k for some small integer k.
Definition: doc_Text.cpp:143
bool isExpPower(double n, string &s)
Return true if n>0 is equal to e^k for some small integer k.
Definition: doc_Text.cpp:163

Here is the call graph for this function:

Here is the caller graph for this function:

const string positiveSymbolicNumber ( double  n)

Return symbolic or numerical representation of n>0.

Definition at line 219 of file doc_Text.cpp.

References addFraction(), and isSymbolicPower().

Referenced by symbolicNumber().

220 {
221  string s;
222  assert(n>0);
223 
224  // Try to find a symbolic representation
225 
226  for (int i=1;i<10;i++) {
227  for(int j=1;j<10;j++) {
228  if (isSymbolicPower(i*n/j,s)) {
229  return addFraction(j,i,s);
230  }
231  }
232  }
233 
234  // No symbolic representation,
235  // Then numerical representation x.10^k
236 
237  char tmp[64];
238  string entree = " * 10^{";
239  char sortie = '}';
240  string::size_type ps;
241 
242  snprintf(tmp, 63, "%.15g", n); // Warning: over 15 decimals, results are wrong !!
243  s = tmp;
244  ps = s.find('e');
245 
246  if (ps != string::npos) {
247  s.replace(ps, 1, "");
248  s.insert(ps, entree);
249  s += sortie;
250  }
251 
252  return s;
253 
254 }
bool isSymbolicPower(double n, string &s)
Return true if n>0 is equal to e^k or PI^k for some integer k The symbolic latex representation is re...
Definition: doc_Text.cpp:183
const string addFraction(int num, int denom, const string &exp)
Return exp or num.exp, or exp/denom, or num/denom.exp.
Definition: doc_Text.cpp:199

Here is the call graph for this function:

Here is the caller graph for this function:

const string symbolicNumber ( double  n)

Return symbolic or numerical representation of n.

Definition at line 260 of file doc_Text.cpp.

References positiveSymbolicNumber().

Referenced by docT().

261 {
262  if (n>0.0) {
263  return positiveSymbolicNumber(n);
264  } else if (n<0.0) {
265  return string("-") + positiveSymbolicNumber(-n);
266  } else {
267  return "0";
268  }
269 }
const string positiveSymbolicNumber(double n)
Return symbolic or numerical representation of n>0.
Definition: doc_Text.cpp:219

Here is the call graph for this function:

Here is the caller graph for this function:

Variable Documentation

bool gInternDoubleSwitch