swift double accuracy

I am new to swift, it's actually my first attempt. I have the following program in playground:

//:===================

import UIKit

func tax(s:Double)->Double{

return s*0.05

}

var xx=66.0*2.0

tax(xx)

//============


The expected result is

6.6,

but swift evaluates the last line as

6.600000000000001

What am I missing.

You are missing nothing. The result comes from the way computers represent numbers. Have a look at this https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems .


If you need perfect numbers and the range is limited you could use Integers and use them premultiplied with a value, for example for a $-value you could just use cent: 145ct instead of 1.45$. This is called fixed point numbers. https://en.wikipedia.org/wiki/Fixed-point_arithmetic

swift double accuracy
 
 
Q