From 6cadc6033d1024556b204645a19cbe59d7721941 Mon Sep 17 00:00:00 2001 From: Bernhard Schiffner Date: Sat, 6 Dec 2014 18:41:39 +0100 Subject: [PATCH 1/2] Make div an alias for idiv --- src/core/functions.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/core/functions.cpp b/src/core/functions.cpp index 5c57451d..4c598081 100644 --- a/src/core/functions.cpp +++ b/src/core/functions.cpp @@ -623,6 +623,12 @@ HNumber function_idiv(Function* f, const Function::ArgumentList& args) return HMath::idiv(args.at(0), args.at(1)); } +HNumber function_div(Function* f, const Function::ArgumentList& args) +{ + ENSURE_ARGUMENT_COUNT(2); + return HMath::idiv(args.at(0), args.at(1)); +} + HNumber function_mod(Function* f, const Function::ArgumentList& args) { ENSURE_ARGUMENT_COUNT(2); @@ -714,6 +720,7 @@ void FunctionRepo::createFunctions() FUNCTION_INSERT(shl); FUNCTION_INSERT(shr); FUNCTION_INSERT(idiv); + FUNCTION_INSERT(div); FUNCTION_INSERT(mod); } @@ -822,6 +829,7 @@ void FunctionRepo::setTranslatableFunctionUsages() FUNCTION_USAGE_TR(binommean, tr("trials; probability")); FUNCTION_USAGE_TR(binompmf, tr("hits; trials; probability")); FUNCTION_USAGE_TR(binomvar, tr("trials; probability")); + FUNCTION_USAGE_TR(div, tr("dividend; divisor")); FUNCTION_USAGE_TR(hypercdf, tr("max; total; hits; trials")); FUNCTION_USAGE_TR(hypermean, tr("total; hits; trials")); FUNCTION_USAGE_TR(hyperpmf, tr("count; total; hits; trials")); @@ -864,6 +872,7 @@ void FunctionRepo::setFunctionNames() FUNCTION_NAME(csc, tr("Cosecant")); FUNCTION_NAME(dec, tr("Decimal Representation")); FUNCTION_NAME(degrees, tr("Degrees of Arc")); + FUNCTION_NAME(div, tr("Integer Quotient")); FUNCTION_NAME(erf, tr("Error Function")); FUNCTION_NAME(erfc, tr("Complementary Error Function")); FUNCTION_NAME(exp, tr("Exponential")); From 2ead406f34720ba8424f9ca57dfa92b0d0b957fb Mon Sep 17 00:00:00 2001 From: Bernhard Schiffner Date: Sat, 6 Dec 2014 18:50:23 +0100 Subject: [PATCH 2/2] An implementation of arctan2 --- src/core/functions.cpp | 12 ++++++++++++ src/math/floathmath.c | 15 +++++++++++++++ src/math/floathmath.h | 6 ++++++ src/math/floatnum.h | 2 +- src/math/hmath.cpp | 26 ++++++++++++++++++++++++++ src/math/hmath.h | 3 ++- 6 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/core/functions.cpp b/src/core/functions.cpp index 4c598081..cd46fd84 100644 --- a/src/core/functions.cpp +++ b/src/core/functions.cpp @@ -323,6 +323,15 @@ HNumber function_arctan(Function* f, const Function::ArgumentList& args) return result; } +HNumber function_arctan2(Function* f, const Function::ArgumentList& args) +{ + ENSURE_ARGUMENT_COUNT(2); + HNumber result = HMath::arctan2(args.at(0), args.at(1)); + if (Settings::instance()->angleUnit == 'd') + result = HMath::rad2deg(result); + return result; +} + HNumber function_sinh(Function* f, const Function::ArgumentList& args) { ENSURE_ARGUMENT_COUNT(1); @@ -693,6 +702,7 @@ void FunctionRepo::createFunctions() FUNCTION_INSERT(artanh); FUNCTION_INSERT(arcsin); FUNCTION_INSERT(arctan); + FUNCTION_INSERT(arctan2); FUNCTION_INSERT(cos); FUNCTION_INSERT(cosh); FUNCTION_INSERT(cot); @@ -774,6 +784,7 @@ void FunctionRepo::setNonTranslatableFunctionUsages() FUNCTION_USAGE(artanh, "x"); FUNCTION_USAGE(arcsin, "x"); FUNCTION_USAGE(arctan, "x"); + FUNCTION_USAGE(arctan2, "y; x"); FUNCTION_USAGE(average, "x1; x2; ..."); FUNCTION_USAGE(bin, "n"); FUNCTION_USAGE(cbrt, "x"); @@ -858,6 +869,7 @@ void FunctionRepo::setFunctionNames() FUNCTION_NAME(artanh, tr("Area Hyperbolic Tangent")); FUNCTION_NAME(arcsin, tr("Arc Sine")); FUNCTION_NAME(arctan, tr("Arc Tangent")); + FUNCTION_NAME(arctan2, tr("Arc Tangent from Cathedus")); FUNCTION_NAME(average, tr("Average (Arithmetic Mean)")); FUNCTION_NAME(bin, tr("Binary Representation")); FUNCTION_NAME(binomcdf, tr("Binomial Cumulative Distribution Function")); diff --git a/src/math/floathmath.c b/src/math/floathmath.c index 42251ac4..502664cd 100644 --- a/src/math/floathmath.c +++ b/src/math/floathmath.c @@ -318,6 +318,21 @@ float_arctan( return 1; } +char +float_arctan2( + floatnum x, + floatnum y, + int digits) +{ + if (!chckmathparam(x, digits)) + return 0; + if (!chckmathparam(y, digits)) + return 0; + float_div(x, x, y, digits); + _arctan(x, digits); + return 1; +} + char float_arcsin( floatnum x, diff --git a/src/math/floathmath.h b/src/math/floathmath.h index 76fa428f..1ad41000 100644 --- a/src/math/floathmath.h +++ b/src/math/floathmath.h @@ -176,6 +176,12 @@ char float_power10(floatnum x, int digits); InvalidPrecision (digits > MATHPRECISION) */ char float_arctan(floatnum x, int digits); +/* evaluates arctan of arguments, resulting in a full circle + -pi MATHPRECISION) */ +char float_arctan2(floatnum y, floatnum x, int digits); + /* evaluates arcsin x for -1 <= x <= 1, yielding a result -pi/2 <= result <= pi/2. In case of an error, x is set to NaN and 0 is returned. diff --git a/src/math/floatnum.h b/src/math/floatnum.h index 24ef12ee..f0d354aa 100644 --- a/src/math/floatnum.h +++ b/src/math/floatnum.h @@ -118,7 +118,7 @@ void float_create(floatnum f); any subsequent arithmetic operation on this variable will fail. However, a variable such finalized can still be re-used without prior initialization, by making it the destination of an operation. - If you wish to deliberately "empty" a variable, without detroying it, + If you wish to deliberately "empty" a variable, without destroying it, call this function. An alias "float_free" to this function is defined that you may use anywhere as a replacement for float_setnan. diff --git a/src/math/hmath.cpp b/src/math/hmath.cpp index e1dfcc08..a7752c4b 100644 --- a/src/math/hmath.cpp +++ b/src/math/hmath.cpp @@ -1232,6 +1232,32 @@ HNumber HMath::arctan( const HNumber & x ) return result; }; +/** + * Returns the arc tangent calculated from the two cathedus. + * It's calculated from (opposite leg / adjacent leg) and + * results in a full circle range (-Pi;Pi) depending on + * the sign of the respective legs. + * Both legs can not be 0 the same time. + */ +HNumber HMath::arctan2( const HNumber & x, const HNumber & y ) +{ +/* x: opposite leg; y: adjacent leg */ + Error error = checkNaNParam(*x.d, y.d); + if (error != Success) + return HMath::nan(error); + + if (x.isZero() && y.isZero()) return HMath::nan(); + if (x.isPositive() && y.isZero()) return (HMath::pi()/2.0); + if (x.isNegative() && y.isZero()) return (0.0-(HMath::pi()/2.0)); + + HNumber offset = HNumber(0.0); + /* 2. Quadrant */ + if (x.isPositive() && y.isNegative()) offset+=HMath::pi(); + /* 3. Quadrant */ + if (x.isNegative() && y.isNegative()) offset-=HMath::pi(); + return (arctan(x/y)+offset); +} + /** * Returns the arc sine of x. */ diff --git a/src/math/hmath.h b/src/math/hmath.h index 2c33cf23..24365ec8 100644 --- a/src/math/hmath.h +++ b/src/math/hmath.h @@ -144,6 +144,7 @@ class HMath static HNumber arcsin( const HNumber & x ); static HNumber arccos( const HNumber & x ); static HNumber arctan( const HNumber & x ); + static HNumber arctan2( const HNumber & x, const HNumber & y ); // HIGHER MATH FUNCTIONS static HNumber factorial( const HNumber & x, const HNumber & base = HNumber(1) ); static HNumber gamma( const HNumber & x); @@ -173,4 +174,4 @@ class HMath std::ostream & operator<<( std::ostream &, const HNumber & ); -#endif \ No newline at end of file +#endif