Sdk: Add abs to Math

Created on 10 Nov 2011  路  8Comments  路  Source: dart-lang/sdk

_This issue was originally filed by domi...@google.com_


Math should have the abs method that returns the absolute value of the num passed in.

area-library closed-not-planned type-enhancement

Most helpful comment

_This comment was originally written by Francisco.M.S.Fer...@gmail.com_


I'm starting with dart and was searching for the Math.abs

Considering almost everywhere abs belongs to Math this lib will be the first place users will search, and they will be confused when they don't find it. Although I understand moving this kind of logic to num so that Math as other utilities.

Doing:
(foo+bar).abs() feels wrong
(foo-9999).abs()

While:
Math.abs(foo+bar)
Math.floor(foo+bar) feels correct.

Also as the instruction is coming first it is like saying "We are going to floor the result of: foo + bar"

All 8 comments

It's a member on 'num'.
Closing as invalid. If your issue is actually that it shouldn't be there, but on Math, please reopen.


_Added Area-Compiler, Invalid labels._

_This comment was originally written by domi...@google.com_


Having some methods on num and others on Math is quite confusing. Math has sqrt, num has abs.

I can imagine a rule where if it's an operation on a number then it should be in num, if it's on multiple nums it should be in Math, but that's violated by sqrt.

So I'll ask that this be considered a request for consistency between num and Math and that floor, ceil, truncate, round, and abs should be moved to Math.

_Removed Area-Compiler label._
_Added Area-Library, Triaged labels._

_This comment was originally written by kale.sami...@gmail.com_


I can imagine a rule where if it's an operation on a number then it should be in num, if it's on multiple nums it should be in Math, but that's violated by sqrt.

+1 for that idea. So instead of moving floor, ceil, truncate, round, and abs to Math. sqrt should move into num.

_This comment was originally written by Francisco.M.S.Fer...@gmail.com_


I'm starting with dart and was searching for the Math.abs

Considering almost everywhere abs belongs to Math this lib will be the first place users will search, and they will be confused when they don't find it. Although I understand moving this kind of logic to num so that Math as other utilities.

Doing:
(foo+bar).abs() feels wrong
(foo-9999).abs()

While:
Math.abs(foo+bar)
Math.floor(foo+bar) feels correct.

Also as the instruction is coming first it is like saying "We are going to floor the result of: foo + bar"

_This comment was originally written by dirve...@gmail.com_


I would like to echo comment #颅6. Looking in Math is the first place to look on some of these. In fact, as a newbie, I wouldn't have even thought of looking in num until finding this post. Call it a remnant of being a C++ developer, but I don't think of a double as having operations for abs, sqrt, floor, ceil, etc. That's just not intuitive for me. So Math is the first place to look.

As a potential side fix, maybe replicate some of that functionality in Math simply by redirecting functions for sqrt, abs, etc. to be the return of num.xxx(). Similar to adding this in Math:

static num abs(num value) {return value.abs();}

It shouldn't be that much work, and would save lots of newbie Dart developers lots of time hunting for the right function call.

While I can understand the pain (as a C++/Java/JavaScript programmer), we don't intend to hide the object nature of integers and doubles. Unlike the languages above, a double is a full object in Dart, and it has methods, and it makes a certain kind of sense to have "abs" as a method on all numbers.

We also don't want too much duplication. It's bad for readability - if one programmer used "d.abs()" and another uses "abs(d)", reading suffers - is it the same, or is it different, and why is it different??? - PANIC!
Reading code is much harder than writing it in the long run, so we'll keep to one way of doing this.
So, all in all, we think the disadvantages of having the same function twice outweighs the advantages.


_Removed Type-Defect label._
_Added Type-Enhancement, WontFix labels._

_This comment was originally written by @stevenroose_


+1 on this issue. Now that Dart passed 1.0, I assume this won't be changed?

Was this page helpful?
0 / 5 - 0 ratings