任意精度JavaScript上的浮点数 [英] Arbitrary precision Float numbers on JavaScript

查看:123
本文介绍了任意精度JavaScript上的浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的站点上有一些输入,代表了浮点数,最多可以有十位精度数字(十进制)。在某些情况下,在客户端验证代码中,我需要比较一些值,看看它们是否相等,在这里,正如你所期望的那样,IEEE754的内在特性使得这种简单的检查失败, 2.0000000000 == 2.0000000001)= true。



我可能会在点的两边打破浮点数,每边长64位,比较手动,但它看起来很丑!

任何体面的Javascript库处理任意(或至少保证)的精度浮点数在Javascript?



在此先感谢!
$ b

PS:基于GWT的解决方案有一个++

http中有GWT-MAT​​H库://code.google.com/p/gwt-math/ 。然而,我警告你,这是一个GWT jsni覆盖的java- > java自动转换的java.BigDecimal(实际上t他老com.ibm.math.BigDecimal)。

它的工作原理,但很快它不是。 (不会瘦的,它会在你的项目中填充一个好的70K)。

在我的工作场所,我们正在研究一个固定点的简单小数点,但没有什么值得发布的。 :(


I have some inputs on my site representing floating point numbers with up to ten precision digits (in decimal). At some point, in the client side validation code, I need to compare a couple of those values to see if they are equal or not, and here, as you would expect, the intrinsics of IEEE754 make that simple check fails with things like (2.0000000000==2.0000000001) = true.

I may break the floating point number in two longs for each side of the dot, make each side a 64 bit long and do my comparisons manually, but it looks so ugly!

Any decent Javascript library to handle arbitrary (or at least guaranteed) precision float numbers on Javascript?

Thanks in advance!

PS: A GWT based solution has a ++

解决方案

There is the GWT-MATH library at http://code.google.com/p/gwt-math/.

However, I warn you, it's a GWT jsni overlay of a java->javascript automated conversion of java.BigDecimal (actually the old com.ibm.math.BigDecimal).

It works, but speedy it is not. (Nor lean. It will pad on a good 70k into your project).

At my workplace, we are working on a fixed point simple decimal, but nothing worth releasing yet. :(

这篇关于任意精度JavaScript上的浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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