I identified a few minor problems with FormatCmd and doubles that
are -0.0, -Infinity or Infinity.
Below are two test files Zero.java and NaN.java, followed by a small
patch.
cxh@maury 235% cat Zero.java
public class Zero {
public static double zero = -0.0;
public static double myarray[] = { -0.0, 0.0, +0.0};
public String printmyarray() {
return "zero=" + zero + " myarray=" + myarray[0] + " " +
myarray[1] + " " +
myarray[2];
}
public static void main(String argv[]) {
Zero zero = new Zero();
System.out.println(zero.printmyarray());
}
}
cxh@maury 236% javac Zero.java
cxh@maury 237% java Zero
zero=-0.0 myarray=-0.0 0.0 0.0
cxh@maury 238% java tcl.lang.Shell
% set z [java::new Zero]
java0x1
% $z printmyarray
zero=-0.0 myarray=-0.0 0.0 0.0
#### Bug! zero should be -0.0
% java::field $z zero
0.0
% set myarray [java::field $z myarray]
java0x2
#### Bug! the array should be -0.0 0.0 0.0
% $myarray getrange
0.0 0.0 0.0
% exit
#### Here's the fix
cxh@maury 239% make
making jclass in tcl/lang/library
make[1]: Entering directory `/export/maury/maury2/cxh/ptII/tcl/lang/library'
make[1]: Leaving directory `/export/maury/maury2/cxh/ptII/tcl/lang/library'
making jclass in tcl/lang/reflect
make[1]: Entering directory `/export/maury/maury2/cxh/ptII/tcl/lang/reflect'
make[1]: Leaving directory `/export/maury/maury2/cxh/ptII/tcl/lang/reflect'
rm -f `basename FormatCmd.java .java`.class
CLASSPATH="../.." /opt/jdk1.2latest/bin/javac -g FormatCmd.java
cxh@maury 240% java tcl.lang.Shell
% set z [java::new Zero]
java0x1
% $z printmyarray
zero=-0.0 myarray=-0.0 0.0 0.0
% java::field $z zero
-0.0
% set myarray [java::field $z myarray]
java0x2
% $myarray getrange
-0.0 0.0 0.0
% exit
Here's a little test for NEGATIVE_INFINITY
cxh@maury 241% cat NaN.java
public class NaN {
public static double myarray[] = { Double.NEGATIVE_INFINITY,
Double.NaN,
Double.POSITIVE_INFINITY
};
public String printmyarray() {
return " myarray=" + myarray[0] + " " +
myarray[1] + " " +
myarray[2];
}
public static void main(String argv[]) {
NaN nan = new NaN();
System.out.println(nan.printmyarray());
}
}
cxh@maury 242% javac NaN.java
cxh@maury 243% java NaN
myarray=-Infinity NaN Infinity
cxh@maury 244% java tcl.lang.Shell
#### Bug! This should be -Infinity NaN Infinity
% set n [java::new NaN]; set myarray [java::field $n myarray]; $myarray getrange
NaN NaN NaN
% exit
cxh@maury 245% make
making jclass in tcl/lang/library
make[1]: Entering directory `/export/maury/maury2/cxh/ptII/tcl/lang/library'
make[1]: Leaving directory `/export/maury/maury2/cxh/ptII/tcl/lang/library'
making jclass in tcl/lang/reflect
make[1]: Entering directory `/export/maury/maury2/cxh/ptII/tcl/lang/reflect'
make[1]: Leaving directory `/export/maury/maury2/cxh/ptII/tcl/lang/reflect'
rm -f `basename FormatCmd.java .java`.class
CLASSPATH="../.." /opt/jdk1.2latest/bin/javac -g FormatCmd.java
cxh@maury 246% java tcl.lang.Shell
% set n [java::new NaN]; set myarray [java::field $n myarray]; $myarray getrange
-Infinity NaN Infinity
% exit
Here's the change to FormatCmd.java:
cxh@maury 247% diff -c ~ptII/tcl/lang/FormatCmd.java .
*** /users/ptII/tcl/lang/FormatCmd.java Fri Mar 19 18:09:12 1999
--- ./FormatCmd.java Mon May 24 00:56:52 1999
***************
*** 7,13 ****
* redistribution of this file, and for a DISCLAIMER OF ALL
* WARRANTIES.
*
! * RCS: @(#) $Id: FormatCmd.java,v 1.2 1999/03/20 01:59:03 cxh Exp $
*
*/
--- 7,13 ----
* redistribution of this file, and for a DISCLAIMER OF ALL
* WARRANTIES.
*
! * RCS: @(#) $Id: FormatCmd.java,v 1.3 1999/05/24 06:36:43 cxh Exp $
*
*/
***************
*** 639,644 ****
--- 639,654 ----
boolean flag_rtz = true; /* Flag for "remove trailing zeros" */
boolean flag_dp = true; /* Flag for remove "decimal point" */
+ if ((new Double(dblValue)).isNaN()) {
+ return "NaN";
+ }
+ if (dblValue == Double.NEGATIVE_INFINITY) {
+ return "-Infinity";
+ }
+ if (dblValue == Double.POSITIVE_INFINITY) {
+ return "Infinity";
+ }
+
/*
* If precision < 0 (eg -1) then the precision defaults
*/
***************
*** 651,656 ****
--- 661,685 ----
dblValue = -dblValue;
prefix = '-';
prefixSize = 1;
+ } else if (dblValue == 0.0 &&
+ (new Double(dblValue)).equals((new Double(-0.0)))) {
+ // Handle -0.0
+ //
+ // 15.19.1 "Numerical Comparison Operators <, <=, >, and >= "
+ // of the Java Language Spec says:
+ // "Positive zero and negative zero are considered
+ // equal. Therefore, -0.0<0.0 is false, for example, but
+ // -0.0<=0.0 is true."
+ //
+ // The Double.equal man page says:
+ // "If d1 represents +0.0 while d2 represents -0.0, or
+ // vice versa, the equal test has the value false, even
+ // though +0.0==-0.0 has the value true. This allows
+ // hashtables to operate properly.
+
+ dblValue = -dblValue;
+ prefix = '-';
+ prefixSize = 1;
} else if ((flags & SHOW_SIGN) != 0) {
prefix = '+';
prefixSize = 1;
***************
*** 683,691 ****
*/
exp = 0;
- if ((new Double(dblValue)).isNaN()) {
- return "NaN";
- }
if (dblValue>0.0) {
int k = 0;
while ((dblValue >= 1e8) && (k++ < 100)) {
--- 712,717 ----
cxh@maury 248%
Note that the reason that we move the test for NaN was to keep the NaN
and Infinity tests together. We had to move the -Infinity test
earlier in the method because the code negates the value at one point,
so the test for -Infinity would never be true because the value was
always positive.
-Christopher
----------------------------------------------------------------
The TclJava mailing list is sponsored by WebNet Technologies.
To subscribe: send mail to [EMAIL PROTECTED]
with the word SUBSCRIBE as the subject.
To unsubscribe: send mail to [EMAIL PROTECTED]
with the word UNSUBSCRIBE as the subject.
To send to the list, send email to '[EMAIL PROTECTED]'.
A list archive is at: http://www.findmail.com/listsaver/tcldallas/