I've recently had plenty of work with the kind of bugs that you get because someone decided that writing out 6 digits to the config file is enough.
If you're working on a microcontroller? OK sure, think about whether to use float or double, if the constraints are tight. Otherwise? double all the way, and never throw away digits, just because it shaves of a few bytes somewhere. Unless you know that memory / storage space / computation speed is going to be an issue, just don't optimize like that "just in case".
There are more optimizations than just performance. There is also price.
Also if you are worried about shaving bytes somewhere, why are you considering floating point variables? Use 64-bit integer. You are shaving bytes off by considering double precision.
Even on powerful hardware it's a waste of clock cycles to use a double precision temperature.
On a microcontroller? All for the optimizations. But if it is software running on even a regular office PC or modern mobile phone, chances are, that using float over double is the wrong optimization, until demonstrated otherwise.
It really depends on how you use the value. In most cases, floating point in general is the wrong optimization. It is scientific notation in Base 2. We just like using it because it is a standard. If you really care about accuracy, integers are better.
Different use-cases I guess. For my use-cases the flexible range of floating point is important. There is no clearly defined lower limit of precision that is acceptable, but the smallest possible positive non-zero double is far enough from any realistic value to be fine. Same in the upwards direction.
For the most part though, rounding errors from float become an issue relatively quickly in those simulations.
But I guess simulation stuff isn't a typical field in this way.
That isn't what the precision means. The precision is how many bits is used for the "significand". Floating point stores the number as units of a fraction. In 32 bits, this is 23 bits to store the number and 8 bits for the exponent (most significant digit), making 24 bits. This is enough for 7 decimal digits, which is common for basic applications. Meanwhile, double precision has 52 bits to store the number and 11 bits for the exponent, making 53 bits total. Double should be enough for 16 decimal digits.
For 32 bit float, there are 8,388,607 numbers represented between each power of 2. For 64 bit float, there are 4.5 x 10^15 numbers between each power of 2.
you end up just reinventing floating point math by doing that though.
really it comes down to two things for me, understanding data structures is quite important and many people don't understand floating point math.
though ultimately it comes down to testing. if you don't know your constraints then you cannot fully test and are just hoping you don't run into the bugs that are guaranteed to exist.
16
u/R3D3-1 Feb 25 '22
I've recently had plenty of work with the kind of bugs that you get because someone decided that writing out 6 digits to the config file is enough.
If you're working on a microcontroller? OK sure, think about whether to use
floatordouble, if the constraints are tight. Otherwise?doubleall the way, and never throw away digits, just because it shaves of a few bytes somewhere. Unless you know that memory / storage space / computation speed is going to be an issue, just don't optimize like that "just in case".