Skip to content

Commit

Permalink
FEAT: faster trigonometric functions (without conversions and bounds …
Browse files Browse the repository at this point in the history
…checks)

While the original functions SINE, COSINE, TANGENT and ARCTANGENT works also with degrees and do better output formating, these new functions: SIN, COS, TAN and ATAN expects only value in radians and are directly mapped to libc trigonometric functions. They will be faster, but one must expect different output in some cases!

Difference example:

>> tangent/radians (pi / 2)
== 1.#INF

>> tan (pi / 2)
== 1.633123935319537e16

NOTE: these functions are available only with INFINITY support (needs 1.#NaN value)
  • Loading branch information
Oldes committed Jun 25, 2018
1 parent a54c697 commit 72d8155
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions src/core/n-math.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,97 @@ enum {SINE, COSINE, TANGENT};
}


// Follows faster trigonometric functions (without conversions and bounds checks)
#ifndef USE_NO_INFINITY //use these functions only with INFINITY support (cos/sin may return 1.#NaN value)!

/***********************************************************************
**
*/ REBNATIVE(cos)
/*
// cos: native [
// {Returns the trigonometric cosine.}
// value [decimal!] "In radians"
// ]
***********************************************************************/
{
SET_DECIMAL(D_RET, cos(VAL_DECIMAL(D_ARG(1))));
return R_RET;
}

/***********************************************************************
**
*/ REBNATIVE(sin)
/*
// sin: native [
// {Returns the trigonometric sine.}
// value [decimal!] "In radians"
// ]
***********************************************************************/
{
SET_DECIMAL(D_RET, sin(VAL_DECIMAL(D_ARG(1))));
return R_RET;
}

/***********************************************************************
**
*/ REBNATIVE(tan)
/*
// tan: native [
// {Returns the trigonometric tangent.}
// value [decimal!] "In radians"
// ]
***********************************************************************/
{
SET_DECIMAL(D_RET, tan(VAL_DECIMAL(D_ARG(1))));
return R_RET;
}

/***********************************************************************
**
*/ REBNATIVE(arctan)
/*
// arctan: native [
// {Returns the trigonometric arctangent.}
// value [decimal!] "In radians"
// ]
***********************************************************************/
{
SET_DECIMAL(D_RET, atan(VAL_DECIMAL(D_ARG(1))));
return R_RET;
}

/***********************************************************************
**
*/ REBNATIVE(asin)
/*
// asin: native [
// {Returns the trigonometric arcsine.}
// value [decimal!] "In radians"
// ]
***********************************************************************/
{
SET_DECIMAL(D_RET, asin(VAL_DECIMAL(D_ARG(1))));
return R_RET;
}

/***********************************************************************
**
*/ REBNATIVE(acos)
/*
// acos: native [
// {Returns the trigonometric arccosine.}
// value [decimal!] "In radians"
// ]
***********************************************************************/
{
SET_DECIMAL(D_RET, acos(VAL_DECIMAL(D_ARG(1))));
return R_RET;
}

#endif //!USE_NO_INFINITY



/***********************************************************************
**
*/ REBNATIVE(exp)
Expand Down

0 comments on commit 72d8155

Please sign in to comment.