Coder Social home page Coder Social logo

Add Math.clamp about sdk HOT 15 CLOSED

dart-lang avatar dart-lang commented on May 11, 2024
Add Math.clamp

from sdk.

Comments (15)

DartBot avatar DartBot commented on May 11, 2024

This comment was originally written by [email protected]


Here's a patch with an implementation and tests.


Attachment:
clamp.diff (1.36 KB)

from sdk.

DartBot avatar DartBot commented on May 11, 2024

This comment was originally written by [email protected]


What should happen if lower >= upper?

from sdk.

DartBot avatar DartBot commented on May 11, 2024

This comment was originally written by [email protected]


Removed Type-Defect label.
Added Type-Enhancement label.

from sdk.

DartBot avatar DartBot commented on May 11, 2024

This comment was originally written by [email protected]


It should probably throw an exception like the functions in Date do.

Side note: should the docs for Expect.throws() mention the () => foo() syntax? I at least had to poke around for a bit to figure it out.


Attachment:
clamp_v2.diff (1.51 KB)

from sdk.

DartBot avatar DartBot commented on May 11, 2024

This comment was originally written by [email protected]


Added Area-Language, Triaged labels.

from sdk.

DartBot avatar DartBot commented on May 11, 2024

This comment was originally written by [email protected]


It's probably worth noting here that this idea was received pretty warmly over on Reddit. http://www.reddit.com/r/programming/comments/l6uwv/dart_programming_language/c2q9sbc

from sdk.

gbracha avatar gbracha commented on May 11, 2024

Removed Area-Language label.
Added Area-Library label.

from sdk.

floitschG avatar floitschG commented on May 11, 2024

We are discussing the idea. Currently we are tending towards adding it to the num interface. Ex:
x.clamp(1, 3);
instead of
Math.clamp(x, 1, 3);

from sdk.

DartBot avatar DartBot commented on May 11, 2024

This comment was originally written by [email protected]


I don't see if lower = upper as an issue. Just return lower or upper, which is almost certainly what the programmer would want. If lower > upper, than it would make sence to which lower and upper around. Although this is probably the best thing to do hear in terms of the programmers intentions, this would degrade performance(no more than throwing an exception would however).

from sdk.

DartBot avatar DartBot commented on May 11, 2024

This comment was originally written by [email protected]


My vote agrees with Comment #­8. Sorry if this is considered a "Me too".. I'd like the ability to just Star a comment. :-\

from sdk.

jmesserly avatar jmesserly commented on May 11, 2024

Also would be good to get nearlyEquals from goog.math:

http://code.google.com/p/closure-library/source/browse/trunk/closure/goog/math/math.js

from sdk.

sethladd avatar sethladd commented on May 11, 2024

cc @lrhn.
Added this to the Later milestone.

from sdk.

DartBot avatar DartBot commented on May 11, 2024

This comment was originally written by [email protected]


@clamp_v2.diff

"if (lower >= upper) throw new IllegalArgumentException();"

That should be '>', not '>='. Using the same value for both is seemingly pointless, but it's not illegal. E.g. if you want that your 5 is at least 5 and at most 5, then, yes, you can have that totally five-y 5.

It really isn't as stupid as it sounds, seriously. Imagine there is a range slider which doesn't enforce a minimum distance between both values and you just take those values and throw them at this clamp function. Would be nice if it would work, wouldn't it?

Well, with "Math.max(Math.min(val, atLeast), atMost)" this works just fine. Naturally, it should work with a built-in clamp function, too.

from sdk.

floitschG avatar floitschG commented on May 11, 2024

Set owner to @floitschG.
Added Started label.

from sdk.

floitschG avatar floitschG commented on May 11, 2024

Fixed in lib_v2, r14576.


Added Fixed label.

from sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.