为什么Erlang的浮标不允许“无限” [英] Why is 'Infinity' not allowed in Erlang's floats?

查看:159
本文介绍了为什么Erlang的浮标不允许“无限”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



一些可能的浮点数:




  • 1.2345

  • 1.0e10

  • 1.0e-42



Erlang支持在Erlang中,NaN( nan。)(但是我还没有发现一种输出 nan 本身的方法) p>

但是,Erlang不支持 Infinity 。虽然像IEEE-754这样的通用标准规定,在执行像 1.0 / 0.0 这样的事情时,应该返回 Infinity ,而是Erlang抛出一个错误的算术错误



同样的情况发生在尝试使浮动太大 code> 1.0e400 。



这可能是一些(历史?)原因。

解决方案

Erlang用浮动(不太符合IEEE 754 binary64)做各种各样的坏事物:confLates -0和+0到0,不做+ SNaNs,+ -QNaNs,+ -denormals或+ -infinity。



对于具有精确错误处理的实际科学计算,可能值得使用另一种语言, code> Port ,做一些RPC或编写C / C ++ NIF。



参考:

更新:我写了一个微不足道的ieee754模块 here 它将binary64浮点数分类并转换为Erlang支持的那些。


Erlang (and by extension Elixir) supports floating-point numbers.

Some possible Floats:

  • 1.2345
  • 1.0e10
  • 1.0e-42

Erlang supports NaN (nan. in Erlang) (I am however yet to discover a method that outputs nan itself).

However, Erlang does not have support for Infinity. While common standards like IEEE-754 state that one should return Infinity when doing things like 1.0/0.0, instead, Erlang throws a bad arithmetic error.

The same happens when attempting to make floats that are 'too large' like 1.0e400.

There's probably some (historical?) reason behind this.

解决方案

Erlang does all sorts of "bad" things with floats (not quite IEEE 754 binary64): conflates -0 and +0 to 0, doesn't do +-SNaNs, +-QNaNs, +-denormals or +-infinity.

For actual, scientific computations with precise error handling, it's probably worth dropping into another language either with a Port, doing some RPC or writing C/C++ NIF.

Reference: http://steve.hollasch.net/cgindex/coding/ieeefloat.html

Update: I wrote a trivial ieee754 module here which classifies binary64 floats and converts those that Erlang supports.

这篇关于为什么Erlang的浮标不允许“无限”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆