summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordgp <dgp@users.sourceforge.net>2005-05-25 16:13:16 (GMT)
committerdgp <dgp@users.sourceforge.net>2005-05-25 16:13:16 (GMT)
commit17da6ea9da2761c0e93da57cbbd779ec138e7a39 (patch)
tree5f85fe1c5270262b37c3b75c16728b3c5cc54428
parentf4ce59d358e3993aa45141bf6f1c37c1166ae2ec (diff)
downloadtcl-17da6ea9da2761c0e93da57cbbd779ec138e7a39.zip
tcl-17da6ea9da2761c0e93da57cbbd779ec138e7a39.tar.gz
tcl-17da6ea9da2761c0e93da57cbbd779ec138e7a39.tar.bz2
TIP#182 IMPLEMENTATION [Patch 1165062]
* doc/mathfunc.n: New built-in math function bool(). * generic/tclBasic.c: * tests/expr.test: * tests/info.test:
-rw-r--r--ChangeLog9
-rw-r--r--doc/mathfunc.n25
-rw-r--r--generic/tclBasic.c26
-rw-r--r--tests/expr.test67
-rw-r--r--tests/info.test6
5 files changed, 119 insertions, 14 deletions
diff --git a/ChangeLog b/ChangeLog
index a997e39..a46420e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2005-05-25 Don Porter <dgp@users.sourceforge.net>
+
+ TIP#182 IMPLEMENTATION [Patch 1165062]
+
+ * doc/mathfunc.n: New built-in math function bool().
+ * generic/tclBasic.c:
+ * tests/expr.test:
+ * tests/info.test:
+
2005-05-24 Don Porter <dgp@users.sourceforge.net>
* library/init.tcl: Updated [unknown] to be sure the [return]
diff --git a/doc/mathfunc.n b/doc/mathfunc.n
index f5b11b6..9f5d10a 100644
--- a/doc/mathfunc.n
+++ b/doc/mathfunc.n
@@ -6,7 +6,7 @@
'\" See the file "license.terms" for information on usage and redistribution
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
'\"
-'\" RCS: @(#) $Id: mathfunc.n,v 1.3 2005/05/12 21:21:14 kennykb Exp $
+'\" RCS: @(#) $Id: mathfunc.n,v 1.4 2005/05/25 16:13:17 dgp Exp $
'\"
.so man.macros
.TH mathfunc n 8.5 Tcl "Tcl Mathematical Functions"
@@ -27,6 +27,8 @@ package require \fBTcl 8.5\fR
.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 @@ Tcl supports the following mathematical functions in expressions, all
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 @@ Returns the arc tangent of \fIy\fR/\fIx\fR, in the range [\fI-pi\fR,\fIpi\fR]
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 @@ Copyright (c) 1993 The Regents of the University of California.
.br
Copyright (c) 1994-2000 Sun Microsystems Incorporated.
.br
-Copyright (c) 2005 by Kevin B. Kenny <kennykb@acm.org>. All rights reserved. \ No newline at end of file
+Copyright (c) 2005 by Kevin B. Kenny <kennykb@acm.org>. All rights reserved.
diff --git a/generic/tclBasic.c b/generic/tclBasic.c
index 7637f62..e526dc2 100644
--- a/generic/tclBasic.c
+++ b/generic/tclBasic.c
@@ -13,7 +13,7 @@
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
- * RCS: @(#) $Id: tclBasic.c,v 1.156 2005/05/22 01:30:44 chengyemao Exp $
+ * RCS: @(#) $Id: tclBasic.c,v 1.157 2005/05/25 16:13:17 dgp Exp $
*/
#include "tclInt.h"
@@ -52,6 +52,8 @@ static int ExprAbsFunc _ANSI_ARGS_((ClientData clientData,
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 @@ BuiltinFuncDef BuiltinFuncTable[] = {
{ "::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 @@ ExprAbsFunc(clientData, interp, objc, objv)
}
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
diff --git a/tests/expr.test b/tests/expr.test
index 5992421..067ad20 100644
--- a/tests/expr.test
+++ b/tests/expr.test
@@ -10,10 +10,10 @@
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
-# RCS: @(#) $Id: expr.test,v 1.32 2005/05/10 18:35:19 kennykb Exp $
+# RCS: @(#) $Id: expr.test,v 1.33 2005/05/25 16:13:17 dgp Exp $
if {[lsearch [namespace children] ::tcltest] == -1} {
- package require tcltest
+ package require tcltest 2.1
namespace import -force ::tcltest::*
}
@@ -5181,6 +5181,69 @@ test expr-30.4 {silent overflow on input conversion} {ieeeFloatingPoint} {
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
diff --git a/tests/info.test b/tests/info.test
index b3e777e..3441a3b 100644
--- a/tests/info.test
+++ b/tests/info.test
@@ -11,7 +11,7 @@
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
-# RCS: @(#) $Id: info.test,v 1.29 2004/11/24 19:28:42 dgp Exp $
+# RCS: @(#) $Id: info.test,v 1.30 2005/05/25 16:13:17 dgp Exp $
if {[lsearch [namespace children] ::tcltest] == -1} {
package require tcltest
@@ -613,9 +613,9 @@ test info-19.6 {info vars: Bug 1072654} -setup {
# 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