为什么没有括在括号中时,dart中的abs()函数返回负数? [英] why abs() function in dart return negative number when not wrapped in parenthesis?
问题描述
abs()
函数在调用负数文字时无效.
var y = -123.11.abs();//打印-123.11
但其他功能,例如 floor()
可以正常工作
var y = -123.11.floor();//打印-123
如果我将负数文字括在括号中,则效果很好
var y =(-123.11).abs();//打印123.11
感谢您对了解此行为的任何帮助.
我使用的dart版本是 Dart VM版本:"windows_x64"上的2.2.1-dev.0.0.flutter-571ea80e11(星期一3月4日19:30:53 2019 +0000)
>
更新:注意: floor()
应用于@HighPerformanceMark
根据运算符优先级和 Dart语言规范 -123.11.abs()
与-((123.11).abs())
相同.
The abs()
function has no effect When calling on negative number literal.
var y = -123.11.abs(); // prints -123.11
but other functions, for example floor()
works fine
var y = -123.11.floor(); // prints -123
If I wrap the negative number literal in parenthesis it works fine
var y = (-123.11).abs(); // prints 123.11
Any help to understand this behaviour is appreciated.
The dart version I use is Dart VM version: 2.2.1-dev.0.0.flutter-571ea80e11 (Mon Mar 4 19:30:53 2019 +0000) on "windows_x64"
Update:
Note: the floor()
does not work correctly when applied on negative number as pointed by @HighPerformanceMark
According to Operator precedence and Dart Language Specification-123.11.abs()
is the same as -((123.11).abs())
.
这篇关于为什么没有括在括号中时,dart中的abs()函数返回负数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!