mirror of
https://github.com/lcn2/calc.git
synced 2025-08-16 01:03:29 +03:00
add cmappr() and missing complex tan, cot, sec, csc in liblcac
Added complex multiple approximation function to commath.c so that users of libcalc may directly round complex number to nearest multiple of a given real number: E_FUNC COMPLEX *cmappr(COMPLEX *c, NUMBER *e, long rnd, bool cfree); For example: COMPLEX *c; /* complex number to round to nearest epsilon */ NUMBER *eps; /* epsilon rounding precision */ COMPLEX *res; /* c rounded to nearest epsilon */ long rnd = 24L; /* a common rounding mode */ bool ok_to_free; /* true ==> free c, false ==> do not free c */ ... res = cmappr(c, eps, ok_to_free); The complex trigonometric functions tan, cot, sec, csc were implemented in func.c as calls to complex sin and complex cos. We added the direct calls to comfunc.c so that users of libcalc may call them directly: E_FUNC COMPLEX *c_tan(COMPLEX *c, NUMBER *eps); E_FUNC COMPLEX *c_cot(COMPLEX *c, NUMBER *eps); E_FUNC COMPLEX *c_sec(COMPLEX *c, NUMBER *eps); E_FUNC COMPLEX *c_cot(COMPLEX *c, NUMBER *eps);
This commit is contained in:
320
comfunc.c
320
comfunc.c
@@ -878,6 +878,96 @@ c_acosh(COMPLEX *c, NUMBER *epsilon)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* c_tan - complex trigonometric tangent
|
||||
*
|
||||
* This uses the formula:
|
||||
*
|
||||
* tan(x) = sin(x) / cos(x)
|
||||
*
|
||||
* given:
|
||||
* c argument to the trigonometric function
|
||||
* epsilon precision of the trigonometric calculation
|
||||
*
|
||||
* returns:
|
||||
* != NULL ==> allocated pointer to COMPLEX result
|
||||
* NULL ==> invalid trigonometric argument
|
||||
*
|
||||
* NOTE: When the trigonometric result is returned as non-NULL result,
|
||||
* the value may be a real value. The caller may wish to:
|
||||
*
|
||||
* COMPLEX *c; (* return result of this function *)
|
||||
* NUMBER *q; (* COMPLEX result when c is a real number *)
|
||||
*
|
||||
* if (c == NULL) {
|
||||
* math_error("... some error message");
|
||||
* not_reached();
|
||||
* }
|
||||
* if (cisreal(c)) {
|
||||
* q = c_to_q(c, ok_to_free);
|
||||
* }
|
||||
*/
|
||||
COMPLEX *
|
||||
c_tan(COMPLEX *c, NUMBER *epsilon)
|
||||
{
|
||||
COMPLEX *denom; /* trigonometric identity numerator */
|
||||
COMPLEX *numer; /* trigonometric identity denominator */
|
||||
COMPLEX *res; /* trigonometric result */
|
||||
|
||||
/*
|
||||
* firewall - check args
|
||||
*/
|
||||
if (c == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (check_epsilon(epsilon) == false) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* evaluate the cos(x) denominator
|
||||
*
|
||||
* Return NULL if cos(x) failed or we otherwise divide by zero.
|
||||
*/
|
||||
denom = c_cos(c, epsilon);
|
||||
if (denom == NULL || ciszero(denom)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* evaluate the sin(x) numerator
|
||||
*
|
||||
* Return NULL if sin(x) failed.
|
||||
*/
|
||||
numer = c_sin(c, epsilon);
|
||||
if (numer == NULL) {
|
||||
comfree(denom);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* catch the special case of numerator of 0
|
||||
*/
|
||||
if (ciszero(numer)) {
|
||||
comfree(denom);
|
||||
comfree(numer);
|
||||
return clink(&_czero_);
|
||||
}
|
||||
|
||||
/*
|
||||
* compute the trigonometric function value
|
||||
*/
|
||||
res = c_div(numer, denom);
|
||||
comfree(denom);
|
||||
comfree(numer);
|
||||
|
||||
/*
|
||||
* return the trigonometric result
|
||||
*/
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
COMPLEX *
|
||||
c_atan(COMPLEX *c, NUMBER *epsilon)
|
||||
{
|
||||
@@ -900,6 +990,96 @@ c_atan(COMPLEX *c, NUMBER *epsilon)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* c_cot - complex trigonometric cotangent
|
||||
*
|
||||
* This uses the formula:
|
||||
*
|
||||
* cot(x) = cos(x) / sin(x)
|
||||
*
|
||||
* given:
|
||||
* c argument to the trigonometric function
|
||||
* epsilon precision of the trigonometric calculation
|
||||
*
|
||||
* returns:
|
||||
* != NULL ==> allocated pointer to COMPLEX result
|
||||
* NULL ==> invalid trigonometric argument
|
||||
*
|
||||
* NOTE: When the trigonometric result is returned as non-NULL result,
|
||||
* the value may be a real value. The caller may wish to:
|
||||
*
|
||||
* COMPLEX *c; (* return result of this function *)
|
||||
* NUMBER *q; (* COMPLEX result when c is a real number *)
|
||||
*
|
||||
* if (c == NULL) {
|
||||
* math_error("... some error message");
|
||||
* not_reached();
|
||||
* }
|
||||
* if (cisreal(c)) {
|
||||
* q = c_to_q(c, ok_to_free);
|
||||
* }
|
||||
*/
|
||||
COMPLEX *
|
||||
c_cot(COMPLEX *c, NUMBER *epsilon)
|
||||
{
|
||||
COMPLEX *denom; /* trigonometric identity numerator */
|
||||
COMPLEX *numer; /* trigonometric identity denominator */
|
||||
COMPLEX *res; /* trigonometric result */
|
||||
|
||||
/*
|
||||
* firewall - check args
|
||||
*/
|
||||
if (c == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (check_epsilon(epsilon) == false) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* evaluate the sin(x) denominator
|
||||
*
|
||||
* Return NULL if sin(x) failed or we otherwise divide by zero.
|
||||
*/
|
||||
denom = c_sin(c, epsilon);
|
||||
if (denom == NULL || ciszero(denom)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* evaluate the cos(x) numerator
|
||||
*
|
||||
* Return NULL if cos(x) failed.
|
||||
*/
|
||||
numer = c_cos(c, epsilon);
|
||||
if (numer == NULL) {
|
||||
comfree(denom);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* catch the special case of numerator of 0
|
||||
*/
|
||||
if (ciszero(numer)) {
|
||||
comfree(denom);
|
||||
comfree(numer);
|
||||
return clink(&_czero_);
|
||||
}
|
||||
|
||||
/*
|
||||
* compute the trigonometric function value
|
||||
*/
|
||||
res = c_div(numer, denom);
|
||||
comfree(denom);
|
||||
comfree(numer);
|
||||
|
||||
/*
|
||||
* return the trigonometric result
|
||||
*/
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
COMPLEX *
|
||||
c_acot(COMPLEX *c, NUMBER *epsilon)
|
||||
{
|
||||
@@ -921,6 +1101,75 @@ c_acot(COMPLEX *c, NUMBER *epsilon)
|
||||
return tmp1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* c_sec - complex trigonometric tangent
|
||||
*
|
||||
* This uses the formula:
|
||||
*
|
||||
* sec(x) = 1 / cos(x)
|
||||
*
|
||||
* given:
|
||||
* c argument to the trigonometric function
|
||||
* epsilon precision of the trigonometric calculation
|
||||
*
|
||||
* returns:
|
||||
* != NULL ==> allocated pointer to COMPLEX result
|
||||
* NULL ==> invalid trigonometric argument
|
||||
*
|
||||
* NOTE: When the trigonometric result is returned as non-NULL result,
|
||||
* the value may be a real value. The caller may wish to:
|
||||
*
|
||||
* COMPLEX *c; (* return result of this function *)
|
||||
* NUMBER *q; (* COMPLEX result when c is a real number *)
|
||||
*
|
||||
* if (c == NULL) {
|
||||
* math_error("... some error message");
|
||||
* not_reached();
|
||||
* }
|
||||
* if (cisreal(c)) {
|
||||
* q = c_to_q(c, ok_to_free);
|
||||
* }
|
||||
*/
|
||||
COMPLEX *
|
||||
c_sec(COMPLEX *c, NUMBER *epsilon)
|
||||
{
|
||||
COMPLEX *denom; /* trigonometric identity numerator */
|
||||
COMPLEX *res; /* trigonometric result */
|
||||
|
||||
/*
|
||||
* firewall - check args
|
||||
*/
|
||||
if (c == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (check_epsilon(epsilon) == false) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* evaluate the cos(x) denominator
|
||||
*
|
||||
* Return NULL if cos(x) failed or we otherwise divide by zero.
|
||||
*/
|
||||
denom = c_cos(c, epsilon);
|
||||
if (denom == NULL || ciszero(denom)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* compute the trigonometric function value
|
||||
*/
|
||||
res = c_div(&_cone_, denom);
|
||||
comfree(denom);
|
||||
|
||||
/*
|
||||
* return the trigonometric result
|
||||
*/
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
COMPLEX *
|
||||
c_asec(COMPLEX *c, NUMBER *epsilon)
|
||||
{
|
||||
@@ -932,6 +1181,75 @@ c_asec(COMPLEX *c, NUMBER *epsilon)
|
||||
return tmp2;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* c_sec - complex trigonometric cosecant
|
||||
*
|
||||
* This uses the formula:
|
||||
*
|
||||
* csc(x) = 1 / sin(x)
|
||||
*
|
||||
* given:
|
||||
* c argument to the trigonometric function
|
||||
* epsilon precision of the trigonometric calculation
|
||||
*
|
||||
* returns:
|
||||
* != NULL ==> allocated pointer to COMPLEX result
|
||||
* NULL ==> invalid trigonometric argument
|
||||
*
|
||||
* NOTE: When the trigonometric result is returned as non-NULL result,
|
||||
* the value may be a real value. The caller may wish to:
|
||||
*
|
||||
* COMPLEX *c; (* return result of this function *)
|
||||
* NUMBER *q; (* COMPLEX result when c is a real number *)
|
||||
*
|
||||
* if (c == NULL) {
|
||||
* math_error("... some error message");
|
||||
* not_reached();
|
||||
* }
|
||||
* if (cisreal(c)) {
|
||||
* q = c_to_q(c, ok_to_free);
|
||||
* }
|
||||
*/
|
||||
COMPLEX *
|
||||
c_csc(COMPLEX *c, NUMBER *epsilon)
|
||||
{
|
||||
COMPLEX *denom; /* trigonometric identity numerator */
|
||||
COMPLEX *res; /* trigonometric result */
|
||||
|
||||
/*
|
||||
* firewall - check args
|
||||
*/
|
||||
if (c == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (check_epsilon(epsilon) == false) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* evaluate the sin(x) denominator
|
||||
*
|
||||
* Return NULL if sin(x) failed or we otherwise divide by zero.
|
||||
*/
|
||||
denom = c_sin(c, epsilon);
|
||||
if (denom == NULL || ciszero(denom)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* compute the trigonometric function value
|
||||
*/
|
||||
res = c_div(&_cone_, denom);
|
||||
comfree(denom);
|
||||
|
||||
/*
|
||||
* return the trigonometric result
|
||||
*/
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
COMPLEX *
|
||||
c_acsc(COMPLEX *c, NUMBER *epsilon)
|
||||
{
|
||||
@@ -983,6 +1301,7 @@ c_acoth(COMPLEX *c, NUMBER *epsilon)
|
||||
return tmp2;
|
||||
}
|
||||
|
||||
|
||||
COMPLEX *
|
||||
c_asech(COMPLEX *c, NUMBER *epsilon)
|
||||
{
|
||||
@@ -994,6 +1313,7 @@ c_asech(COMPLEX *c, NUMBER *epsilon)
|
||||
return tmp2;
|
||||
}
|
||||
|
||||
|
||||
COMPLEX *
|
||||
c_acsch(COMPLEX *c, NUMBER *epsilon)
|
||||
{
|
||||
|
Reference in New Issue
Block a user