#include <assert.h> // For test()
#include <math.h>
#include <errno.h>
-#include "fparser.h"
+#include "fparser.hh"
#include "debug.h"
/**
* Rounds the given double to the next int.
*/
-int RS_Math::round(double v)
+int Math::round(double v)
{
return (v - floor(v) < 0.5 ? (int)floor(v) : (int)ceil(v));
}
/**
* Save pow function
*/
-double RS_Math::pow(double x, double y)
+double Math::pow(double x, double y)
{
errno = 0;
double ret = ::pow(x, y);
if (errno == EDOM)
{
- RS_DEBUG->print(RS_Debug::D_ERROR, "RS_Math::pow: EDOM in pow");
+ DEBUG->print(Debug::D_ERROR, "Math::pow: EDOM in pow");
ret = 0.0;
}
else if (errno == ERANGE)
{
- RS_DEBUG->print(RS_Debug::D_WARNING, "RS_Math::pow: ERANGE in pow");
+ DEBUG->print(Debug::D_WARNING, "Math::pow: ERANGE in pow");
ret = 0.0;
}
/**
* Converts radians to degrees.
*/
-double RS_Math::rad2deg(double a)
+double Math::rad2deg(double a)
{
return (a / (2.0 * M_PI) * 360.0);
}
/**
* Converts degrees to radians.
*/
-double RS_Math::deg2rad(double a)
+double Math::deg2rad(double a)
{
return ((a / 360.0) * (2.0 * M_PI));
}
/**
* Converts radians to gradians.
*/
-double RS_Math::rad2gra(double a)
+double Math::rad2gra(double a)
{
return (a / (2.0 * M_PI) * 400.0);
}
/**
* Finds greatest common divider using Euclid's algorithm.
*/
-int RS_Math::findGCD(int a, int b)
+int Math::findGCD(int a, int b)
{
while (b != 0)
{
* @param reversed true for clockwise testing. false for ccw testing.
* @return true if the angle a is between a1 and a2.
*/
-bool RS_Math::isAngleBetween(double a, double a1, double a2, bool reversed)
+bool Math::isAngleBetween(double a, double a1, double a2, bool reversed)
{
bool ret = false;
ret = true;
}
- //RS_DEBUG->print("angle %f is %sbetween %f and %f",
+ //DEBUG->print("angle %f is %sbetween %f and %f",
// a, ret ? "" : "not ", a1, a2);
return ret;
}
/**
* Corrects the given angle to the range of 0-2*Pi.
*/
-double RS_Math::correctAngle(double a)
+double Math::correctAngle(double a)
{
while (a > 2 * M_PI)
a -= 2 * M_PI;
* @return The angle that needs to be added to a1 to reach a2.
* Always positive and less than 2*pi.
*/
-double RS_Math::getAngleDifference(double a1, double a2)
+double Math::getAngleDifference(double a1, double a2)
{
double ret;
* @return The given angle or the given angle+PI, depending which on
* is readable from the bottom or right.
*/
-double RS_Math::makeAngleReadable(double angle, bool readable, bool * corrected)
+double Math::makeAngleReadable(double angle, bool readable, bool * corrected)
{
double ret;
bool cor = isAngleReadable(angle) ^ readable;
* @return true: if the given angle is in a range that is readable
* for texts created with that angle.
*/
-bool RS_Math::isAngleReadable(double angle)
+bool Math::isAngleReadable(double angle)
{
if (angle > M_PI /2.0 * 3.0 + 0.001 || angle < M_PI / 2.0 + 0.001)
return true;
* @param tol Tolerance in rad.
* @retval true The two angles point in the same direction.
*/
-bool RS_Math::isSameDirection(double dir1, double dir2, double tol)
+bool Math::isSameDirection(double dir1, double dir2, double tol)
{
double diff = fabs(dir1 - dir2);
/**
* Compares two double values with a tolerance.
*/
-bool RS_Math::cmpDouble(double v1, double v2, double tol)
+bool Math::cmpDouble(double v1, double v2, double tol)
{
return (fabs(v2 - v1) < tol);
}
* Evaluates a mathematical expression and returns the result.
* If an error occured, the given default value 'def' will be returned.
*/
-double RS_Math::eval(const QString & expr, double def)
+double Math::eval(const QString & expr, double def)
{
bool ok;
- double res = RS_Math::eval(expr, &ok);
+ double res = Math::eval(expr, &ok);
if (!ok)
return def;
* Evaluates a mathematical expression and returns the result.
* If an error occured, ok will be set to false (if ok isn't NULL).
*/
-double RS_Math::eval(const QString & expr, bool * ok)
+double Math::eval(const QString & expr, bool * ok)
{
if (expr.isEmpty())
{
* @param prec Precision e.g. a precision of 1 would mean that a
* value of 2.12030 will be converted to "2.1". 2.000 is always just "2").
*/
-QString RS_Math::doubleToString(double value, double prec)
+QString Math::doubleToString(double value, double prec)
{
if (prec < 1.0e-12)
{
- std::cerr << "RS_Math::doubleToString: invalid precision\n";
+ std::cerr << "Math::doubleToString: invalid precision\n";
return "";
}
QString ret;
- int num = RS_Math::round(value / prec);
+ int num = Math::round(value / prec);
- QString exaStr = RS_Math::doubleToString(prec, 10);
+ QString exaStr = Math::doubleToString(prec, 10);
int dotPos = exaStr.indexOf('.');
if (dotPos == -1)
- ret.sprintf("%d", RS_Math::round(num * prec));
+ ret.sprintf("%d", Math::round(num * prec));
else
{
int digits = exaStr.length() - dotPos - 1;
- ret = RS_Math::doubleToString(num * prec, digits);
+ ret = Math::doubleToString(num * prec, digits);
}
return ret;
* @param value The double value
* @param prec Precision
*/
-QString RS_Math::doubleToString(double value, int prec)
+QString Math::doubleToString(double value, int prec)
{
QString valStr;
/**
* Performs some testing for the math class.
*/
-void RS_Math::test()
+void Math::test()
{
- std::cout << "RS_Math::test: doubleToString:\n";
+ std::cout << "Math::test: doubleToString:\n";
double v = 0.1;
- QString s = RS_Math::doubleToString(v, 0.1);
+ QString s = Math::doubleToString(v, 0.1);
assert(s == "0.1");
- s = RS_Math::doubleToString(v, 0.01);
+ s = Math::doubleToString(v, 0.01);
assert(s == "0.1");
- s = RS_Math::doubleToString(v, 0.0);
+ s = Math::doubleToString(v, 0.0);
assert(s == "0");
v = 0.01;
- s = RS_Math::doubleToString(v, 0.1);
+ s = Math::doubleToString(v, 0.1);
assert(s == "0");
- s = RS_Math::doubleToString(v, 0.01);
+ s = Math::doubleToString(v, 0.01);
assert(s == "0.01");
- s = RS_Math::doubleToString(v, 0.0);
+ s = Math::doubleToString(v, 0.0);
assert(s == "0");
v = 0.001;
- s = RS_Math::doubleToString(v, 0.1);
+ s = Math::doubleToString(v, 0.1);
assert(s == "0");
- s = RS_Math::doubleToString(v, 0.01);
+ s = Math::doubleToString(v, 0.01);
assert(s == "0");
- s = RS_Math::doubleToString(v, 0.001);
+ s = Math::doubleToString(v, 0.001);
assert(s == "0.001");
- s = RS_Math::doubleToString(v, 0.0);
+ s = Math::doubleToString(v, 0.0);
assert(s == "0");
- std::cout << "RS_Math::test: complete\n";
+ std::cout << "Math::test: complete\n";
}