类 浮点数和 Haskell 中的小数有什么区别?
What's the difference between the classes Floating and Fractional in Haskell?
Haskell中的Floating
和Fractional
类有什么区别?
Fractional
和 Floating
的定义可以在 Prelude 的文档中找到:
class Num a => Fractional a where
(/) :: a -> a -> a
recip :: a -> a
fromRational :: Rational -> a
Fractional numbers, supporting real division.
[...]
class Fractional a => Floating a where
pi :: a
exp :: a -> a
log :: a -> a
sqrt :: a -> a
(**) :: a -> a -> a
logBase :: a -> a -> a
sin :: a -> a
cos :: a -> a
tan :: a -> a
asin :: a -> a
acos :: a -> a
atan :: a -> a
sinh :: a -> a
cosh :: a -> a
tanh :: a -> a
asinh :: a -> a
acosh :: a -> a
atanh :: a -> a
Trigonometric and hyperbolic functions and related functions.
[...]
因此,将其翻译成英语:Fractional
是我可以为其定义除法的任何一种数字:
(/) :: Fractional a => a -> a -> a
例如 浮点数 数字就是这种情况,分数 也是如此(其中分数有 分子和分母)。 Int
不是这种情况,因为如果将 Int
除以 Int
并不总是产生 Int
(从技术上讲,计算机上的浮点除法并不精确,但那是另一回事了)。
Fractional
个数字的子集是 Floating
个定义了三角函数的数字。例如,分数的 sin
不可能总是产生分数:sin
被定义为无限序列的和。它仅适用于非常有限的情况(如 sin 0
)。基本上,计算机上唯一定义了三角函数(近似)的数字是浮点数。
非常粗略:
Fractional
是 class 类型,可以表示(准确地或至少在适当的近似值中)任何 rational number。
换句话说,就是class的数字类型有除法运算;因为它是 Num
的子 class,因此类型必须包含有理数。
Floating
是柯西意义下封闭的数型class,即complete spaces. This is necessary to do any sort of calculus. The methods of the Floating
class are functions that are mathematically defined as limits, namely infinite sums (which are the limits of the sequence of partial sums of taylor series).
Since you can define the real numbers as limits of sequences of rational numbers and because again Floating
is a subclass of Fractional
, any Floating
type is able to represent (again, at least to a decent approximation) any real number.
可视化差异的一个好方法是通过拓扑:Floating
类型是 connected spaces,即它们形成一个连续体。这对于浮点数意味着什么:每个值都被理解为实数的整体 interval(因为 floating-point 总是有一些不确定性)。当您并排放置这些间隔时,您可以毫无间隙地平铺整个实数(至少到 ±10300)。
相比之下,一些 Fractional
类型没有连接。特别地,Rational
可以正好表示它所有的(rational-number)值,所以每个值只是一个“无限小的点”。您永远无法用这些点覆盖整条实线,并且您无法计算像 sin
或 log
这样的函数,因为这些函数的结果通常是 non-rational 实数。
值得深思一下这个“合适的近似值”是什么意思。 Haskell 标准没有对此进行定义。这个关于每个浮点数代表整个实数区间的故事很好地捕捉了 IMO。更一般地说,我们可以说:Num
/Fractional
/Floating
是代表 equivalance classes of integer/rational/real numbers. In fact, these classes need not even be “small” intervals: in particular the finite types like Word32
or the standard Int
can be understood in a modular arithmetic 意义的 class 类型,表现为 [=27] 这样的结果=],即等价 classes 是数字空间乘以 264.
的倍数
在 Integer
或 Rational
的情况下,等价 classes 实际上只包含一个元素,即数字表示 完全 .对于实数,这实际上也是可能的,但更棘手,它被称为精确实数算术。 aern 等库可以执行此操作。
Haskell中的Floating
和Fractional
类有什么区别?
Fractional
和 Floating
的定义可以在 Prelude 的文档中找到:
class Num a => Fractional a where (/) :: a -> a -> a recip :: a -> a fromRational :: Rational -> a
Fractional numbers, supporting real division.
[...]
class Fractional a => Floating a where pi :: a exp :: a -> a log :: a -> a sqrt :: a -> a (**) :: a -> a -> a logBase :: a -> a -> a sin :: a -> a cos :: a -> a tan :: a -> a asin :: a -> a acos :: a -> a atan :: a -> a sinh :: a -> a cosh :: a -> a tanh :: a -> a asinh :: a -> a acosh :: a -> a atanh :: a -> a
Trigonometric and hyperbolic functions and related functions.
[...]
因此,将其翻译成英语:Fractional
是我可以为其定义除法的任何一种数字:
(/) :: Fractional a => a -> a -> a
例如 浮点数 数字就是这种情况,分数 也是如此(其中分数有 分子和分母)。 Int
不是这种情况,因为如果将 Int
除以 Int
并不总是产生 Int
(从技术上讲,计算机上的浮点除法并不精确,但那是另一回事了)。
Fractional
个数字的子集是 Floating
个定义了三角函数的数字。例如,分数的 sin
不可能总是产生分数:sin
被定义为无限序列的和。它仅适用于非常有限的情况(如 sin 0
)。基本上,计算机上唯一定义了三角函数(近似)的数字是浮点数。
非常粗略:
Fractional
是 class 类型,可以表示(准确地或至少在适当的近似值中)任何 rational number。
换句话说,就是class的数字类型有除法运算;因为它是Num
的子 class,因此类型必须包含有理数。Floating
是柯西意义下封闭的数型class,即complete spaces. This is necessary to do any sort of calculus. The methods of theFloating
class are functions that are mathematically defined as limits, namely infinite sums (which are the limits of the sequence of partial sums of taylor series).
Since you can define the real numbers as limits of sequences of rational numbers and because againFloating
is a subclass ofFractional
, anyFloating
type is able to represent (again, at least to a decent approximation) any real number.
可视化差异的一个好方法是通过拓扑:Floating
类型是 connected spaces,即它们形成一个连续体。这对于浮点数意味着什么:每个值都被理解为实数的整体 interval(因为 floating-point 总是有一些不确定性)。当您并排放置这些间隔时,您可以毫无间隙地平铺整个实数(至少到 ±10300)。
相比之下,一些 Fractional
类型没有连接。特别地,Rational
可以正好表示它所有的(rational-number)值,所以每个值只是一个“无限小的点”。您永远无法用这些点覆盖整条实线,并且您无法计算像 sin
或 log
这样的函数,因为这些函数的结果通常是 non-rational 实数。
值得深思一下这个“合适的近似值”是什么意思。 Haskell 标准没有对此进行定义。这个关于每个浮点数代表整个实数区间的故事很好地捕捉了 IMO。更一般地说,我们可以说:Num
/Fractional
/Floating
是代表 equivalance classes of integer/rational/real numbers. In fact, these classes need not even be “small” intervals: in particular the finite types like Word32
or the standard Int
can be understood in a modular arithmetic 意义的 class 类型,表现为 [=27] 这样的结果=],即等价 classes 是数字空间乘以 264.
在 Integer
或 Rational
的情况下,等价 classes 实际上只包含一个元素,即数字表示 完全 .对于实数,这实际上也是可能的,但更棘手,它被称为精确实数算术。 aern 等库可以执行此操作。