Reputation: 163
I have a double array of the type:
let a = [-1.0, 2.0, 3.4, -4.12, -0.05, 5.5]
Is there an easy way to get its absolute values:
let aAbs = [1.0, 2.0, 3.4, 4.12, 0.05, 5.5]
Thanks!
Upvotes: 0
Views: 1511
Reputation: 63399
Even simpler, you can pass the abs
function directly:
let absolutes = inputs.map(abs)
Upvotes: 1
Reputation: 318954
A simple map
will do it:
let a = [-1.0, 2.0, 3.4, -4.12, -0.05, 5.5]
let aAbs = a.map { abs($0) }
Upvotes: 2