floating-point


floating point - Truncating floats in Python

round(1.923328437452, 3) See Python's documentation on the standard types. You'll need to scroll down a bit to get to the round function. Essentially the second number says how many decimal places to... Read More


In MATLAB, are variables REALLY double-precision by default?

They're doubles. vpa() is simply choosing to display non-significant digits beyond the floating point relative accuracy, where printf() and disp() truncate or zero them out. You're only getting your... Read More


c++ - How to force GCC to assume that a floating-point expression is non-negative?

You can write assert(x*x >= 0.f) as a compile-time promise instead of a runtime check as follows in GNU C: #include <cmath> float test1 (float x) { float tmp = x*x; if (!(tmp >= 0.0f))... Read More


objective c - How dangerous is it to compare floating point values?

First of all, floating point values are not "random" in their behavior. Exact comparison can and does make sense in plenty of real-world usages. But if you're going to use floating point you need to... Read More


c++ - In which order should floats be added to get the most precise result?

Your instinct is basically right, sorting in ascending order (of magnitude) usually improves things somewhat. Consider the case where we're adding single-precision (32 bit) floats, and there are 1 bi... Read More


java - How to check that a string is parseable to a double?

Apache, as usual, has a good answer from Apache Commons-Lang in the form of NumberUtils.isCreatable(String). Handles nulls, no try/catch block required.... Read More


ios - Interface Builder degrades storyboards, resizes and repositions views in small increments

The most interesting clue in this mystery is that it seems especially bad when you open the same storyboard on a Retina display as opposed to a non-retina display. At first I was going back and forth... Read More


floating point - Large numbers erroneously rounded in JavaScript

What you're seeing here is actually the effect of two roundings. Numbers in ECMAScript are internally represented double-precision floating-point. When id is set to 714341252076979033 (0x9e9d9958274c... Read More


floating point - round() for float in C++

There's no round() in the C++98 standard library. You can write one yourself though. The following is an implementation of round-half-up: double round(double d) { return floor(d + 0.5); } The prob... Read More


python - Why are some float < integer comparisons four times slower than others?

A comment in the Python source code for float objects acknowledges that: Comparison is pretty much a nightmare This is especially true when comparing a float to an integer, because, unlike floats,... Read More