为什么没有括在括号中时,dart中的abs()函数返回负数? [英] why abs() function in dart return negative number when not wrapped in parenthesis?

查看:96
本文介绍了为什么没有括在括号中时,dart中的abs()函数返回负数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆