简体   繁体   English

如何解决这个模数运算符不准确的问题javascript?

[英]How to solve this problem of inaccurate modulus operators javascript?

Is there a way to solve this problem?有没有办法解决这个问题? I have tried it on a console and the results are wrong, but technically the results are correct我在控制台上试过,结果是错误的,但从技术上讲,结果是正确的

Experiment on the console:在控制台上实验:

In C and C++, the remainder operator accepts only integral operands;在 C 和 C++ 中,余数运算符只接受整数操作数; in ECMAScript, it also accepts floating-point operands.在 ECMAScript 中,它还接受浮点操作数。 ECMA Standands ECMA 标准

The result of a floating-point remainder operation as computed by the % operator is not the same as the “remainder” operation defined by IEEE 754-2008. % 运算符计算的浮点余数运算的结果与 IEEE 754-2008 定义的“余数”运算不同。

The IEEE 754-2008 “remainder” operation computes the remainder from a rounding division, not a truncating division, and so its behaviour is not analogous to that of the usual integer remainder operator. IEEE 754-2008“余数”运算计算取整除法的余数,而不是截除除法,因此其行为与通常的整数余数运算符的行为不同。

console.log(Math.floor(1%0.01)===0)

Instead the ECMAScript language defines % on floating-point operations to behave in a manner analogous to that of the Java integer remainder operator;相反,ECMAScript 语言在浮点运算上定义了 % 以类似于 Java 整数余数运算符的行为方式; this may be compared with the C library function fmod.这可以与 C 库函数 fmod 进行比较。

使用 Math.floor()

 console.log(Math.floor(1%0.01)===0)

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

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