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

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

问题描述

我的网站上有一些输入表示浮点数,最多十位精度(十进制).在某些时候,在客户端验证代码中,我需要比较这些值中的几个以查看它们是否相等,并且在这里,正如您所期望的,IEEE754 的内在函数使这个简单的检查失败,例如(2.0000000000==2.0000000001) = 真.

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.

我可以将点的每一边的浮点数分解为两个 long,使每一边的长度为 64 位并手动进行比较,但它看起来太丑了!

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!

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

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

提前致谢!

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

PS: A GWT based solution has a ++

推荐答案

http://code.google.com/p/gwt-math/.

但是,我警告你,它是 java->javascript 自动转换的 java.BigDecimal(实际上是旧的 com.ibm.math.BigDecimal)的 GWT jsni 覆盖.

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).

它有效,但速度不快.(也不精益.它将为您的项目增加 7 万美元).

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天全站免登陆