Haskell中的高精度浮点数? [英] High precision floating point numbers in Haskell?
本文介绍了Haskell中的高精度浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道Haskell具有原生数据类型,它允许你有非常大的整数,例如
>>让x = 131242358045284502395482305
>> x
131242358045284502395482305
按预期工作。我想知道是否有一个类似的大精度浮点本地结构,我可以使用,所以像
>> ;让x = 5.0000000000000000000000001
>> x
5.0000000000000000000000001
是可能的。如果我在Haskell中输入它,如果超过15个小数位(双精度),它会截断为5。 解决方案
取决于您正在寻找什么:
-
浮动
和Double
- 几乎所有你知道的和来自Floats和Doubles的所有其他语言的Love。 haskell.org/packages/archive/base/latest/doc/html/Prelude.html#t:Rationalrel =noreferrer> -
FixedPoint
- 此包提供任意大小的定点值。例如,如果您想要一个由64个整数位和64个小数位表示的数字,您可以使用FixedPoint6464
。如果你想要一个1024位整数和8位小数的数字,那么使用$(mkFixedPoint 1024 8)
来产生类型FixedPoint1024_8 $ c $编辑:是的,我刚刚了解了上面提到的
数字
包 - 非常酷。 b $ b
Rational
这是比率
整数
s I know Haskell has native data types which allow you to have really big integers so things like
>> let x = 131242358045284502395482305
>> x
131242358045284502395482305
work as expected. I was wondering if there was a similar "large precision float" native structure I could be using, so things like
>> let x = 5.0000000000000000000000001
>> x
5.0000000000000000000000001
could be possible. If I enter this in Haskell, it truncates down to 5 if I go beyond 15 decimal places (double precision).
解决方案
Depending on exactly what you are looking for:
Float
andDouble
- pretty much what you know and "love" from Floats and Doubles in all other languages.Rational
which is aRatio
ofInteger
sFixedPoint
- This package provides arbitrary sized fixed point values. For example, if you want a number that is represented by 64 integral bits and 64 fractional bits you can useFixedPoint6464
. If you want a number that is 1024 integral bits and 8 fractional bits then use$(mkFixedPoint 1024 8)
to generate typeFixedPoint1024_8
.- EDIT: And yes, I just learned about the
numbers
package mentioned above - very cool.
这篇关于Haskell中的高精度浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文