简体   繁体   English

任意精确数字和Javascript,Google Web Toolkit

[英]Arbitrary precision numbers and Javascript, Google Web Toolkit

This is not really a question as I do have a work around with the issue, but I thought I'd let everyone know because it may have quite a wide impact on the way people work with the Google Web Toolkit. 这不是一个真正的问题,因为我已经解决了这个问题,但我想我会让每个人都知道,因为它可能会对人们使用Google Web Toolkit的方式产生很大的影响。

So one of the problem is with the way Google gson represents numbers in JSON. 所以问题之一就是Google gson用JSON表示数字的方式。 For example, int myInt = 2 will become "myInt":2 and long myLong = 5432198765L will become "myLong":5432198765 , and BigInteger myBI = 1310381093810938109481049128409487109378109248104098130981039810983 will become "myBI":1310381093810938109481049128409487109378109248104098130981039810983 . 例如, int myInt = 2将变为"myInt":2并且long myLong = 5432198765L将变为"myLong":5432198765 ,并且BigInteger myBI = 1310381093810938109481049128409487109378109248104098130981039810983将变为"myBI":1310381093810938109481049128409487109378109248104098130981039810983 While gson itself can deserialize this without a problem, the AutoBeans framework in GWT 2.4 in JSON will not like it. 虽然gson本身可以毫无问题地反序列化,但JSON中的GWT 2.4中的AutoBeans框架不会喜欢它。 Issue 6331 fixes it for the long representation in the upcoming GWT 2.5 release. 问题6331修复了它在即将发布的GWT 2.5版本中的长期表示。 However, issue 7555 will not be resolved because of the way Javascript number precision works. 但是,由于Javascript数字精度的工作方式, 问题7555将无法解决。

Thus, we will need to represent BigIntegers as Strings and it will work, eg, String myBIStr = new BigInteger("1310381093810938109481049128409487109378109248104098130981039810983").toString() will be represented as "myBIStr":"1310381093810938109481049128409487109378109248104098130981039810983" . 因此,我们需要将BigIntegers表示为字符串并且它将起作用,例如, String myBIStr = new BigInteger("1310381093810938109481049128409487109378109248104098130981039810983").toString()将表示为"myBIStr":"1310381093810938109481049128409487109378109248104098130981039810983" At the GWT end, this will result a String and we will have to build a BigInteger out of it. 在GWT结束时,这将产生一个String,我们将不得不构建一个BigInteger。

It all makes perfect sense why Google will not resolve 7555, but that leads me to the real open question: how does one deal with high precision numbers in Javascript? 这一切都很有道理,为什么谷歌不会解决7555,但这引出了一个真正的开放性问题:如何处理Javascript中的高精度数字?

In general, if web-based Javascript and Google Web Toolkit front-ends are to challenge native front-ends, then there could be situations where we work with arbitrary precision numbers, instead of being restricted by about the 53-bit precision that comment 3 talks about. 一般来说,如果基于Web的Javascript和Google Web Toolkit前端要挑战本机前端,那么可能会出现我们处理任意精度数的情况,而不是受到评论3的53位精度的限制。谈论。 What's even worse is that doesn't this limitation also affect node.js or any other server side Javascript? 更糟糕的是,这个限制是否也会影响node.js或任何其他服务器端的Javascript?

Is there a nice work around, especially one that uses or seamlessly works with Google Web Toolkit? 有一个很好的工作,尤其是使用或无缝使用Google Web Toolkit的工作吗?

GWT can do math on integers wider than 53 bits because it emulates Long (and BigInteger I think). GWT可以对大于53位的整数进行数学运算,因为它模拟Long(我认为是BigInteger)。 The math is slower, since it can't just use the native JS operations, but there's not a hard limit. 数学运算速度较慢,因为它不能只使用本机JS操作,但没有硬性限制。

So, GWT has already implemented the workaround, and it's built in. You just have to avoid passing numeric JSON when you need a big integer. 因此,GWT已经实现了变通方法,它已经内置。当你需要一个大整数时,你只需要避免传递数字JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM