sin, cos, tan and rounding error

前端 未结 9 629
执念已碎
执念已碎 2020-12-06 14:09

I\'m doing some trigonometry calculations in C/C++ and am running into problems with rounding errors. For example, on my Linux system:

#include 

        
9条回答
  •  眼角桃花
    2020-12-06 14:41

    @Josh Kelley - ok serious answer.
    In general you should never compare the results of any operation involving floats or doubles with each other.

    The only exceptions is assignment.
    float a=10.0;
    float b=10.0;
    then a==b

    Otherwise you always have to write some function like bool IsClose(float a,float b, float error) to allow you to check if two numbers are within 'error' of each other.
    Remember to also check signs/use fabs - you could have -1.224647e-16

提交回复
热议问题