Cos(90) returning a value very close to 0, but I need 0?

后端 未结 5 898
旧时难觅i
旧时难觅i 2021-01-07 04:00

The values for temp_x_btm_left = 0 & temp_y_btm_left=1;

angle = 90;

//Moving the bottom left coordinates
_btm_left.real() = (temp_x_btm_left * cos(angle         


        
5条回答
  •  情话喂你
    2021-01-07 04:31

    Pre-calculate your angle calculations, then use the results. Something like:

    if (angle == 90) {
      cos_angle = 0
    } else {
      cos_angle = cos(angle)
    }
    ...
    _btm_left.real() = ... * cos_angle ...
    

    This has the additional benefit of doing fewer calls to cos() and sin().

    Keep in mind that if angle is a float, doing something like angle == 90 could always return false, because of how floating point numbers are represented internally. You might want to change that to something like:

    if (abs(angle-90) < some_small_number) {
    

提交回复
热议问题