TIP 255: Add 'min' and 'max' [expr] Functions

Login
Author:		Jeff Hobbs <[email protected]>
State:		Final
Type:		Project
Tcl-Version:	8.5
Vote:		Done
Created:	21-Jul-2005
Post-History:	
Tcl-Ticket:	1309020

Abstract

This TIP proposes enhancing the Tcl expr command with min and max functions.

Rationale

Many programs need to find the minimum or maximum of a set of numbers, so having functions to do this will make many script programmers' lives easier. This will be adopting yet another feature from TclX, but with the minor enhancement of being able to handle arbitrary numbers of arguments.

Specification

The min and max functions shall be created using Tcl commands (in a namespace such all namespaces will pick them up by default) in the new 8.5 expr function style (see [232]). They will take one or more numeric arguments (of any valid numeric type) and return the minimum or maximum value of those numbers (depending on which function was invoked).

Examples

 expr {min(0,3,1)}
 => 0
 expr {max(4.2, 0xF, wide(1))}
 => 15.0
 expr {min(4.2)}
 => 4.2
 expr {min()}
 => too few arguments for math function

Reference Implementation

[To be uploaded to SourceForge and URL added to this TIP.]

Copyright

This document has been placed in the public domain.