Tcl Source Code

Artifact Content
Login
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2019 Conference, Houston/TX, US, Nov 4-8
Send your abstracts to [email protected]
or submit via the online form by Sep 9.

Artifact 77255a8d9037c3c7f3c2bc0978efb458a76b5ece:

Attachment "1165062.patch" to ticket [1165062fff] added by dgp 2005-05-21 00:05:35.
Index: doc/mathfunc.n
===================================================================
RCS file: /cvsroot/tcl/tcl/doc/mathfunc.n,v
retrieving revision 1.3
diff -u -r1.3 mathfunc.n
--- doc/mathfunc.n	12 May 2005 21:21:14 -0000	1.3
+++ doc/mathfunc.n	20 May 2005 16:48:34 -0000
@@ -27,6 +27,8 @@
 .br
 \fB::tcl::mathfunc::atan2\fR \fIy\fR \fIx\fR
 .br
+\fB::tcl::mathfunc::bool\fR \fIarg\fR
+.br
 \fB::tcl::mathfunc::ceil\fR \fIarg\fR
 .br
 \fB::tcl::mathfunc::cos\fR \fIarg\fR
@@ -85,13 +87,13 @@
 of which work solely with floating-point numbers unless otherwise noted:
 .DS
 .ta 3c 6c 9c
-\fBabs\fR	\fBcosh\fR	\fBlog\fR	\fBsqrt\fR
-\fBacos\fR	\fBdouble\fR	\fBlog10\fR	\fBsrand\fR
-\fBasin\fR	\fBexp\fR	\fBpow\fR	\fBtan\fR
-\fBatan\fR	\fBfloor\fR	\fBrand\fR	\fBtanh\fR
-\fBatan2\fR	\fBfmod\fR	\fBround\fR	\fBwide\fR
+\fBabs\fR      \fBcos\fR       \fBint\fR       \fBsinh\fR
+\fBacos\fR     \fBcosh\fR      \fBlog\fR       \fBsqrt\fR
+\fBasin\fR     \fBdouble\fR    \fBlog10\fR     \fBsrand\fR
+\fBatan\fR     \fBexp\fR       \fBpow\fR       \fBtan\fR
+\fBatan2\fR    \fBfloor\fR     \fBrand\fR      \fBtanh\fR
+\fBbool\fR     \fBfmod\fR      \fBround\fR     \fBwide\fR
 \fBceil\fR	\fBhypot\fR	\fBsin\fR
-\fBcos\fR	\fBint\fR	\fBsinh\fR
 .DE
 .PP
 .TP
@@ -116,6 +118,13 @@
 radians.  \fIx\fR and \fIy\fR cannot both be 0.  If \fIx\fR is greater
 than \fI0\fR, this is equivalent to \fBatan(\fIy/x\fB)\fR.
 .TP
+\fBbool(\fIarg\fB)\fR
+Accepts any numerical value, or any string acceptable to
+\fBstring is boolean\fR, and returns the corresponding 
+boolean value \fB0\fR or \fB1\fR.  Non-zero numbers are true.
+Other numbers are false.  Non-numeric strings produce boolean value in
+agreement with \fBstring is true\fR and \fBstring is false\fR.
+.TP
 \fBceil(\fIarg\fB)\fR
 Returns the smallest integral floating-point value (i.e. with a zero
 fractional part) not less than \fIarg\fR.
@@ -218,4 +227,4 @@
 .br
 Copyright (c) 1994-2000 Sun Microsystems Incorporated.
 .br
-Copyright (c) 2005 by Kevin B. Kenny <[email protected]>. All rights reserved.
\ No newline at end of file
+Copyright (c) 2005 by Kevin B. Kenny <[email protected]>. All rights reserved.
Index: generic/tclBasic.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclBasic.c,v
retrieving revision 1.155
diff -u -r1.155 tclBasic.c
--- generic/tclBasic.c	19 May 2005 15:18:02 -0000	1.155
+++ generic/tclBasic.c	20 May 2005 16:48:35 -0000
@@ -52,6 +52,8 @@
 		    Tcl_Interp *interp, int argc, Tcl_Obj *CONST *objv));
 static int	ExprBinaryFunc _ANSI_ARGS_((ClientData clientData,
 		    Tcl_Interp *interp, int argc, Tcl_Obj *CONST *objv));
+static int	ExprBoolFunc _ANSI_ARGS_((ClientData clientData,
+		    Tcl_Interp *interp, int argc, Tcl_Obj *CONST *objv));
 static int	ExprDoubleFunc _ANSI_ARGS_((ClientData clientData,
 		    Tcl_Interp *interp, int argc, Tcl_Obj *CONST *objv));
 static int	ExprIntFunc _ANSI_ARGS_((ClientData clientData,
@@ -249,6 +251,7 @@
     { "::tcl::mathfunc::asin",	ExprUnaryFunc,	(ClientData) asin 	},
     { "::tcl::mathfunc::atan",	ExprUnaryFunc,	(ClientData) atan 	},
     { "::tcl::mathfunc::atan2",	ExprBinaryFunc,	(ClientData) atan2 	},
+    { "::tcl::mathfunc::bool",	ExprBoolFunc,	NULL			},
     { "::tcl::mathfunc::ceil",	ExprUnaryFunc,	(ClientData) ceil 	},
     { "::tcl::mathfunc::cos",	ExprUnaryFunc,	(ClientData) cos 	},
     { "::tcl::mathfunc::cosh",	ExprUnaryFunc,	(ClientData) cosh	},
@@ -5127,6 +5130,27 @@
 }
 
 static int
+ExprBoolFunc(clientData, interp, objc, objv)
+    ClientData clientData;	/* Ignored. */
+    Tcl_Interp *interp;		/* The interpreter in which to execute the
+				 * function. */
+    int objc;			/* Actual parameter count */
+    Tcl_Obj *CONST *objv;	/* Actual parameter vector */
+{
+    int value;
+
+    if (objc != 2) {
+	MathFuncWrongNumArgs(interp, 2, objc, objv);
+	return TCL_ERROR;
+    }
+    if (Tcl_GetBooleanFromObj(interp, objv[1], &value) != TCL_OK) {
+	return TCL_ERROR;
+    }
+    Tcl_SetObjResult(interp, Tcl_NewBooleanObj(value));
+    return TCL_OK;
+}
+
+static int
 ExprDoubleFunc(clientData, interp, objc, objv)
     ClientData clientData;	/* Ignored. */
     Tcl_Interp *interp;		/* The interpreter in which to execute the
Index: tests/expr.test
===================================================================
RCS file: /cvsroot/tcl/tcl/tests/expr.test,v
retrieving revision 1.32
diff -u -r1.32 expr.test
--- tests/expr.test	10 May 2005 18:35:19 -0000	1.32
+++ tests/expr.test	20 May 2005 16:48:39 -0000
@@ -13,7 +13,7 @@
 # RCS: @(#) $Id: expr.test,v 1.32 2005/05/10 18:35:19 kennykb Exp $
 
 if {[lsearch [namespace children] ::tcltest] == -1} {
-    package require tcltest
+    package require tcltest 2.1
     namespace import -force ::tcltest::*
 }
 
@@ -5181,6 +5181,69 @@
     list [scan -1.7976931348623159e+308 %f v] $v
 } {1 -Inf}
 
+# bool() tests (TIP #182)
+set i 0
+foreach s {yes true on} {
+    test expr-31.$i.0 {boolean conversion} {expr bool($s)} 1
+    test expr-31.$i.1 {boolean conversion} {expr bool(!$s)} 0
+    test expr-31.$i.2 {boolean conversion} {expr bool("$s")} 1
+    test expr-31.$i.3 {boolean conversion} {expr bool(!"$s")} 0
+    set j 1
+    while {$j < [string length $s]-1} {
+	test expr-31.$i.4.$j {boolean conversion} {
+	    expr bool([string range $s 0 $j])
+	} 1
+	test expr-31.$i.5.$j {boolean conversion} {
+	    expr bool("[string range $s 0 $j]")
+	} 1
+	incr j
+    }
+    incr i
+}
+test expr-31.0.4.0 {boolean conversion} {expr bool(y)} 1
+test expr-31.0.5.0 {boolean conversion} {expr bool("y")} 1
+test expr-31.1.4.0 {boolean conversion} {expr bool(t)} 1
+test expr-31.1.5.0 {boolean conversion} {expr bool("t")} 1
+test expr-31.2.4.0 {boolean conversion} -body {
+    expr bool(o)
+} -returnCodes error -match glob -result *
+test expr-31.2.5.0 {boolean conversion} -body {
+    expr bool("o")
+} -returnCodes error -match glob -result *
+foreach s {no false off} {
+    test expr-31.$i.0 {boolean conversion} {expr bool($s)} 0
+    test expr-31.$i.1 {boolean conversion} {expr bool(!$s)} 1
+    test expr-31.$i.2 {boolean conversion} {expr bool("$s")} 0
+    test expr-31.$i.3 {boolean conversion} {expr bool(!"$s")} 1
+    set j 1
+    while {$j < [string length $s]-1} {
+	test expr-31.$i.4.$j {boolean conversion} {
+	    expr bool([string range $s 0 $j])
+	} 0
+	test expr-31.$i.5.$j {boolean conversion} {
+	    expr bool("[string range $s 0 $j]")
+	} 0
+	incr j
+    }
+    incr i
+}
+test expr-31.3.4.0 {boolean conversion} {expr bool(n)} 0
+test expr-31.3.5.0 {boolean conversion} {expr bool("n")} 0
+test expr-31.4.4.0 {boolean conversion} {expr bool(f)} 0
+test expr-31.4.5.0 {boolean conversion} {expr bool("f")} 0
+test expr-31.6  {boolean conversion} {expr bool(-1 + 1)} 0
+test expr-31.7  {boolean conversion} {expr bool(0 + 1)} 1
+test expr-31.8  {boolean conversion} {expr bool(0.0)} 0
+test expr-31.9  {boolean conversion} {expr bool(0x0)} 0
+test expr-31.10 {boolean conversion} {expr bool(wide(0))} 0
+test expr-31.11 {boolean conversion} {expr bool(5.0)} 1
+test expr-31.12 {boolean conversion} {expr bool(5)} 1
+test expr-31.13 {boolean conversion} {expr bool(0x5)} 1
+test expr-31.14 {boolean conversion} {expr bool(wide(5))} 1
+test expr-31.15 {boolean conversion} -body {
+    expr bool("fred")
+} -returnCodes error -match glob -result *
+
 # cleanup
 if {[info exists a]} {
     unset a
Index: tests/info.test
===================================================================
RCS file: /cvsroot/tcl/tcl/tests/info.test,v
retrieving revision 1.29
diff -u -r1.29 info.test
--- tests/info.test	24 Nov 2004 19:28:42 -0000	1.29
+++ tests/info.test	20 May 2005 16:48:39 -0000
@@ -613,9 +613,9 @@
 
 # Check whether the extra testing functions are defined...
 if {([catch {expr T1()} msg] == 1) && ($msg == {unknown math function "T1"})} {
-    set functions {abs acos asin atan atan2 ceil cos cosh double exp floor fmod hypot int log log10 pow rand round sin sinh sqrt srand tan tanh wide}
+    set functions {abs acos asin atan atan2 bool ceil cos cosh double exp floor fmod hypot int log log10 pow rand round sin sinh sqrt srand tan tanh wide}
 } else {
-    set functions {T1 T2 T3 abs acos asin atan atan2 ceil cos cosh double exp floor fmod hypot int log log10 pow rand round sin sinh sqrt srand tan tanh wide}
+    set functions {T1 T2 T3 abs acos asin atan atan2 bool ceil cos cosh double exp floor fmod hypot int log log10 pow rand round sin sinh sqrt srand tan tanh wide}
 }
 test info-20.1 {info functions option} {info functions sin} sin
 test info-20.2 {info functions option} {lsort [info functions]} $functions