Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Clarify the algorithm to be fully consistent with CSS clamp() #3

Closed
wants to merge 1 commit into from

Conversation

yisibl
Copy link

@yisibl yisibl commented Jan 17, 2024

Note: This may not be the same as other languages, e.g. in C++, C#, Rust, an error is thrown when MIN is greater than MAX.

C#

image

Rust

image

@@ -14,31 +14,15 @@ A [clamping function](https://en.wikipedia.org/wiki/Clamping_(graphics)) constra

The primary motivation for this function is to bring parity with the [CSS function][css-clamp] of the same name. It will be useful when using it in conjunction with the [CSS Painting API](https://developer.mozilla.org/en-US/docs/Web/API/CSS_Painting_API).

## Examples
That is, given `Math.clamp(VAL, MIN, MAX)`, it represents exactly the same value as `Math.max(MIN, Math.min(VAL, MAX))`.
Copy link
Collaborator

@Richienb Richienb Jan 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
That is, given `Math.clamp(VAL, MIN, MAX)`, it represents exactly the same value as `Math.max(MIN, Math.min(VAL, MAX))`.
That is, `Math.clamp(val, min, max)` is exactly equal to `Math.max(min, Math.min(val, max))`.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is copied from the CSS specification, which I think makes it more terminological.

Copy link
Collaborator

@Richienb Richienb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should keep the alternative implementations so that we may demonstrate their inefficacy. We should be listing both orderings of the max and min function.


- Nesting [`Math.min`][math-min] and [`Math.max`][math-max] calls

```js
function clamp(number, minimum, maximum) {
return Math.min(Math.max(number, minimum), maximum);
function clamp(value, minimum, maximum) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following the implementation, I think it should be val, min and max everywhere. Do they name it value in the spec more often?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The specification uses highest and lowest: https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-math.max

Do we need to change to these two keywords?

}

return number;
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should keep the alternative implementations so that we may demonstrate their inefficacy. We should be listing both orderings of the max and min function.

Do you mean to keep this function?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we should keep this function in the readme for the purpose of demonstration

@@ -14,31 +14,15 @@ A [clamping function](https://en.wikipedia.org/wiki/Clamping_(graphics)) constra

The primary motivation for this function is to bring parity with the [CSS function][css-clamp] of the same name. It will be useful when using it in conjunction with the [CSS Painting API](https://developer.mozilla.org/en-US/docs/Web/API/CSS_Painting_API).
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm skeptical of the use of CSS behavior here, which is probably not available in ES from a programming language level.

I invite @hax experts to come and give further guidance.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that the motivation behind this proposal is to provide a similar functionality to the CSS function clamp in JavaScript. However:

  1. The JavaScript version of clamp does not support CSS units. And it seems JS will never support them because of the death of https://github.com/tc39/proposal-extended-numeric-literals .
  2. The parameter order in the draft JavaScript version is (value, min, max), whereas the CSS version is (min, value, max), which is quite confusing.
  3. The CSS version's prioritization of the minimum value is based on accessibility needs in CSS practices. However, this behavior might not be easily mapped to general-purpose programming languages, as pointed out in this pull request. It seems to be quite inconsistent with the behavior of other programming languages.

This was referenced Jan 17, 2024
@Richienb Richienb marked this pull request as draft January 17, 2024 12:53
@Richienb
Copy link
Collaborator

Will revisit once we have figured out what we actually want the argument names and order to be.

@yisibl yisibl closed this Feb 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants