FloatingPoint协议中的文字数字 [英] Literal numbers in FloatingPoint protocol
问题描述
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)
编译...
ExpressibleByIntegerLiteral
通过继承链
FloatingPoint - SignedNumeric - 数字 - ExpressibleByIntegerLiteral
这就是为什么第二个函数 uprightAngle2
编译:值为
的类型 T
由整数文字 2
和 3
创建。
第一个功能on 创建有理数值为 有关浮点数 Swift标准库中的浮点类型( Having not much to do over the holidays, I finally got round to updating my maths library to use the however, this works fine: 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 The and that is why the second function The first function Possible solutions: Create rational values as Replace 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 ( 这篇关于FloatingPoint协议中的文字数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! 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 。
Double
, Float
, CGFloat $ Core Graphics框架中的c $ c>,
Float80
)以及
CGFloat
都符合
BinaryFloatingPoint
协议,因此该协议对于许多应用程序而言足够
通用。 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
}
}
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
}
}
let half: T = 0.5
nor T(0.5)
compile...FloatingPoint
protocol inherits from ExpressibleByIntegerLiteral
via the inheritance chainFloatingPoint - SignedNumeric - Numeric - ExpressibleByIntegerLiteral
uprightAngle2
compiles: Values
of the type T
are created from the integer literals 2
and 3
.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
.
let half: T = 1/2
. (Not let half = T(1/2)
,
that would truncate the division result before creating the T
value.)FloatingPoint
by BinaryFloatingPoint
(which inherits
from ExpressibleByFloatLiteral
).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.