Math Functions

There are a lot of built-in mathematical functions in Silk. All the functions take double as an argument and return double as the result.

1. _fun("math_acos", x)
math_acos returns the arc cosine of x in radians.

2. _fun("math_asin", x)
math_asin returns the arc sine of x in radians.

3. _fun("math_atan", x)
math_atan returns the arc tangent of x in radians.

4. _fun("math_atan2", y, x)
math_atan2 returns the arc tangent in radians of y/x based on the signs of both values to determine the correct quadrant.

5. _fun("math_cos", x)
math_cos returns the cosine of a radian angle x.

6. _fun("math_cosh", x)
math_cosh returns the hyperbolic cosine of x.

7. _fun("math_sin", x)
math_sin returns the sine of a radian angle x.

8. _fun("math_sinh", x)
math_sinh returns the hyperbolic sine of x.

9. _fun("math_tanh", x)
math_tanh returns the hyperbolic tangent of x.

10. _fun("math_exp", x)
math_exp returns the value of e raised to the xth power.

11. _fun("math_frexp", x)
The returned value is an array which has 2 elements, the first one is the mantissa and the second one is the exponent. 

12. _fun("math_ldexp", x, e)
math_ldexp returns x multiplied by 2 raised to the power of e.

13. _fun("math_log", x)
math_log returns the natural logarithm (base-e logarithm) of x.

14. _fun("math_log10", x)
math_log10 returns the common logarithm (base-10 logarithm) of x.

15. _fun("math_modf", x)
The returned value is an array which has 2 elements, the first one is the integer component, and the second one is the fraction component (part after the decimal).

16. _fun("math_pow", x, y)
math_pow returns x raised to the power of y.

17. _fun("math_sqrt", x)
math_sqrt returns the square root of x.

18. _fun("math_fabs", x)
math_fabs returns the absolute value of x.。

19. _fun("math_floor", x)
math_floor returns the largest integer value less than or equal to x.

20. _fun("math_ceil", x)
math_ceil returns the smallest integer value greater than or equal to x.

21. _fun("math_fmod", x, y)
math_fmod returns the remainder of x divided by y.

Here are some samples for math functions:
main()
{
    PI=3.14159265;

    x = 60.0;
    val = PI / 180.0;
    ret = _fun("math_cos", x*val );
    printf("The cos of %lf is %lf \n", x, ret);

    x = 45.0;
    val = PI / 180;
    ret =  _fun("math_sin", x*val );
    printf("The sin of %lf is %lf \n", x, ret);

    x = 0.65;
    n = 3;
    ret =  _fun("math_ldexp",x ,n);
    printf("%f * 2^%d = %f\n", x, n, ret); 

    x = 1024;
    ret =  _fun("math_frexp",x);
    printf("x = %d = %.2lf * 2^%d\n", x, ret[0], ret[1]);

    x = 8.123456;
    ret =  _fun("math_modf",x);
    printf("The part of interger = %lf\n", ret[0]);
    printf("The part of fraction = %lf \n", ret[1]);  

    ret=_fun("math_pow",8,3);
    printf("8 ^ 3 = %lf\n", ret);

    ret=_fun("math_fabs",-3.05);
    printf("fabs(-3.05) = %lf\n",ret);  
    
    x = 2.3;
    ret=_fun("math_log",x);
    printf("log(%lf) = %lf\n", x, ret);

    x = 100000;
    ret=_fun("math_log10",x);
    printf("log10(%d) = %lf\n", x, ret);
    
    x = 2;
    ret=_fun("math_sqrt",x);
    printf("The square root of %d is %lf\n", x, ret );
   
    a = 9.2;
    b = 3.8;
    ret=_fun("math_fmod",a,b);
    printf("The remainder of %f / %f is %lf\n", a, b, ret);
}