summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin B Kenny <kennykb@acm.org>2005-08-29 16:18:59 (GMT)
committerKevin B Kenny <kennykb@acm.org>2005-08-29 16:18:59 (GMT)
commitef4b48b4a556b3bc1ecac4ba3f1414cf3b7d4bd1 (patch)
tree88a7ae9ad11d90393c4e10c805865f8b091b8818
parent6b46682cfeecee8430f13996041c1347b9a20658 (diff)
downloadtcl-ef4b48b4a556b3bc1ecac4ba3f1414cf3b7d4bd1.zip
tcl-ef4b48b4a556b3bc1ecac4ba3f1414cf3b7d4bd1.tar.gz
tcl-ef4b48b4a556b3bc1ecac4ba3f1414cf3b7d4bd1.tar.bz2
Bug 1275043
-rw-r--r--ChangeLog8
-rw-r--r--generic/tclBasic.c44
-rw-r--r--tests/expr.test76
3 files changed, 111 insertions, 17 deletions
diff --git a/ChangeLog b/ChangeLog
index de410c1..3137dc5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2005-08-29 Kevin Kenny <kennykb@acm.org>
+
+ * generic/tclBasic.c (ExprMathFunc): Restored "round away from
+ * tests/expr.test (expr-39.*): zero" behaviour to the
+ "round" function. Added
+ test cases for the behavior, including the awkward case of a
+ number whose fractional part is 1/2-1/2ulp. [Bug 1275043]
+
2005-08-26 Andreas Kupries <andreask@activestate.com>
* generic/tclIO.c: Moved Tcl_{Cut,Splice}Channel to
diff --git a/generic/tclBasic.c b/generic/tclBasic.c
index df4abd8..a259130 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.167 2005/08/24 17:56:23 andreas_kupries Exp $
+ * RCS: @(#) $Id: tclBasic.c,v 1.168 2005/08/29 16:18:59 kennykb Exp $
*/
#include "tclInt.h"
@@ -5425,7 +5425,7 @@ ExprRoundFunc(clientData, interp, objc, objv)
Tcl_Obj *CONST *objv; /* Parameter vector */
{
Tcl_Obj *valuePtr, *resPtr;
- double d, a, f;
+ double d, i, f;
/* Check the argument count. */
@@ -5448,31 +5448,43 @@ ExprRoundFunc(clientData, interp, objc, objv)
GET_DOUBLE_VALUE(d, valuePtr, valuePtr->typePtr);
/*
- * Round the number to the nearest integer. I'd like to use rint()
- * or nearbyint(), but they are far from universal.
+ * Round the number to the nearest integer. I'd like to use round(),
+ * but it's C99 (or BSD), and not yet universal.
*/
- a = fabs(d);
- if (a < Tcl_WideAsDouble(LLONG_MAX) + 0.5) {
- d = valuePtr->internalRep.doubleValue;
- f = floor(d);
- d -= f;
- if (d > 0.5 || (d == 0.5 && fmod(f, 2.0) != 0.0)) {
- f = f + 1.0;
+ d = valuePtr->internalRep.doubleValue;
+ f = modf(d, &i);
+ if (d < 0.0) {
+ if (f <= -0.5) {
+ i += -1.0;
}
- if (f >= (double) LONG_MIN && f <= (double) LONG_MAX) {
- TclNewLongObj(resPtr, (long) f);
+ if (i <= Tcl_WideAsDouble(LLONG_MIN)) {
+ goto tooLarge;
+ } else if (d <= (double) LONG_MIN) {
+ resPtr = Tcl_NewWideIntObj(Tcl_DoubleAsWide(i));
} else {
- TclNewWideIntObj(resPtr, Tcl_DoubleAsWide(f));
+ resPtr = Tcl_NewLongObj((long) i);
+ }
+ } else {
+ if (f >= 0.5) {
+ i += 1.0;
+ }
+ if (i >= Tcl_WideAsDouble(LLONG_MAX)) {
+ goto tooLarge;
+ } else if (i >= (double) LONG_MAX) {
+ resPtr = Tcl_NewWideIntObj(Tcl_DoubleAsWide(i));
+ } else {
+ resPtr = Tcl_NewLongObj((long) i);
}
- Tcl_SetObjResult(interp, resPtr);
- return TCL_OK;
}
+ Tcl_SetObjResult(interp, resPtr);
+ return TCL_OK;
/*
* Error return: result cannot be represented as an integer.
*/
+ tooLarge:
Tcl_SetObjResult(interp, Tcl_NewStringObj(
"integer value too large to represent", -1));
Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW",
diff --git a/tests/expr.test b/tests/expr.test
index 6089368..7b7c13b 100644
--- a/tests/expr.test
+++ b/tests/expr.test
@@ -10,7 +10,7 @@
# 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.40 2005/08/24 23:36:56 dkf Exp $
+# RCS: @(#) $Id: expr.test,v 1.41 2005/08/29 16:18:59 kennykb Exp $
if {[lsearch [namespace children] ::tcltest] == -1} {
package require tcltest 2.1
@@ -6239,6 +6239,80 @@ test expr-38.1 {abs of smallest 32-bit integer [Bug 1241572]} {wideIs64bit} {
expr {abs(-2147483648)}
} 2147483648
+set tcl_precision 17
+
+test expr-39.1 {round() rounds to +-infinity} {
+ expr round(0.5)
+} 1
+test expr-39.2 {round() rounds to +-infinity} {
+ expr round(1.5)
+} 2
+test expr-39.3 {round() rounds to +-infinity} {
+ expr round(-0.5)
+} -1
+test expr-39.4 {round() rounds to +-infinity} {
+ expr round(-1.5)
+} -2
+test expr-39.5 {round() overflow} {
+ list [catch {expr round(9.2233720368547758e+018)} result] $result
+} {1 {integer value too large to represent}}
+test expr-39.6 {round() overflow} {
+ list [catch {expr round(-9.2233720368547758e+018)} result] $result
+} {1 {integer value too large to represent}}
+test expr-39.7 {round() bad value} {
+ set x trash
+ list [catch {expr {round($x)}} result] $result
+} {1 {argument to math function didn't have numeric value}}
+test expr-39.8 {round() already an integer} {
+ set x 123456789012
+ incr x
+ expr round($x)
+} 123456789013
+test expr-39.9 {round() boundary case - 1/2 - 1 ulp} {
+ set x 0.25
+ set bit 0.125
+ while 1 {
+ set newx [expr { $x + $bit }]
+ if { $newx == $x || $newx == 0.5 } break
+ set x $newx
+ set bit [expr { $bit / 2.0 }]
+ }
+ expr round($x)
+} 0
+test expr-39.10 {round() boundary case - 1/2 + 1 ulp} {
+ set x 0.75
+ set bit 0.125
+ while 1 {
+ set newx [expr { $x - $bit }]
+ if { $newx == $x || $newx == 0.5 } break
+ set x $newx
+ set bit [expr { $bit / 2.0 }]
+ }
+ expr round($x)
+} 1
+test expr-39.11 {round() boundary case - -1/2 - 1 ulp} {
+ set x -0.75
+ set bit 0.125
+ while 1 {
+ set newx [expr { $x + $bit }]
+ if { $newx == $x || $newx == -0.5 } break
+ set x $newx
+ set bit [expr { $bit / 2.0 }]
+ }
+ expr round($x)
+} -1
+test expr-39.10 {round() boundary case - -1/2 + 1 ulp} {
+ set x -0.25
+ set bit 0.125
+ while 1 {
+ set newx [expr { $x - $bit }]
+ if { $newx == $x || $newx == -0.5 } break
+ set x $newx
+ set bit [expr { $bit / 2.0 }]
+ }
+ expr round($x)
+} 0
+
# cleanup
if {[info exists a]} {
unset a