FloatingPoint协议中的文字数字 [英] Literal numbers in FloatingPoint protocol

查看:152
本文介绍了FloatingPoint协议中的文字数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在节假日没有太多事情的时候,我终于开始更新我的数学库以使用 FloatingPoint 协议,并摆脱所有重复的代码。令我惊讶的是,我几乎立刻就被字面数字咬了:

  func uprightAngle1< T:FloatingPoint>(_ x: T) - > T(
if(x> 0.5 * T.pi)&& (x <1.5 * T.pi){// ***错误在这里
//二元运算符'*'不能应用于'Double'和'T'类型的操作数
return x - T.pi
} else {
return x
}
}

然而, 可以正常工作:

  func uprightAngle2< T:FloatingPoint>( _ x:T) - > T(
if(x> T.pi / 2)&& (x <3 * T.pi / 2){//所有罚款都在
return x - T.pi
} else {
return x
}
}

任何人都可以

A)解释为什么编译器会用整数文字正确地推断出类型,而不是浮点文字,

B)告诉我当我不能使用的习惯用法因为既不是让一半有理由:T = 0.5 也不 T(0.5)编译...

解决方案

FloatingPoint 协议继承自 ExpressibleByIntegerLiteral
通过继承链

  FloatingPoint  -  SignedNumeric  - 数字 -  ExpressibleByIntegerLiteral 

这就是为什么第二个函数 uprightAngle2 编译:值为
的类型 T 由整数文字 2 3 创建。


第一个功能on uprightAngle1 不会编译,因为
FloatingPoint 协议没有 ExpressibleByFloatLiteral ,即 T 类型的值不能从浮点文字(如<$ c)创建


$ ul










$ b li>

创建有理数值为 let half:T = 1/2 。 ( not let half = T(1/2)
会在创建 T
值。) BinaryFloatingPoint (它从 ExpressibleByFloatLiteral 继承
)。


有关浮点数
协议设计的更多信息,请参阅 SE-0067 Enhanced Floating Point Protocols


Swift标准库中的浮点类型( Double Float CGFloat Float80 )以及
CGFloat 都符合
BinaryFloatingPoint 协议,因此该协议对于许多应用程序而言足够
通用。

Having not much to do over the holidays, I finally got round to updating my maths library to use the FloatingPoint protocol, and get rid of all the duplicate code. To my surprise, I got bitten pretty much straight away by literal numbers:

func uprightAngle1<T: FloatingPoint>(_ x: T) -> T {
    if (x > 0.5 * T.pi) && (x < 1.5 * T.pi) { // *** ERROR HERE
        // binary operator '*' cannot be applied to operands of type 'Double' and 'T'
        return x - T.pi
    } else {
        return x
    }
}

however, this works fine:

func uprightAngle2<T: FloatingPoint>(_ x: T) -> T {
    if (x > T.pi / 2) && (x < 3 * T.pi / 2) { // All fine here
        return x - T.pi
    } else {
        return x
    }
}

Can anyone

A) explain why the compiler is inferring the type correctly with the integer literals, but not with the floating point literal,

B) show me the idiom to use when I can't use rationals as neither let half: T = 0.5 nor T(0.5) compile...

解决方案

The FloatingPoint protocol inherits from ExpressibleByIntegerLiteral via the inheritance chain

FloatingPoint - SignedNumeric - Numeric - ExpressibleByIntegerLiteral

and that is why the second function uprightAngle2 compiles: Values of the type T are created from the integer literals 2 and 3.

The first function uprightAngle1 does not compile because the FloatingPoint protocol does not inherit from ExpressibleByFloatLiteral, i.e. values of type T can not be created from a floating point literal like 1.5.

Possible solutions:

  • Create rational values as let half: T = 1/2. (Not let half = T(1/2), that would truncate the division result before creating the T value.)

  • Replace FloatingPoint by BinaryFloatingPoint (which inherits from ExpressibleByFloatLiteral).

For more information about the design of the floating point protocols see SE-0067 Enhanced Floating Point Protocols.

The floating point types in the Swift Standard Library (Double, Float, CGFloat, Float80) as well as CGFloat from the Core Graphics framework all conform to the BinaryFloatingPoint protocol, so this protocol is "sufficiently generic" for many applications.

这篇关于FloatingPoint协议中的文字数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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