# Where do the errors in these floats come from?

#1

Something that’s bugged me ever since I started doing C++/DSP programming is that many of my floats in Visual Studio aren’t what they’re supposed to be.

For example, here I’m hovering over the 1.35f default value in my parameter tree, and instead, Visual Studio has assigned it 1.3500000024 for its value:

Why on earth does this happen? I see it in the final synth as well, for example, if I save the parameter tree to a preset, and open that in a text editor, sure enough, the value will be “1.3500000024”.

Is there any reason this happens and should anything be done about it?

Thanks.

#2

It’s normal for floating point code, they are not “errors” as such. The best you can do is to deal with the cosmetics issues in your GUI code by formatting the numbers to not have so many decimals. Visual Studio shows you the values as they will be used in the compiled code. (For example 1.35 can’t be represented exactly as floating point.)

#3

Yeah, I’ve rounded them off in the GUI. But why do they happen? I presume it’s something to do with binary for floats only being able to express certain numbers precisely and others will be off by definition by a tiny amount.

Fortunately, from what I see, numbers like 0.f and 1.f aren’t subject to these problems. Because for example, sometimes I want to us things like:

if (parameter != 0) {
run code;
}

So calculations are only done when parameters have values requiring math based on them.

Do doubles have the same problems, or is it just floats?

#4

Your presumption is correct. There’s a pretty good explanation here: https://www.learncpp.com/cpp-tutorial/floating-point-numbers/

#5

They do, just several more decimal places later
(7 places in float vs. 15 places in double, according to this source: https://www.geeksforgeeks.org/difference-float-double-c-cpp/)

Also interesting might be the class std::numeric_limits

``````auto smallestDouble = std::numeric_limits<double>::min();
``````