Q:

Division of Float's not accurate in Swift

I have this function: 
extension Float {
    func shortenDecimals() -> Float {
        println(self)

        let a = self * 10.0
        println(a)

        let b = round(a)
        println(b)

        println(b/10.0)
        return b / 10.0
    }
}

What this function should do is return a float with 1 decimal
This is what it prints out :
self        :   0.0833333358168602
self * 10   :   0.833333373069763
round(a)    :   1.0
b/10        :   0.100000001490116

I find this very frustrating, why does it not return 0.1?

A:

0.5 is the only float “with 1 decimal”. All other fractional floats have more digits after the dot when expressed in decimal, because the representation of floating-point numbers is binary, not decimal.
The binary and decimal systems are equivalent when it comes to representing integers (they can both represent all the integers), but not when it comes to representing fractional values. The values that are simple to represent in decimal are overwhelmingly not representable in binary. Exceptions to this general rule are 0.25, 0.5, 0.75, 0.125, but not 0.1, which is not representable exactly in binary. 0.100000001490116 shows the first digits of the best approximation that Float has to offer.

