将整个数组从int转换为double,以执行一些aritmetic操作 [英] Conversion of entire array from int to double in order to do some aritmetic operations

查看:175
本文介绍了将整个数组从int转换为double,以执行一些aritmetic操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

  var arrayIntegers:[Int] = [] 
arrayIntegers + = 0 ... 33
var arrayDecimal:[Int] = []
arrayDecimal + = 0 ... 999

问题是我不能将两个数组的值转换为Double。我想要做的是使用复合值获取一个新的数组(称为arrayDoubleComposed),将arrayIntegers的值作为整数部分,然后以arrayDecimal的另一个值作为浮动部分。



当我尝试类型转换时:

  var arrayDoubleComposed:[Double] = [] 
arrayDoubleComposed = Double(arrayIntegers [] +(arrayDecimal [])/ 1000)

我有一个错误。如果我压制[]。相同,



我是一个新来的人,我知道...

解决方案

将一种数组转换为另一种数组是 map 的工作,它将一个函数应用于数组的每个元素,返回结果作为函数返回的类型的新数组。在这种情况下,您需要一个将 Int s转换为 Double 的函数。



尝试这样的东西:

  let integers = Array(0 ... 33)
let fraction = Array(0 ... 999)

让arrayDoubleComposed = map(Zip2(整数,分数)){
(i,f)in
Double i)+ Double(f)/ 1_000
}

Zip2 采用两个序列,并将它们对齐 - 将元素组合在一起,将第二个元素组合在一起等等,然后将其传递到 map 中,将两个元素相结合。 / p>

(另请注意,您只需从范围初始化数组,而不是声明数组,然后添加值)



不清楚你的意思是做什么,因为整数和分数组的长度不一样。方式 Zip2 处理这个是在第一个顺序用完时停止,但这可能不是你想要的。



PS如Swift编译器洒上一些魔法,您可以使用像您尝试过的那样转换数组的内容,但只能在特殊情况下从Objective-C类型转换为本机Swift类型。


I have this bit of code:

    var arrayIntegers : [Int] = []
    arrayIntegers += 0...33
    var arrayDecimal : [Int] = []
    arrayDecimal += 0...999

The problem is I can´t convert the values of both arrays to Double. What I want to do is to get a new array (called arrayDoubleComposed) with composite values, taking a value of arrayIntegers as the integer part and then taking another value of arrayDecimal as the floating part.

When I try to typecast this:

    var arrayDoubleComposed : [Double] = []
    arrayDoubleComposed = Double (arrayIntegers[] + (arrayDecimal[])/1000)

I´ve got an error. The same if I suppress the [].

I´m a little bit newcomer, I know...

解决方案

Converting one kind of array into another is a job for map, which applies a function to each element of an array, returning the results as a new array of the type that function returns. In this case, you want a function that converts the Ints to a Double.

Try something like this:

let integers = Array(0...33)
let fractions = Array(0...999)

let arrayDoubleComposed = map(Zip2(integers, fractions)) { 
  (i, f) in 
    Double(i) + Double(f)/1_000 
}

Zip2 takes two sequences and pairs them up – first elements together, second elements together etc. Then this passes that into map which combines the two elements.

(note also you can just initialize the arrays from the ranges rather than declaring them then adding the values)

It’s not clear what you mean to do, as the integer and fractional arrays are going to be different length. The way Zip2 handles this is to stop when the first sequence runs out, but this may not be what you want.

P.S. casts like the one you tried, which convert the contents of an array en-mass, only work in special cases when converting from Objective-C types to native Swift types, when the Swift compiler sprinkles some magic.

这篇关于将整个数组从int转换为double,以执行一些aritmetic操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆