How do you compare float and double while accounting for precision loss?

Be extremely careful using any of the other suggestions. It all depends on context. I have spent a long time tracing bugs in a system that presumed a==b if |a-b|<epsilon. The underlying problems were: The implicit presumption in an algorithm that if a==b and b==c then a==c. Using the same epsilon for lines measured in … Read more

How to check if C++ compiler uses IEEE 754 floating point standard

Actually you have an easier way to achieve this in C++. From the C++ standard 18.2.1.1 the class numeric_limits exists within std. In order to access said static member you simply do this: std::numeric_limits<double>::is_iec559; Or: std::numeric_limits<float>::is_iec559; Which should return true if IEEE 754 is in use, false otherwise. As an alternative method, the second part … Read more

Floating point keys in std:map

So there are a few issues with using doubles as keys in a std::map. First, NaN, which compares less than itself is a problem. If there is any chance of NaN being inserted, use this: struct safe_double_less { bool operator()(double left, double right) const { bool leftNaN = std::isnan(left); bool rightNaN = std::isnan(right); if (leftNaN … Read more

Can 0.99999999999 be rounded to 1.0 when multiplying?

If you multiply the greatest value below 1.0 with someInt (> 0), the result will never be someInt. This can be exhaustively tested for integers like this: Double greatestLessThanOne = Double.longBitsToDouble(4607182418800017407L); // Assert that greatestLessThanOne is indeed the largest double less than 1. //assert 1.0 == greatestLessThanOne + Math.ulp(greatestLessThanOne); for (int i = 1; i … Read more

How to check for inf (and | or) NaN in a double variable

When using scanf() double should be read using %lf, not %f. %f will convert the input into a 32-bit float, so the first 32 bits of your variables will be filled with some invalid data, and the last 32 bits will be left as garbage. Yes. #include <limits>, then std::numeric_limits<double>::quiet_NaN(). Some compilers (e.g. gcc) also … Read more

Printing double without losing precision

It’s not correct to say “floating point is inaccurate”, although I admit that’s a useful simplification. If we used base 8 or 16 in real life then people around here would be saying “base 10 decimal fraction packages are inaccurate, why did anyone ever cook those up?”. The problem is that integral values translate exactly … Read more

Is Python’s == an equivalence relation on the floats?

== is reflexive for all numbers, zero, -zero, ininity, and -infinity, but not for nan. You can get inf, -inf, and nan in native Python just by arithmetic operations on literals, like below. These behave correctly, as in IEEE 754 and without math domain exception: >>> 1e1000 == 1e1000 True >>> 1e1000/1e1000 == 1e1000/1e1000 False … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)