Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why abs() function in dart return negative number when not wrapped in parenthesis?

Tags:

dart

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

like image 378
kodebot Avatar asked Mar 27 '19 10:03

kodebot


People also ask

How do you make a negative number positive in darts?

You can do it with a division too: num /= -1; .

What is abs flutter?

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.


1 Answers

According to Operator precedence and Dart Language Specification-123.11.abs() is the same as -((123.11).abs()).

like image 200
Alexandre Ardhuin Avatar answered Oct 25 '22 05:10

Alexandre Ardhuin