本文介绍了使用floor(+0.5)做圆有什么不妥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须舍入值并将其存储,因为C ++没有舍入函数,因此我使用以下函数:

I have to round value and store it, because the c++ does not has round function I use this:

floor(x+0.5)



但是当我在代码中使用它时,它不起作用:



but when I use it in my code it does not work:

void algo(float xx1, float yy1, float xx2, float yy2, float xx[], float yy[])
{
   double mm, m1, m2;
   m1 = yy2 - yy1;
   m2 = xx2 - xx1;
   mm = m1 / m2;
   yy[0] = yy1;
   xx[0] = xx1;

   if((mm>=-1)&&(mm<=1))
   {
      for(int i=0; i<=xx2; i++)
      {
         yy[i + 1] = floor(yy[i] + mm + 0.5);
         xx[i + 1] = xx[i] + 1;
      }
   }
   else
   {
      for(int i=0; i<=xx2; i++)
      {
         xx[i + 1] = floor(xx[i] + (1/mm) + 0.5);
         yy[i + 1] = yy[i] + 1;
      }
   }
}

推荐答案


yy[i+1]=floor(yy[i]+mm+0.5);




使用floor()时会做完全不同的事情,因为yy [i + 1]取决于yy [i].

您正在完全生成另一个系列.

您的算法不正确,需要工作.




does completely different things when you use floor() because yy[i+1] is dependent on yy[i].

You are generating a different series altogether.

Your algorithm is incorrect and needs work.


inline long RoundInt (double a)
{
    long retval;
    __asm fld a
    __asm fistp retval
    return retval;
}



这个小小的内联函数只能转换为两条机器指令,并且根据我的测量,它比floor(x + 0.5)和以下隐式转换为int更快.

希望对您有帮助.



This little inline function translates into only two machine instructions and is according to my measurements way faster than floor (x + 0.5) and the following implicit conversion to int.

Hope that helped you a little further along the way.


这篇关于使用floor(+0.5)做圆有什么不妥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 11:37