Swift中的数学函数


95

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


进行时:

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

我得到:

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

在此处输入图片说明


如果您Ambiguous reference to x查看了此答案,请访问:stackoverflow.com/a/34357943/1359306
Patrick

Answers:


98

正如其他指出的那样,您有几种选择。如果只需要数学函数。您只能导入达尔文。

import Darwin

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

import Foundation

如果您需要所有内容以及用户界面的类,则取决于您的游乐场是OS X还是iOS。

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

import Cocoa

对于iOS,您需要导入UIKit。

import UIKit

您可以通过打开File Inspector(can1)轻松发现您的游乐场平台。

游乐场设置-平台


6
在Swift操场上,我得到了一个错误:No such module 'Cocoa'
David Poxon 2014年

@noilly:有点晚了,你必须将文件类型设置到iOS在操场上的文件属性(Cmd的-OPT-0),或进口的UIKit而是按照上述评论
克里斯·康诺弗

1
没有这样的模块“可可”
亚历山大·沃尔科夫2014年

4
UIKit,可可粉,粉底液,所有有效选择。
erdekhayser 2014年

导入Cocoa在iOS应用程序中不起作用。在“没有这样的模块‘可可’错误棱如果你只是导入UIKit中,你使用的sqrt(错误)。
SomeGuy

77

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

import Darwin

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


而随着斯威夫特3+,你不需要达尔文(或Glibc的)的sqrtfloor并且round因为你可能本身分别使用0.5.squareRoot()Int(0.5)0.5.round()
心教堂

17

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

正确的方法是使用Glibc。这是因为在OS X和iOS上,基本的类Unix API在Darwin中,而在Linux中,它们位于Glibc中。Importing Foundation在这里无济于事,因为它本身没有区别。为此,您必须自己显式导入它:

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

您可以在此处关注Foundation框架的开发以了解更多信息


编辑:2018年12月26日

正如指出@心教堂,迅速从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

而随着斯威夫特3+,你不需要达尔文(或Glibc的)的sqrtfloor并且round因为你可能本身分别使用0.5.squareRoot()Int(0.5)0.5.round()
心教堂

@Cœur,我已经有一段时间没有联系了。也许您可以以答案的形式提供这种见解。谢谢
smac89 '18 -10-10

因为pow我需要包括GlibcLinux,谢谢!
A1rPun

10

您可以内联使用它们:

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)")

1
我收到以下错误:使用未解析的标识符'sqrt'。
Giovanni

我认为期望我声明一个名为sqrt的函数,但这显然不是我想要的。谢谢
Giovanni

您需要导入UIKit或Cocoa或Foundation。
Ben Gottlieb 2014年

有没有可以给我一个Ints数组的函数,然后Swift会给我返回最匹配它的2(或1、3或其他函数)的幂函数?就像我给的那样:[1,2,3]它会返回,y = x或者我给它[1,4,9]它会返回,y = x^2或者以某种方式接近结束f(x)
亲爱的

它为我工作,但是默认情况下,我的游乐场会导入UIKit。它也可以工作,导入Foundation或Darwin而不是UIKit。
Timbergus

8

要使用数学功能,您必须 import Cocoa

您可以通过以下方式查看其他定义的数学函数。单击函数名称,sqrt然后输入包含所有其他全局数学函数和常量的文件。

文件的一小段

...
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)        */
...

1
好吧,这实际上是一个问题的答案:“我如何使用诸如sqrt(),floor(),round(),sin()等数学函数?”
DanEEStar 2014年

2
这不是正确的答案。它们不是全局函数。您必须import Cocoa使用它们。
James Billingham 2014年

3

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

相反的floor()round()和舍入例程可以使用的其他rounded(_:)

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"

这些目前在上可用,Float并且Double应该足够容易地转换CGFloat为例如。

代替FloatingPoint协议sqrt()squareRoot()方法。同样,两种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"
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.