gpt4 book ai didi

arrays - 将数组与 -nan 进行比较

转载 作者:行者123 更新时间:2023-11-30 10:51:44 25 4
gpt4 key购买 nike

我有两个元素数量相等的 Double 数组。它们都包含数字和一些三角计算后的 NaN 值。

我需要将第一个数组的每个元素与第二个数组的每个元素进行比较,找到较大的数字,并将其放入新的第三个数组中。最后,第三个数组应包含与第一个或第二个数组相同数量的元素。

如果比较两个 Nan,我需要在该确切索引处显示特定的错误消息“可怕的错误”。所以我想第三个数组应该是 String 才能显示数字和错误消息。如果将 Double 数字与 NaN 进行比较,则应始终选择 Double 数字中较大的那个。

我该如何做到这一切?

这是我的代码:

import Foundation

var initValue = Double()
var finalValue = Double()
var stepValue = Double()

while true {
print("Enter the starting number of the range")
if let number = readLine(), Double(number) != nil {
initValue = Double(number)!
break
} else {
print("Enter the correct number!")
}
}

while true {
print("Enter the end value of the range")
if let number = readLine(), Double(number) != nil, Double(number)! > initValue {
finalValue = Double(number)!
break
} else {
print("Enter the correct number, which is greater than starting number of the range!")
}
}

while true {
print("Enter delta")
if let number = readLine(), Double(number) != nil {
stepValue = Double(number)!
break
} else {
print("Enter the correct number!")
}
}

var trueArray = [Double]()
for number in stride(from: initValue, through: finalValue, by: stepValue) {
trueArray.append(number)
}

func calcLn () -> [Double] {
let calculatedArray = trueArray.map { log(1-46/sin($0)) }

return calculatedArray
}

func calcTan () -> [String] {
let calculatedArray = trueArray.map { (tan($0)/46) }

return calculatedArray
}

最佳答案

您可以使用 zip 同时迭代 2 个数组,然后使用 map 将元素对转换为输出,如您所愿。打开(ln.isNaN, tan.isNaN)并使用它来表达各种情况及其结果应该很有用。

这是一个粗略的开始:

import Darwin

func promptForDouble(
initialMessage: String,
errorMessage: String,
acceptanceCriteria isAcceptable: (Double) -> Bool = { _ in true }
) -> Double {
print(initialMessage)

while true {
if let number = readLine().flatMap(Double.init), isAcceptable(number) {
return number
}

print(errorMessage)
}
}

let initValue = promptForDouble(
initialMessage: "Enter the starting number of the range",
errorMessage: "Enter the correct number!"
)
let finalValue = promptForDouble(
initialMessage: "Enter the end value of the range",
errorMessage: "Enter the correct number, which is greater than starting number of the range!",
acceptanceCriteria: { initValue < $0 }
)
let stepValue = promptForDouble(
initialMessage: "Enter delta",
errorMessage: "Enter the correct number!"
)

// TODO: give these functions better names!
func calcLn(_ input: [Double]) -> [Double] {
return input.map { log(1 - 46/sin($0)) }
}

func calcTan(_ input: [Double]) -> [Double] {
return input.map { tan($0) / 46 }
}

func mergeResults(lns: [Double], tans: [Double]) -> [Double?] {
return zip(lns, tans).map { ln, tan -> Double? in
switch (ln.isNaN, tan.isNaN) {
case ( true, true): return nil // Return nil to express error. Don't introduce Strings yet.
case (false, true): return ln
case ( true, false): return tan
case (false, false): return max(ln, tan)
}
}
}

func printResults(_ a1: [Double], _ a2: [Double], _ a3: [Double?]) {
for (a, (b, c)) in zip(a1, zip(a2, a3)) {
let resultString = c.map(String.init) ?? "terrible error" // Note: Strings are only introduced at the UI/presentation layer
print("ln: \(a),\ttan: \(b),\tresult: \(resultString)")
}
}


// TODO: give these arrays better names!

let inputs = Array(stride(from: initValue, through: finalValue, by: stepValue))

let array1 = calcLn(inputs)
let array2 = calcTan(inputs)
let array3 = mergeResults(lns: array1, tans: array2)

printResults(array1, array2, array3)

关于arrays - 将数组与 -nan 进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54394417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com