javascript 为什么 0.-5 评估为 -5?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54859228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:27:46  来源:igfitidea点击:

Why does 0.-5 evaluate to -5?

javascriptnumbers

提问by ocomfd

Suppose I write 0.5as 0.-5in unexpected way, but it can still run. What does 0.in 0.-5do so that it can still run and evaluates to -5?

假设我写的0.50.-5在意想不到的方式,但它仍然可以运行。是什么0.0.-5这样做,它仍然可以运行,并评估为-5?

I also tried alert(0.-5+1)which prints -4, does JavaScript ignore 0.in 0.-5?

我也试过alert(0.-5+1)它打印-4,不忽略的JavaScript0.0.-5

回答by CertainPerformance

Trailing digits after a .are optional:

a 后的尾随数字.是可选的:

console.log(0. === 0); // true

So

所以

0.-5

evalutes to

评价为

0 - 5

which is just -5. Similarly,

这只是-5. 相似地,

0.-5+1

is

0 - 5 + 1

which is

这是

-5 + 1

or -4.

-4

回答by Salman A

0.-5could be successfully parsed as 0.[1], -and 5. Below is the abstract syntax tree for the expression generated by AST explorer:

0.-5可以成功地解析为0.[1] -5。下面是 AST explorer 生成的表达式的抽象语法树:

Parse tree generated by AST explorer

AST 资源管理器生成的解析树

This (in an unexpected way) is valid JavaScript and evaluates to -5.

这(以一种意想不到的方式)是有效的 JavaScript 并且计算结果为-5.



[1]According to the grammar for numeric literalsthe decimal digits and exponent parts are optional:

[1]根据数字文字的语法,十进制数字和指数部分是可选的:

NumericLiteral ::
??DecimalLiteral
??[...]

DecimalLiteral ::
??DecimalIntegerLiteral . DecimalDigitsoptExponentPartopt

NumericLiteral ::
??DecimalLiteral
??[...]

DecimalLiteral ::
??DecimalIntegerLiteral 。DecimalDigits选择ExponentPart选择

回答by Charlie

In JS you can express a number with optional decimal point.

在 JS 中,你可以用可选的小数点来表示一个数字。

x = 5.;    //5
x = 5. + 6.   //11

And as of Tvde1's comment, any Number method can be applied too.

根据 Tvde1 的评论,也可以应用任何 Number 方法。

5..toString()

This syntax let us run the Number functions without parentheses.

这个语法让我们可以在没有括号的情况下运行 Number 函数。

5.toString() //error
(5).toString() //good
5..toString() //good
5 .toString() // awesome

See thisquestion to find out why.

请参阅问题以了解原因。

回答by Sean_999

I would think that the real answer is not about the decimal point, but is about the minus sign: isn't that going to be interpreted as an operator if it is preceded by anything that looks like a number?

我认为真正的答案不是关于小数点,而是关于减号:如果它前面有任何看起来像数字的东西,难道它不会被解释为运算符吗?

回答by Κωλζαρ

console.log(0. - 5)      // -5
console.log(0 - 5)       // -5
console.log('0.' - 5)    // -5
console.log('0' - 5)     // -5
console.log(0.-5 === -5) // true

'0.' or '0' is the same in JavaScript because the type is unique for numbers, called Number. The minus operator is between Numbers, try always to convert what you pass to a Number. In Python is different first is a Float and the second an Integer because it has several types.

'0。' 或 '0' 在 JavaScript 中是相同的,因为该类型对于数字来说是唯一的,称为 Number。减号运算符位于数字之间,请始终尝试将传递的内容转换为数字。在 Python 中,第一个是 Float,第二个是 Integer,因为它有多种类型。