Matlab mod函数 对应C语言 函数

    技术2022-05-19  17

    Matlab mod函数 对应C语言 函数 2010-09-10 23:43

    Matlab中的mod函数,不同于C语言中的"%",在C中没有专门对应的函数。

    Matlab的mod函数解释为:mod(x,y) is x - n.*y where n=floor(x./y) if y~=0;

    其中:floor为向负无穷取整,即floor(2.6)=2,floor(-2.3) = -3;

    由此可以方便写出对应的C代码

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include"stdio.h" #include "math.h" double MOD(double x,double y) { long n; double f; if((x<0.000000000000001 && x>-0.000000000000001) || (y<0.000000000000001 && y>-0.000000000000001))return x; f = x/y; n = (long)f; if(n>f) n=n-1; f =x-n*y; return f; } void main() { double x = -2.3654,y=3.25665; printf("%lf",MOD(x,y));/*double型,小数点后不能超过15位*/ }

    C中运行结果为:0.891250

    Maltab中运行结果为(format short g):0.89125

     

     

    Matlab中的angle 和C语言中的atan是有差别的。

    C语言用atan计算出来的弧度值,若实部小于零,计算出的弧度值得加上pi(3.1415926)。这样就和Maltab里的angle相同了。

     


    最新回复(0)