大数-JavaScript中的数学 [英] Large numbers - Math in JavaScript

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

问题描述

我正在开发3D太空游戏,该游戏使用了大量的数学公式,导航,缓和效果,旋转,行星之间的巨大距离,物体质量等等...

我的问题是,使用数学这样做的最佳方法是什么?我应该将所有内容都计算为整数并获得真正的大整数(超过20个数字),还是使用小数和小数点.

根据我的经验,使用带小数位的数字时数学不准确,当使用带小数位的数字时会引起奇怪的行为.

解决方案

我会避免使用小数.他们已经知道精确的问题: http://floating-point-gui.de/


我建议使用整数,但是如果您需要使用非常大的整数,我建议使用较大的数字或较大的整数库,例如其中之一:

缺点是,您必须使用这些数字对象及其方法,而不是原始的Number类型和标准JS运算符,但是对大数进行操作将具有更大的灵活性.

修改:
正如 le_m 指出的,另一个缺点是速度.库方法的运行速度不如本机运算符.您必须自己进行测试,看看性能是否可以接受.

I'm developing a 3D space game, which is using alot of math formulas, navigation, ease effects, rotations, huge distances between planets, objects mass, and so on...

My Question is what would be the best way in doing so using math. Should I calculate everything as integers and obtain really large integers(over 20 digits), or use small numbers with decimals.

In my experience, math when using digits with decimals is not accurate, causing strange behavior when using large numbers with decimals.

解决方案

I would avoid using decimals. They have known issues with precision: http://floating-point-gui.de/


I would recommend using integers, though if you need to work with very large integers I would suggest using a big number or big integer library such as one of these:

The downside is you have to use these number objects and their methods rather than the primitive Number type and standard JS operators, but you'll have a lot more flexibility with operating on large numbers.

Edit:
As le_m pointed out, another downside is speed. The library methods won't run as fast as the native operators. You'll have to test for yourself to see if the performance is acceptable.

这篇关于大数-JavaScript中的数学的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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