On 12/26/2017 12:27 PM, Stephane Ducasse wrote: > Hi > > > I'm working on improving the number of named colors we have. > I got this list > And I did the following to convert numbers to float and I want to keep > only max three decimal "float" and "three decimal" are fundamentally incompatible goals. Float is a binary format, not decimal, so you cannot expect results to always be exact in decimal. > > Now I do not get > > 0.9420000000001 roundUpTo: 0.001 There are many numbers that cannot be represented exactly as floats. 1/1000 is one of them:
0.001 > (1/1000) ---> true So what you're actually asking for is to round to some multiple of a binary number that is close to, but not quite exactly, 1/1000. Therefore, #roundUpTo: determines (correctly) that the answer should be 943 * 0.001, and computes that, and answers it. But since 0.001 is greater than 1/1000, the result is greater than 943/1000. There are a number of conceivable alternatives, depending on what you want. But here, it's not clear why you want to do rounding at all in this case -- the expense of a float is the same whether rounded or not, so if you want floats I'd leave them unrounded and therefore more accurately approximating the original input numbers. Or if you want them exact, use fractions instead of floats. Regards, -Martin
