Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do arithmetic between int and float in Swift?

This is my first day in Swift programming and till now we are using Objective C. I tried to write simple addition program it works. Like,

var i = 10
var j = 10
var k = i + j
println(k)

But when I change one of the values to float it gives error.

var i = 10
var j = 10.4
var k = i + j
println(k)

Error: main.swift:13:11: Could not find an overload for '+' that accepts the supplied arguments

Now I did Google search and tried few thing e.g. Double(i+j) , but it doesn't work. Swift should implicitly convert int to float in this case, isn't it?

Please suggest if I am doing any mistake understanding Swift language.

like image 875
Pranit Kothari Avatar asked Jun 19 '14 06:06

Pranit Kothari


People also ask

Can you add float and int in Swift?

As a result of all this, Swift will refuse to automatically convert between its various numeric types – you can't add an Int and a Double , you can't multiply a Float and an Int , and so on.

Can you mix integers and floats?

Yes, an integral value can be added to a float value. The basic math operations ( + , - , * , / ), when given an operand of type float and int , the int is converted to float first. So 15.0f + 2 will convert 2 to float (i.e. to 2.0f ) and the result is 17.0f .

Does Swift have float?

Swift provides two signed floating-point number types: Double represents a 64-bit floating-point number. Float represents a 32-bit floating-point number.


3 Answers

Depending on what you want your result to be, you should convert it to the appropriate type using that types init method.

eg.

var myInt = 5;
var myDouble = 3.4;

If I want a double for example in my result

var doubleResult = Double(myInt) + myDouble;

if I want an integer instead, please note the double will be truncated.

var intResult = myInt + Int(myDouble)

The problem I see in your example is you're trying to do an add operation and then convert it but both values needs to be the same before you perform the addition.

Apple has made it quiet strict to avoid type mis-match and conversion errors. Sometimes this can be a bit 'too strict' for dev coming from other languages, I was annoyed at first but I got used to it.

like image 91
TheLazyChap Avatar answered Oct 28 '22 21:10

TheLazyChap


You could define your own operator...

// Put this at file level anywhere in your project
operator infix + { }
@infix func + (a: Int, b: Double) -> Double {
    return Double(a) + b
}
@infix func + (a: Double, b: Int) -> Double {
    return Double(b) + a
}

let i = 10
let j = 10.4
let k = i + j // 20.4

...but I feel this is going against the spirit of the language (and as @TheLazyChap says, it depends what you want, which may not always be the same).

like image 37
Grimxn Avatar answered Oct 28 '22 22:10

Grimxn


try this:

 var i = 10  //Int Type
 var j = 10.4  //Double Type
 var k = Double(i) + j //result is now Double Type
 println(k)
like image 27
MarcS Avatar answered Oct 28 '22 21:10

MarcS