Swift中的幂运算符 [英] Exponentiation operator in Swift

查看:454
本文介绍了Swift中的幂运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Swift语言参考中没有看到在基本算术运算符中定义的幂运算符.

I don't see an exponentiation operator defined in the base arithmetic operators in the Swift language reference.

该语言中确实没有预定义的整数或浮点运算符吗?

Is there really no predefined integer or float exponentiation operator in the language?

推荐答案

没有运算符,但是您可以使用pow函数,如下所示:

There isn't an operator but you can use the pow function like this:

return pow(num, power)

如果愿意,还可以让操作员像这样调用pow函数:

If you want to, you could also make an operator call the pow function like this:

infix operator ** { associativity left precedence 170 }

func ** (num: Double, power: Double) -> Double{
    return pow(num, power)
}

2.0**2.0 //4.0

这篇关于Swift中的幂运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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