|
FAUST compiler
0.9.9.6b8
|
#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"
Go to the source code of this file.
Defines | |
| #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. | |
| 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. | |
| double | dblEpsilon () |
| Compute the smallest double representable difference epsilon such that 1 != 1+epsilon. | |
| static bool | AlmostEqual (double A, double B) |
| Check if two floating point numbers are (almost) equal Abs(x-y) < epsilon. | |
| bool | isPiPower (double n, string &s) |
| Return true if n>0 is equal to PI^k for some small integer k. | |
| bool | isExpPower (double n, string &s) |
| Return true if n>0 is equal to e^k for some small integer k. | |
| 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. | |
| const string | addFraction (int num, int denom, const string &exp) |
| Return exp or num.exp, or exp/denom, or num/denom.exp. | |
| const string | positiveSymbolicNumber (double n) |
| Return symbolic or numerical representation of n>0. | |
Variables | |
| bool | gInternDoubleSwitch |
| #define M_E 2.71828182845904523536 |
Definition at line 50 of file doc_Text.cpp.
| #define M_PI 3.14159265358979323846 |
Definition at line 38 of file doc_Text.cpp.
Referenced by isPiPower().
| #define M_PI_2 1.57079632679489661923 |
Definition at line 42 of file doc_Text.cpp.
| #define M_PI_4 0.785398163397448309616 |
Definition at line 46 of file doc_Text.cpp.
| 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 198 of file doc_Text.cpp.
Referenced by positiveSymbolicNumber().
{
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();
}

| static bool AlmostEqual | ( | double | A, |
| double | B | ||
| ) | [static] |
Check if two floating point numbers are (almost) equal Abs(x-y) < epsilon.
Definition at line 118 of file doc_Text.cpp.
References dblEpsilon().
Referenced by isExpPower(), and isPiPower().
{
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;
}


| double dblEpsilon | ( | ) |
Compute the smallest double representable difference epsilon such that 1 != 1+epsilon.
Definition at line 104 of file doc_Text.cpp.
Referenced by AlmostEqual().
{
double machEps = 1.0f;
do {
machEps /= 2.0f;
} while ((1.0 + (machEps/2.0)) != 1.0);
return machEps;
}

| string docT | ( | char * | c | ) |
Definition at line 76 of file doc_Text.cpp.
Referenced by DocCompiler::compileLateq(), DocCompiler::generateCode(), DocCompiler::generateIota(), DocCompiler::getFreshID(), and DocCompiler::prepareIntervallicUI().
{ return string(c); }

| string docT | ( | int | n | ) |
Definition at line 77 of file doc_Text.cpp.
{ char c[64]; snprintf(c, 63, "%d",n); return string(c); }
| string docT | ( | long | n | ) |
Definition at line 78 of file doc_Text.cpp.
{ char c[64]; snprintf(c, 63, "%ld",n); return string(c); }
| string docT | ( | double | n | ) |
Definition at line 79 of file doc_Text.cpp.
References symbolicNumber().
{ return symbolicNumber(n); }

| float fltEpsilon | ( | ) |
Compute the smallest float representable difference epsilon such that 1 != 1+epsilon.
Definition at line 90 of file doc_Text.cpp.
{
float machEps = 1.0f;
do {
machEps /= 2.0f;
} while ((float)(1.0 + (machEps/2.0)) != 1.0);
return machEps;
}
| 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 162 of file doc_Text.cpp.
References abs(), and AlmostEqual().
Referenced by isSymbolicPower().
{
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;
}
}


| 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 142 of file doc_Text.cpp.
References abs(), AlmostEqual(), and M_PI.
Referenced by isSymbolicPower().
{
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;
}
}


| 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 182 of file doc_Text.cpp.
References isExpPower(), and isPiPower().
Referenced by positiveSymbolicNumber().
{
assert(n>0);
if (isPiPower(n,s)) {
return true;
} else if (isExpPower(n,s)) {
return true;
} else {
return false;
}
}


| const string positiveSymbolicNumber | ( | double | n | ) |
Return symbolic or numerical representation of n>0.
Definition at line 218 of file doc_Text.cpp.
References addFraction(), and isSymbolicPower().
Referenced by symbolicNumber().
{
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;
}


| const string symbolicNumber | ( | double | n | ) |
Return symbolic or numerical representation of n.
Definition at line 259 of file doc_Text.cpp.
References positiveSymbolicNumber().
Referenced by docT().
{
if (n>0.0) {
return positiveSymbolicNumber(n);
} else if (n<0.0) {
return string("-") + positiveSymbolicNumber(-n);
} else {
return "0";
}
}


| bool gInternDoubleSwitch |
1.8.0