Skip to content

Consider updating Math.Clamp to not throw when min is greater than max #119256

@jeffhandley

Description

@jeffhandley

As noted by @tannergooding:

It's pretty typical for Clamp APIs to follow a well-specified order for mismatched min/max, as it's far more efficient and is useful for a number of scenarios. -- We end up doing just this for the vector APIs and it correspondingly can bleed into other scenarios, like Tensors

The typical behavior is Min(Max(value, min), max) which matches what hardware typically does for such instructions, what HLSL and GLSL do, etc. -- Effectively this returns max if min/max are incorrectly ordered.

Default behavior (min/max are correctly ordered)

value min max result
1 2 3 2
2 1 3 2
3 1 2 2

Swapped behavior (Min(Max(value, min), max))

value min max result
1 3 2 2
2 3 1 1
3 2 1 1

Swapped behavior (Max(Min(value, max), min))

value min max result
1 3 2 3
2 3 1 3
3 2 1 2

Originally posted by @tannergooding in #118824 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions