Saif
Saif

Reputation: 163

Getting absolute values of double array in Swift

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

Answers (2)

Alexander
Alexander

Reputation: 63399

Even simpler, you can pass the abs function directly:

let absolutes = inputs.map(abs)

Upvotes: 1

rmaddy
rmaddy

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

Related Questions