Float to string issues


#1

Is there a way to avoid a loss of precision when concerting between floating point numbers and strings? For example:

myIdentifiers.set("sliderincr", String(".01").getFloatValue());
Logger::writeToLog(myIdentifiers.getWithDefault("sliderincr", -1).toString());

The value printed out is 0.00999999978 which funks up my on-screen slider values. From my limited knowledge I would have thought there would be no loss of precision in this case but it seems there is? Any ideas?


#2

This imprecision does not come from converting between String and float, but the way values are stored in floating point variables. These do not have unlimited precision, and some values that are short to represent in base 10, are very long or repeating in base 2.

 

For more information http://www.exploringbinary.com/why-0-point-1-does-not-exist-in-floating-point/ (just a random link coming up when looking for floating point 0.1, because that's a famous example)


#3

Thanks for the info. I guess I'll start formatting the strings to make sure they look Ok. Thanks for the random link too, not bad for a random one :) 


#4


#5

Seems that JUCE does some tidy converting between doubles and floats. Problem solved :)