Skip to content

Swapped parameter behaviour for constrain is not obvious #3

Closed
@lesamouraipourpre

Description

@lesamouraipourpre

The behaviour when the min_value and max_value parameters are swapped is non-obvious.

from adafruit_simplemath import constrain

print(constrain(-5, 0, 10))  # Outputs 0, as expected
print(constrain(5, 0, 10))  # Outputs 5, as expected
print(constrain(15, 0, 10))  # Outputs 10, as expected

print(constrain(-5, 10, 0))  # Outputs 10
print(constrain(5, 10, 0))  # Outputs 10
print(constrain(15, 10, 0))  # Outputs 10

I think it should either

  • raise an exception if they are swapped
  • auto-swap the values
  • add an exception parameter defaulted to False, which will auto-swap if False and raise an exception if True

My personal preference is to auto-swap the value, because it 'just works'.

I can create a PR if there is a consensus reached.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions