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
You can do it with a division too: num /= -1; .
abs method Null safety num abs() The absolute value of this number. The absolute value is the value itself, if the value is non-negative, and -value if the value is negative. Integer overflow may cause the result of -value to stay negative.
According to Operator precedence and Dart Language Specification-123.11.abs()
is the same as -((123.11).abs())
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With