How do I ensure a number is within a range?

quant

Suppose I have some value:

double x;

and I want to confine it to some range [a, b] such that the resulting value is within that range:

double confine(double x, double a, double b)
{
  if (x < a) return a;
  else if (x > b) return b;
  return x;
}

Is there a single boost or STL function that can do this for me?

chris

Yes, Boost Algorithm has clamp:

double clamped = clamp(x, a, b);

It requires only operator< or a custom comparator, and guarantees that it is called only once or twice. The documentation points out that with double and other floating-point types, NaN could cause unexpected results.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I use the RandomRangedNumberCustomization in Autofixture to ensure parameters are in a certain range?

From Dev

Using Python how do I generate a random number within a range for each row in Pandas dataframe?

From Dev

Using Python how do I generate a random number within a range for each row in Pandas dataframe?

From Dev

How do I generate a random range of numbers within another range?

From Dev

How can I generate a random number within a range in Rust?

From Dev

How can I count an SQL field for the number of results within a range?

From Dev

How to I validated an input for being an Int and within a number range?

From Dev

How do I find how many days are within a date range that are within another date range in PHP?

From Dev

How do I get a random number with a negative number in range?

From Dev

How do I get a cell's position within a range?

From Dev

How do I assign a value in R if within a certain range of time?

From Dev

How do I retrieve data that is within a specific date range?

From Dev

How do I calculate an average of a range from a series within in a dataframe?

From Dev

How do I delete messages specified by a number range?

From Dev

How Do I GET Number Range with a SPACE From A PHP String?

From Dev

How to tell if a number is within a range in PowerShell

From Dev

How to extract a number within a range from a string

From Dev

How to tell if a number is within a range in PowerShell

From Dev

How to generate a random number within a range in D

From Dev

In postgres, how do I find records within a range, and then a single record outside the range on both sides?

From Dev

Postgresql: How do I ensure that indexes are in memory

From Dev

Postgresql: How do I ensure that indexes are in memory

From Dev

How can I count the number of specific files within a date range on the command line using bash?

From Dev

I want to add a range to the number of choices i have. How do I do that?

From Dev

How to check if a number is within the range of the same number including float type

From Dev

How do I use variable within consul-template range construct?

From Dev

How do I use variable within consul-template range construct?

From Dev

How do i deterministically generate n unique numbers within a range from a GUID?

From Java

How do I check if a given hour and minute (int's) falls within or is equal to any range within a given set of ranges of times?

Related Related

  1. 1

    How do I use the RandomRangedNumberCustomization in Autofixture to ensure parameters are in a certain range?

  2. 2

    Using Python how do I generate a random number within a range for each row in Pandas dataframe?

  3. 3

    Using Python how do I generate a random number within a range for each row in Pandas dataframe?

  4. 4

    How do I generate a random range of numbers within another range?

  5. 5

    How can I generate a random number within a range in Rust?

  6. 6

    How can I count an SQL field for the number of results within a range?

  7. 7

    How to I validated an input for being an Int and within a number range?

  8. 8

    How do I find how many days are within a date range that are within another date range in PHP?

  9. 9

    How do I get a random number with a negative number in range?

  10. 10

    How do I get a cell's position within a range?

  11. 11

    How do I assign a value in R if within a certain range of time?

  12. 12

    How do I retrieve data that is within a specific date range?

  13. 13

    How do I calculate an average of a range from a series within in a dataframe?

  14. 14

    How do I delete messages specified by a number range?

  15. 15

    How Do I GET Number Range with a SPACE From A PHP String?

  16. 16

    How to tell if a number is within a range in PowerShell

  17. 17

    How to extract a number within a range from a string

  18. 18

    How to tell if a number is within a range in PowerShell

  19. 19

    How to generate a random number within a range in D

  20. 20

    In postgres, how do I find records within a range, and then a single record outside the range on both sides?

  21. 21

    Postgresql: How do I ensure that indexes are in memory

  22. 22

    Postgresql: How do I ensure that indexes are in memory

  23. 23

    How can I count the number of specific files within a date range on the command line using bash?

  24. 24

    I want to add a range to the number of choices i have. How do I do that?

  25. 25

    How to check if a number is within the range of the same number including float type

  26. 26

    How do I use variable within consul-template range construct?

  27. 27

    How do I use variable within consul-template range construct?

  28. 28

    How do i deterministically generate n unique numbers within a range from a GUID?

  29. 29

    How do I check if a given hour and minute (int's) falls within or is equal to any range within a given set of ranges of times?

HotTag

Archive