How to deal with excess precision in floating-point computations?

前端 未结 5 1253
悲&欢浪女
悲&欢浪女 2020-12-10 15:28

In my numerical simulation I have code similar to the following snippet

double x;
do {
  x = /* some computation */;
} while (x <= 0.0);
/* some algorithm         


        
5条回答
  •  忘掉有多难
    2020-12-10 16:03

    As Arkadiy stated in the comments, an explicit cast ((double)x) <= 0.0 should work - at least according to the standard.

    C99:TC3, 5.2.4.2.2 §8:

    Except for assignment and cast (which remove all extra range and precision), the values of operations with floating operands and values subject to the usual arithmetic conversions and of floating constants are evaluated to a format whose range and precision may be greater than required by the type. [...]


    If you're using GCC on x86, you can use the flags -mpc32, -mpc64 and -mpc80 to set the precision of floating-point operations to single, double and extended double precision.

提交回复
热议问题