简体   繁体   English

Haskell中的“浮动”和“分数”有什么区别?

[英]What's the difference between “float” and “fractional” in Haskell?

Prelude> let c=[1.0,2.0]
Prelude> :t c
c :: Fractional t => [t]

I'd expect "c" to be a list of either Num or Float. 我希望“ c”是Num或Float的列表。 Why Fractional? 为什么是分数? Is there any implicit type conversion going on here in Haskell? 在Haskell中是否有任何隐式类型转换?

You can make it happen: 您可以实现:

> :set -XNumDecimals
> let c=[1.0,2.0]
> :t c
c :: Num t => [t]

The answer to "why Fractional " is basically "because the Report says so": 对于“为什么是Fractional ”的答案基本上是“因为该报告是这样说的”:

 float → decimal . decimal [exponent] | decimal exponent 

A floating literal stands for an application of fromRational to a value of type Rational (that is, Ratio Integer ). 浮动文字表示将fromRational应用于Rational类型的值(即Ratio Integer )。 Given the typings: 给定类型:

 fromInteger :: (Num a) => Integer -> a fromRational :: (Fractional a) => Rational -> a 

integer and floating literals have the typings (Num a) => a and (Fractional a) => a , respectively. 整数和浮点文字分别具有(Num a) => a(Fractional a) => a

I would guess the Report says so because it is a nice simple rule to explain: no dot/exponent, it's Num -polymorphic, yes dot/exponent, it's Fractional -polymorphic. 我猜的报告是这样说的,因为它是一个很好的简单的规则来解释:没有点/指数,这是Num -polymorphic,是点/指数,它的Fractional -polymorphic。

Two relevant bits are: 两个相关的位是:
https://www.haskell.org/onlinereport/haskell2010/haskellch2.html#x7-190002.5 https://www.haskell.org/onlinereport/haskell2010/haskellch2.html#x7-190002.5
https://www.haskell.org/onlinereport/haskell2010/haskellch6.html#x13-1360006.4.1 https://www.haskell.org/onlinereport/haskell2010/haskellch6.html#x13-1360006.4.1

There is no implicit type conversion in Haskell. Haskell中没有隐式类型转换。 Numeric literals have polymorphic types. 数值文字具有多态类型。

> :t 3
3 :: Num a => a

> :t 3.5
3.5 :: Fractional a => a

Standard Haskell treats any number with a decimal point as fractional, even if the fractional digits are all 0. It also treats anything written in scientific notation as fractional. 标准Haskell会将带小数点的任何数字都视为小数,即使小数位数都为0。也会将以科学计数法表示的任何内容视为小数。 There's a GHC extension you can use to be more polymorphic with such representations if you like. 如果您愿意的话,可以使用GHC扩展名,使其具有更多的多态性。

As for the question in your title, Float is a concrete type ( Float :: * ), while Fractional is a type class ( Fractional :: * -> Constraint ). 至于标题中的问题, Float是具体类型( Float :: * ),而Fractional是类型类( Fractional :: * -> Constraint )。 You can write lots of functions that will work with all sorts of Fractional or RealFrac or RealFloat types without having to worry about the exact representation. 您可以编写许多可与各种FractionalRealFracRealFloat类型一起使用的函数,而不必担心确切的表示形式。

Side note: Float is a special-purpose type, for specialized algorithms, formats, etc., and certain cases where compact representation of arrays is more important than precision or numerical stability. 旁注: Float是一种专用类型,用于专用算法,格式等,在某些情况下,数组的紧凑表示比精度或数值稳定性更重要。 When you want floating point, the one you usually want is Double . 当您需要浮点数时,通常需要的是Double

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM