Why my double can contain a value below the machine epsilon?

前端 未结 3 1513
伪装坚强ぢ
伪装坚强ぢ 2021-01-14 00:40

I was solving an equation using double precision and I got -7.07649e-17 as a solution instead of 0.

I agree it\'s close enough that I can s

3条回答
  •  旧巷少年郎
    2021-01-14 01:01

    A common solution for the floating point precision problem is to define an epsilon value yourself and compare to that instead of zero.

    e.g.

    double epsilon = 0.00001;
    if (abs(value) < epsilon) // treat value as 0 in your code
    

提交回复
热议问题