【问题标题】:Mathematical functions in SwiftSwift 中的数学函数
【发布时间】:2014-07-23 15:10:14
【问题描述】:

如何使用sqrt()floor()round()sin() 等数学函数?


做的时候:

_ = floor(2.0)
_ = sqrt(2.0)

我明白了:

错误:使用未解析的标识符“地板”
错误:使用未解析的标识符“sqrt”

【问题讨论】:

标签: swift


【解决方案1】:

你可以直接使用它们:

var square = 9.4
var floored = floor(square)
var root = sqrt(floored)

println("Starting with \(square), we rounded down to \(floored), then took the square root to end up with \(root)")

【讨论】:

  • 我收到以下错误:使用未解析的标识符“sqrt”。
  • 我认为它希望我声明一个名为 sqrt 的函数,但显然这不是我想要的。谢谢
  • 你需要导入UIKit,或者Cocoa,或者Foundation。
  • 是否有一个函数可以给它一个 Ints 数组,然后 Swift 会返回一个最匹配它的 2(或 1、3 或其他)幂的函数?就像我给它:[1,2,3] 它会返回 y = x 或者我会给它 [1,4,9] 它会返回 y = x^2 或以f(x) 的方式接近的东西
  • 它对我有用,但我的游乐场默认导入 UIKit。它也可以,导入 Foundation 或 Darwin 而不是 UIKit。
【解决方案2】:

正如其他人所说,您有几个选择。如果你只想要数学函数。您只能导入 Darwin。

import Darwin

如果您想要数学函数和其他标准类和函数。您可以导入 Foundation。

import Foundation

如果您想要用户界面的所有内容和类,这取决于您的 Playground 是用于 OS X 还是 iOS。

对于 OS X,您需要导入 Cocoa。

import Cocoa

对于 iOS,您需要导入 UIKit。

import UIKit

您可以通过打开文件检查器 (⌥⌘1) 轻松发现您的 Playground 平台。

【讨论】:

  • 在 Swift 操场上,我收到错误:No such module 'Cocoa'
  • @noilly:现在有点晚了,你必须在 Playground 的文件属性 (Cmd-Opt-0) 中将文件类型设置为 iOS,或者根据上述评论导入 UIKit
  • 没有这样的模块'Cocoa'
  • UIKit、Cocoa、Foundation,所有有效的选择。
  • 导入 Cocoa 在 iOS 应用程序中不起作用。出现“没有这样的模块 'Cocoa' 错误。如果你只是导入 UIKit,你会在使用 sqrt() 时出错。
【解决方案3】:

要使用数学函数,您必须 import Cocoa

您可以通过以下方式查看其他定义的数学函数。 在函数名称 sqrt 上创建一个 Cmd 单击并输入包含所有其他全局数学函数和常量的文件。

文件的小sn-p

...
func pow(_: CDouble, _: CDouble) -> CDouble

func sqrtf(_: CFloat) -> CFloat
func sqrt(_: CDouble) -> CDouble

func erff(_: CFloat) -> CFloat
...
var M_LN10: CDouble { get } /* loge(10)       */
var M_PI: CDouble { get } /* pi             */
var M_PI_2: CDouble { get } /* pi/2           */
var M_SQRT2: CDouble { get } /* sqrt(2)        */
...

【讨论】:

  • 嗯,这是对实际问题“如何使用 sqrt()、floor()、round()、sin() 等数学函数?”的回答。
  • 这不是一个正确的答案。它们不是全局函数。你必须import Cocoa 才能使用它们。
【解决方案4】:

准确地说,达尔文就足够了。无需导入整个 Cocoa 框架。

import Darwin

当然,如果您需要来自 Cocoa 或 Foundation 或其他更高级别框架的元素,您可以改为导入它们

【讨论】:

  • 在 Swift 3+ 中,sqrtfloorround 不需要 Darwin(或 Glibc),因为您可以分别使用 0.5.squareRoot()Int(0.5)0.5.round().
【解决方案5】:

对于在 Linux 即 Ubuntu 上使用 swift [2.2] 的人来说,导入是不同的!

正确的方法是使用 Glibc。这是因为在 OS X 和 iOS 上,基本的类 Unix API 位于 Darwin 中,但在 linux 中,这些位于 Glibc 中。在这里导入 Foundation 对您没有帮助,因为它本身并不能区分。为此,您必须自己显式导入它:

#if os(macOS) || os(iOS)
import Darwin
#elseif os(Linux) || CYGWIN
import Glibc
#endif

大家可以关注Foundation框架的开发here了解更多


编辑:2018 年 12 月 26 日

作为@Cœurpointed out,从swift 3.0 开始,一些数学函数现在是类型本身的一部分。例如,Double 现在有一个squareRoot 函数。同理,ceilfloorround,都可以用Double.rounded(FloatingPointRoundingRule) -> Double实现。

此外,我刚刚在 Ubuntu 18.04 上下载并安装了最新的稳定版 swift,看起来您现在只需导入 Foundation 框架即可访问数学函数。我尝试为此查找文档,但没有任何结果。

➜ swift          
Welcome to Swift version 4.2.1 (swift-4.2.1-RELEASE). Type :help for assistance.
  1> sqrt(9)
error: repl.swift:1:1: error: use of unresolved identifier 'sqrt'
sqrt(9)
^~~~


  1> import Foundation
  2> sqrt(9)
$R0: Double = 3
  3> floor(9.3)
$R1: Double = 9
  4> ceil(9.3) 
$R2: Double = 10

【讨论】:

  • 在 Swift 3+ 中,sqrtfloorround 不需要 Darwin(或 Glibc),因为您可以分别使用 0.5.squareRoot()Int(0.5)0.5.round().
  • @Cœur,我已经有一段时间没有使用 swift 了。也许您可以以答案的形式提供这种见解。谢谢
  • 对于pow,我需要在Linux 中包含Glibc,谢谢!
【解决方案6】:

对于 Swift 做事的方式,您可以尝试使用 Swift 标准库中提供的工具。这些应该可以在任何能够运行 Swift 的平台上运行。

您可以使用rounded(_:),而不是floor()round() 和其余的舍入例程:

let x = 6.5

// Equivalent to the C 'round' function:
print(x.rounded(.toNearestOrAwayFromZero))
// Prints "7.0"

// Equivalent to the C 'trunc' function:
print(x.rounded(.towardZero))
// Prints "6.0"

// Equivalent to the C 'ceil' function:
print(x.rounded(.up))
// Prints "7.0"

// Equivalent to the C 'floor' function:
print(x.rounded(.down))
// Prints "6.0"

这些目前在FloatDouble 上可用,并且应该很容易转换为CGFloat

在 FloatingPoint 协议中,squareRoot() 方法不是 sqrt()。同样,FloatDouble 都符合 FloatingPoint 协议:

let x = 4.0
let y = x.squareRoot()

对于三角函数,标准库无法提供帮助,因此您最好在 Apple 平台上导入 Darwin 或在 Linux 上导入 Glibc。祈祷他们将来会是一种更整洁的方式。

#if os(OSX) || os(iOS)
import Darwin
#elseif os(Linux)
import Glibc
#endif

let x = 1.571
print(sin(x))
// Prints "~1.0"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    • 2015-03-14
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 2014-08-16
    相关资源
    最近更新 更多