rfc:clamp

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
rfc:clamp [2021/06/23 18:29] – created thinkverserfc:clamp [2023/06/12 13:59] (current) – withdrawn thinkverse
Line 3: Line 3:
   * Date: 2021-06-23   * Date: 2021-06-23
   * Author: Kim Hallberg, hallbergkim@gmail.com   * Author: Kim Hallberg, hallbergkim@gmail.com
-  * Status: Under Discussion +  * Status: Withdrawn 
-  * Proposed Version: PHP 8.1+  * Proposed Version: PHP 8.2
   * First Published at: http://wiki.php.net/rfc/clamp   * First Published at: http://wiki.php.net/rfc/clamp
  
 ===== Introduction ===== ===== Introduction =====
-''clamp'' checks if a ''int|float'' value is within a certain bound. If the value is inbound it returns the value, if the value is not inbound it returns the nearest bound.+''clamp'' checks if a ''int|float'' value is within a certain bound. If the value is in range it returns the value, if the value is not in range it returns the nearest bound.
  
 Current userland implementations are handled in several ways, some of which use [[https://www.php.net/manual/en/function.min.php|min]] and [[https://www.php.net/manual/en/function.max.php|max]] to check the bound, which is costly and slow when called often. Because userland implementations are for the most part not cost-effective when called multiple times, a language implementation is desired. Current userland implementations are handled in several ways, some of which use [[https://www.php.net/manual/en/function.min.php|min]] and [[https://www.php.net/manual/en/function.max.php|max]] to check the bound, which is costly and slow when called often. Because userland implementations are for the most part not cost-effective when called multiple times, a language implementation is desired.
Line 20: Line 20:
 </code> </code>
  
-''clamp'' takes three arguments, a ''$num'', ''$min'' and ''$max'', checks if ''$num'' is within the bounds of ''$min'' and ''$max'', if inbound, returns the value of ''$num'', otherwise, returns the nearest bound value, i.e. if ''$num > $max'' return ''$max'', if ''$num < $min'' return ''$min''.+''clamp'' takes three arguments, a ''$num'', ''$min'' and ''$max'', checks if ''$num'' is within the bounds of ''$min'' and ''$max'', if in range, returns the value of ''$num'', otherwise, returns the nearest bound value, i.e. if ''$num > $max'' return ''$max'', if ''$num < $min'' return ''$min''.
  
 If ''$min'' value is greater than ''$max'' value, a ''ValueError'' will be thrown, since that constitute an invalid bound. If ''$min'' value is greater than ''$max'' value, a ''ValueError'' will be thrown, since that constitute an invalid bound.
Line 27: Line 27:
  
 <code php> <code php>
-clamp(num: 1, min: 0, max: 3); // 1   inbound+clamp(num: 1, min: 0, max: 3); // 1   in range
 clamp(num: 1, min: 2, max: 5); // 2   < bound clamp(num: 1, min: 2, max: 5); // 2   < bound
 clamp(num: 4, min: 1, max: 3); // 3   > bound clamp(num: 4, min: 1, max: 3); // 3   > bound
Line 33: Line 33:
 clamp(num: 0, min: 2, max: 1); // clamp(): Argument #2 ($min) cannot be greater than Argument #3 ($max) clamp(num: 0, min: 2, max: 1); // clamp(): Argument #2 ($min) cannot be greater than Argument #3 ($max)
 </code> </code>
 +
 +Handling NAN; passing NAN to either of the range values will throw above ''ValueError'' since passing NAN will invalidate the range.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 54: Line 56:
 ===== Open Issues ===== ===== Open Issues =====
  
-No open issues as of yet.+1) Handling of NAN values, as mentioned [[https://externals.io/message/115076#115085|#115085]] and [[https://externals.io/message/115076#115167|#115167]].
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
Line 72: Line 74:
   * Implementation PR: [[https://github.com/php/php-src/issues/7191]]   * Implementation PR: [[https://github.com/php/php-src/issues/7191]]
   * Discussion on the php.internals mailing list: [[https://externals.io/message/115050]]   * Discussion on the php.internals mailing list: [[https://externals.io/message/115050]]
 +  * Announcement thread: [[https://externals.io/message/115076]]
  
 ===== Rejected Features ===== ===== Rejected Features =====
  
 No rejected features currently. No rejected features currently.
rfc/clamp.1624472997.txt.gz · Last modified: 2021/06/23 18:29 by thinkverse