简体   繁体   English

平台之间的浮点数是否一致?

[英]Are floating points consistent between platforms?

I'm building a multi-client javascript game which includes very large flocks/swarms of units of located on a Cartesian plane - location, vectors and acceleration rates are all very finely tuned and constantly affected by each other and other actors - and as such are stored with javascript floats.我正在构建一个多客户端 javascript 游戏,其中包括位于笛卡尔平面上的非常大的单位群/群 - 位置、矢量和加速度都经过非常精细的调整,并不断受到彼此和其他演员的影响 - 因此与 javascript 浮点数一起存储。

For various reasons (speed of game functions and bandwidth limits) much of the logic is done in parallel on client computers running identical games based on common inputs/instructions.由于各种原因(游戏功能的速度和带宽限制),大部分逻辑是在基于公共输入/指令运行相同游戏的客户端计算机上并行完成的。

I'm concerned that some permutations of [browser/OS/hardware] will store floats differently in memory which will result in marginal drift between game versions.我担心 [浏览器/操作系统/硬件] 的某些排列会在 memory 中以不同的方式存储浮点数,这将导致游戏版本之间的边际漂移。

The traditional issues with floats are well documented and I'm not concerned with using them - they're clearly the best degree of precision for my purposes.浮点数的传统问题有据可查,我不关心使用它们——它们显然是我的目的的最佳精度。 (eg (例如

let x = .1;
let y = .2;

if (x+y == .3){
    console.log("the same");
} else {
    console.log("not the same: x+y = " + (x+y));
}
//output >>> "not the same: x+y = 0.30000000000000004"

I've looked around a bit and even well received answers such as this: How to deal with floating point number precision in JavaScript?我环顾四周,甚至得到了很好的答案,例如: 如何处理 JavaScript 中的浮点数精度? don't cover my question exactly.不要完全涵盖我的问题。 This, and similar questions tend to deal with how to 'suppress rounding errors" or "ensure precision" etc.这个以及类似的问题往往涉及如何“抑制舍入误差”或“确保精度”等。

I'm not looking for 'accuracy' as such - but consistency - thoughts welcome.我不是在寻找“准确性”本身——而是一致性——欢迎想法。

Yes, IEE 754 is well specified, and is referenced in the ECMA262 specification.是的, IEE 754已被详细说明,并在 ECMA262 规范中被引用。

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

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