KScript 文档
ZH

错误代码

KS6023

幂运算前面的负号。 先计算幂,因此 -2 ** 2 等于 -(2 ** 2),即 -4,与数学和 Python 中的规则一致。许多读者会预期结果是 4,因此应使用括号来明确意图:

<let name="loss" value="-(2 ** 2)" /> <!-- -4 because the brackets square 2 first -->
<let name="square" value="(-2) ** 2" /> <!-- 4 because the brackets square -2 -->
$loss = -(2 ** 2)             // -4 because the brackets square 2 first
$square = (-2) ** 2           // 4 because the brackets square -2

两种修复方式都会写出括号:写成 -(2 ** 2) 可保持当前的值,写成 (-2) ** 2 则会对负数进行乘方运算。像 -($side ** 2) 那样为整个乘方加括号的写法已经很清楚,不会被标记。

Edit this page on GitHub