我建议使用整数,但是如果您需要使用非常大的整数,我建议使用较大的数字或较大的整数库,例如其中之一: http://jsfromhell.com/classes/bignumber https://silentmatt.com/biginteger/ 缺点是,您必须使用这些数字对象及其方法,而不是原始的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:http://jsfromhell.com/classes/bignumberhttps://silentmatt.com/biginteger/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中的数学的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-22 20:05