I want to create a new operator in Tcl, by operator I mean a proc that could be used in the following way:
$a**2 instead of pow($a,2)
Or in my case I want an operator that does the same as <= and >= but for floating point numbers, meaning:
proc <f= {a b {epsilon 1e-15} {
if {$a < $b} {return 1}
return [expr abs($a - $b) < $epsilon]
}
But I want to use it as:
if {$a <f= $b} {...
instead of”
if {[<f= $a $b]} {...
and the same for >f=
You can’t implement exactly this, but there is one option which might help.
Since Tcl 8.5, it’s possible to define arbitrary functions for
exprmini-language by creating commands in thetcl::mathfuncnamespace, observe:And with a bit of import/export mumbo-jumbo you can happily make this function be available as a command in your root namespace:
(Alternatively, you could create your command in any convenient namespace and then import it into the
tcl::mathfuncnamespace.)Not exactly what you want, but comes close enough in my opinion.
Note also that since 8.5 Tcl has a special namespace
tcl::mathopwhich exposes allexproperators as commands, but you can’t really modify this namespace in any way (but can use commands from it).