Skip to content

Commit 72d8155

Browse files
committed
FEAT: faster trigonometric functions (without conversions and bounds 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)
1 parent a54c697 commit 72d8155

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

src/core/n-math.c

+91
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,97 @@ enum {SINE, COSINE, TANGENT};
184184
}
185185

186186

187+
// Follows faster trigonometric functions (without conversions and bounds checks)
188+
#ifndef USE_NO_INFINITY //use these functions only with INFINITY support (cos/sin may return 1.#NaN value)!
189+
190+
/***********************************************************************
191+
**
192+
*/ REBNATIVE(cos)
193+
/*
194+
// cos: native [
195+
// {Returns the trigonometric cosine.}
196+
// value [decimal!] "In radians"
197+
// ]
198+
***********************************************************************/
199+
{
200+
SET_DECIMAL(D_RET, cos(VAL_DECIMAL(D_ARG(1))));
201+
return R_RET;
202+
}
203+
204+
/***********************************************************************
205+
**
206+
*/ REBNATIVE(sin)
207+
/*
208+
// sin: native [
209+
// {Returns the trigonometric sine.}
210+
// value [decimal!] "In radians"
211+
// ]
212+
***********************************************************************/
213+
{
214+
SET_DECIMAL(D_RET, sin(VAL_DECIMAL(D_ARG(1))));
215+
return R_RET;
216+
}
217+
218+
/***********************************************************************
219+
**
220+
*/ REBNATIVE(tan)
221+
/*
222+
// tan: native [
223+
// {Returns the trigonometric tangent.}
224+
// value [decimal!] "In radians"
225+
// ]
226+
***********************************************************************/
227+
{
228+
SET_DECIMAL(D_RET, tan(VAL_DECIMAL(D_ARG(1))));
229+
return R_RET;
230+
}
231+
232+
/***********************************************************************
233+
**
234+
*/ REBNATIVE(arctan)
235+
/*
236+
// arctan: native [
237+
// {Returns the trigonometric arctangent.}
238+
// value [decimal!] "In radians"
239+
// ]
240+
***********************************************************************/
241+
{
242+
SET_DECIMAL(D_RET, atan(VAL_DECIMAL(D_ARG(1))));
243+
return R_RET;
244+
}
245+
246+
/***********************************************************************
247+
**
248+
*/ REBNATIVE(asin)
249+
/*
250+
// asin: native [
251+
// {Returns the trigonometric arcsine.}
252+
// value [decimal!] "In radians"
253+
// ]
254+
***********************************************************************/
255+
{
256+
SET_DECIMAL(D_RET, asin(VAL_DECIMAL(D_ARG(1))));
257+
return R_RET;
258+
}
259+
260+
/***********************************************************************
261+
**
262+
*/ REBNATIVE(acos)
263+
/*
264+
// acos: native [
265+
// {Returns the trigonometric arccosine.}
266+
// value [decimal!] "In radians"
267+
// ]
268+
***********************************************************************/
269+
{
270+
SET_DECIMAL(D_RET, acos(VAL_DECIMAL(D_ARG(1))));
271+
return R_RET;
272+
}
273+
274+
#endif //!USE_NO_INFINITY
275+
276+
277+
187278
/***********************************************************************
188279
**
189280
*/ REBNATIVE(exp)

0 commit comments

Comments
 (0)