Floating point equality and tolerances

This blogpost contains an example, fairly foolproof implementation, and detailed theory behind it http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ it is also one of a series, so you can always read more. In short: use ULP for most numbers, use epsilon for numbers near zero, but there are still caveats. If you want to be sure about your floating point … Read more

Truncate (not round off) decimal numbers in javascript

Dogbert’s answer is good, but if your code might have to deal with negative numbers, Math.floor by itself may give unexpected results. E.g. Math.floor(4.3) = 4, but Math.floor(-4.3) = -5 Use a helper function like this one instead to get consistent results: truncateDecimals = function (number) { return Math[number < 0 ? ‘ceil’ : ‘floor’](number); … Read more

C++ floating point precision [duplicate]

To get the correct results, don’t set precision greater than available for this numeric type: #include <iostream> #include <limits> int main() { double a = 0.3; std::cout.precision(std::numeric_limits<double>::digits10); std::cout << a << std::endl; double b = 0; for (char i = 1; i <= 50; i++) { b = b + a; }; std::cout.precision(std::numeric_limits<double>::digits10); std::cout << … Read more