How should I do floating point comparison?

TL;DR Use the following function instead of the currently accepted solution to avoid some undesirable results in certain limit cases, while being potentially more efficient. Know the expected imprecision you have on your numbers and feed them accordingly in the comparison function. bool nearly_equal( float a, float b, float epsilon = 128 * FLT_EPSILON, float … Read more

How to properly round-up half float numbers?

The Numeric Types section documents this behaviour explicitly: round(x[, n]) x rounded to n digits, rounding half to even. If n is omitted, it defaults to 0. Note the rounding half to even. This is also called bankers rounding; instead of always rounding up or down (compounding rounding errors), by rounding to the nearest even … Read more

Convert float to double without losing precision

It’s not that you’re actually getting extra precision – it’s that the float didn’t accurately represent the number you were aiming for originally. The double is representing the original float accurately; toString is showing the “extra” data which was already present. For example (and these numbers aren’t right, I’m just making things up) suppose you … Read more

Swift – How to remove a decimal from a float if the decimal is equal to 0?

Swift 3/4: var distanceFloat1: Float = 5.0 var distanceFloat2: Float = 5.540 var distanceFloat3: Float = 5.03 extension Float { var clean: String { return self.truncatingRemainder(dividingBy: 1) == 0 ? String(format: “%.0f”, self) : String(self) } } print(“Value \(distanceFloat1.clean)”) // 5 print(“Value \(distanceFloat2.clean)”) // 5.54 print(“Value \(distanceFloat3.clean)”) // 5.03 Swift 2 (Original answer) let distanceFloat: … Read more

Floating point vs integer calculations on modern hardware

For example (lesser numbers are faster), 64-bit Intel Xeon X5550 @ 2.67GHz, gcc 4.1.2 -O3 short add/sub: 1.005460 [0] short mul/div: 3.926543 [0] long add/sub: 0.000000 [0] long mul/div: 7.378581 [0] long long add/sub: 0.000000 [0] long long mul/div: 7.378593 [0] float add/sub: 0.993583 [0] float mul/div: 1.821565 [0] double add/sub: 0.993884 [0] double mul/div: … 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

Format floats with standard json module

Note: This does not work in any recent version of Python. Unfortunately, I believe you have to do this by monkey-patching (which, to my opinion, indicates a design defect in the standard library json package). E.g., this code: import json from json import encoder encoder.FLOAT_REPR = lambda o: format(o, ‘.2f’) print(json.dumps(23.67)) print(json.dumps([23.67, 23.97, 23.87])) emits: … Read more