数学函数

Silk内置了一些数学函数用以数学计算,变量x和y都为浮点型,如果传入的参数是整形,会自动转换为浮点型,返回的结果也都为浮点型:

1. _fun("math_acos", x)
math_acos 函数返回以弧度表示的 x 的反余弦。

2. _fun("math_asin", x)
math_asin 函数返回以弧度表示的 x 的反正弦。

3. _fun("math_atan", x)
math_atan 函数返回以弧度表示的 x 的反正切。

4. _fun("math_atan2", y, x)
math_atan2 函数返回以弧度表示的 y/x 的反正切。y 和 x 的值的符号决定了正确的象限。

5. _fun("math_cos", x)
math_cos 函数返回弧度角 x 的余弦。

6. _fun("math_cosh", x)
math_cosh 函数返回 x 的双曲余弦。

7. _fun("math_sin", x)
math_sin 函数返回弧度角 x 的正弦。

8. _fun("math_sinh", x)
math_sinh 函数返回 x 的双曲正弦。

9. _fun("math_tanh", x)
math_tanh 函数返回 x 的双曲正切。

10. _fun("math_exp", x)
math_exp 函数返回 e 的 x 次幂的值。

11. _fun("math_frexp", x)
math_frexp 函数把浮点数 x 分解成尾数和指数。返回一个数组,数组的第一个元素是尾数,第二个元素是指数。

12. _fun("math_ldexp", x, e)
math_ldexp 函数返回 x 乘以 2 的 e 次幂。e为整型。

13. _fun("math_log", x)
math_log 函数返回 x 的自然对数(基数为 e 的对数)。

14. _fun("math_log10", x)
math_log10 函数返回 x 的常用对数(基数为 10 的对数)。

15. _fun("math_modf", x)
math_modf 函数返回一个数组,数组第一个元素为x的整数部分,第二个值为小数部分(小数点后的部分)。

16. _fun("math_pow", x, y)
math_pow 函数返回 x 的 y 次幂。

17. _fun("math_sqrt", x)
math_sqrt 函数返回 x 的平方根。

18. _fun("math_fabs", x)
math_fabs 函数返回 x 的绝对值。

19. _fun("math_floor", x)
math_floor 函数返回小于或等于 x 的最大的整数值。

20. _fun("math_ceil", x)
math_ceil 函数返回大于或等于 x 的最小的整数值。

21. _fun("math_fmod", x, y)
math_fmod 函数返回 x 除以 y 的余数。

使用数学函数的例子:
main()
{
    PI=3.14159265;

    x = 60.0;
    val = PI / 180.0;
    ret = _fun("math_cos", x*val );
    printf("%lf 的余弦是 %lf 度\n", x, ret);

    x = 45.0;
    val = PI / 180;
    ret =  _fun("math_sin", x*val );
    printf("%lf 的正弦是 %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("整数部分 = %lf\n", ret[0]);
    printf("小数部分 = %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("%d 的平方根是 %lf\n", x, ret );
   
    a = 9.2;
    b = 3.8;
    ret=_fun("math_fmod",a,b);
    printf("%f / %f 的余数是 %lf\n", a, b, ret);
}