import Complex

Section: 10 Numbers

Section: 10.1 Numeric Class Structure

Section: 10.2 Constructed Numbers

第 10 节 数值

第 10.1 节 数值类的结构

第 10.2 节 构造的数值类型

Here's a brief summary of Haskell numeric classes.

下面是 Haskell 数值类的简要总结。

Class Num

Most general numeric class. Has addition, subtraction, multiplication. Integers can be coerced to any instance of Num with fromInteger. All integer constants are in this class. Instances: Int, Integer, Float, Double, Ratio a, Complex a

类 Num

最一般的数值类。有加、减、乘。

整数可以用 fromInteger 转换为 Num 的任何实例。

所有整数常量都属于这个类。

实例:Int, Integer, Float, Double, Ratio a, Complex a

Class Real

This class contains ordered numbers which can be converted to rationals. Instances: Int, Integer, Float, Double, Ratio a

类 Real

这个类包含可以转换为有理数的、可比较大小的数。

实例:Int, Integer, Float, Double, Ratio a

Class Integral

This class deals with integer division. All values in Integral can be mapped onto Integer. Instances: Int, Integer

类 Integral

这个类处理整数除法。Integral 中的所有值都可以映射到 Integer。

实例:Int, Integer

Class Fractional

These are numbers which can be divided. Any rational number can be converted to a fractional. Floating point constants are in this class: 1.2 would be 12/10. Instances: Float, Double, Ratio a

类 Fractional

这些是可以做除法的数。任何有理数都可以转换为 fractional。

浮点常量属于这个类:1.2 就是 12/10。

实例:Float, Double, Ratio a

Class Floating

This class contains all the standard floating point functions such as sqrt and sin. Instances: Float, Double, Complex a

类 Floating

这个类包含所有标准的浮点函数,如 sqrt 和 sin。

实例:Float, Double, Complex a

Class RealFrac

These values can be rounded to integers and approximated by rationals. Instances: Float, Double, Ratio a

类 RealFrac

这些值可以四舍五入为整数,也可以用有理数近似。

实例:Float, Double, Ratio a

Class RealFloat

These are floating point numbers constructed from a fixed precision mantissa and exponent. Instances: Float, Double

类 RealFloat

这些是由固定精度的尾数和指数构造出的浮点数。

实例:Float, Double

There are only a few sensible combinations of the constructed numerics with built-in types: Ratio Integer (same as Rational): arbitrary precision rationals Ratio Int: limited precision rationals Complex Float: complex numbers with standard precision components Complex Double: complex numbers with double precision components

构造数值类型与内建类型的合理组合只有寥寥几种:

Ratio Integer(即 Rational):任意精度有理数

Ratio Int:有限精度有理数

Complex Float:分量标准精度的复数

Complex Double:分量双精度的复数

The following function works for arbitrary numerics:

下面这个函数对任意数值类型都适用:

fact :: (Num a) => a -> a
fact 0 = 1
fact n = n*(fact (n-1))

Note the behavior when applied to different types of numbers:

注意它作用于不同类型数字时的行为:

e1 :: Int
e1 = fact 6
e2 :: Int
e2 = fact 20   -- Hugs may not handle overflow gracefully!
e3 :: Integer
e3 = fact 20
e4 :: Rational
e4 = fact 6
e5 :: Float
e5 = fact 6
e6 :: Complex Float
e6 = fact 6

Be careful: values like fact 1.5 will loop.

小心:像 fact 1.5 这样的值会陷入循环。

As a practical matter, Int operations are usually faster than Integer operations. Also, overloaded functions can be much slower than non- overloaded functions. Giving a function like fact a precise typing:

fact :: Int -> Int

may yield much faster code.

从实用角度看,Int 的运算通常比 Integer 快。另外,重载函数可能比非

重载函数慢得多。给 fact 这样的函数一个精确的类型:

fact :: Int -> Int

可能会得到快得多的代码。

In general, numeric expressions work as expected. Literals are a little tricky - they are coerced to the appropriate value. A constant like 1 can be used as ANY numeric type.

一般来说,数值表达式的表现符合预期。字面量有点微妙——它们会被转换

为合适的值。像 1 这样的常量可以作为任何数值类型使用。

e7 :: Float
e7 = sqrt 2
e8 :: Rational
e8 = ((4%5) * (1%2)) / (3%4)
e9 :: Rational
e9 = 2.2 * (3%11) - 1
e10 :: Complex Float
e10 = (2 * (3:+3)) / ((1.1:+2.0) - 1)
e11 :: Complex Float
e11 = sqrt (-1)
e12 :: Integer
e12 = numerator (4%2)
e13 :: Complex Float
e13 = conjugate (4:+5.2)

A function using pattern matching on complex numbers:

一个对复数做模式匹配的函数:

mag :: (RealFloat a) => Complex a -> a
mag (a:+b) = sqrt (a^2 + b^2)
e14 :: Float
e14 = mag (1:+1)

Section: 10.3 Numeric Coercions and Overloaded Literals

第 10.3 节 数值转换与重载字面量

The Haskell type system does NOT implicitly coerce values between the different numeric types! Although overloaded constants are coerced when the overloading is resolved, no implicit coercion goes on when values of different types are mixed. For example:

Haskell 的类型系统不会在不同数值类型之间隐式转换值!虽然重载常量在

重载被确定时会被转换,但当不同类型的值混合在一起时,不会发生任何

隐式转换。例如:

f :: Float
f = 1.1
i1 :: Int
i1 = 1
i2 :: Integer
i2 = 2

All of these expressions would result in a type error (try them!):

下面这些表达式都会导致类型错误(试试!):

-- g = i1 + f
-- h = i1 + i2
-- i3 :: Int
-- i3 = i2

Appropriate coercions must be introduced by the user to allow the mixing of types in arithmetic expressions.

必须显式地引入合适的转换,才能在算术表达式中混合类型。

e15 :: Float
e15 = f + fromIntegral i1
e16 :: Integer
e16 = fromIntegral i1 + i2
e17 :: Int
e17 = i1 + fromInteger i2  -- fromIntegral would have worked too.