Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2004-08-06
15:03 • Closed ticket [1003145fff]: expr int operation giving different results for same... artifact: bb744a8fe3 user: dkf
2004-08-04
19:42 • Ticket [1003145fff]: 4 changes artifact: 261800875b user: s_a_white
17:25 • Ticket [1003145fff]: 5 changes artifact: ec46625b77 user: dkf
17:25 • Ticket [1003145fff]: 1 change artifact: 68ab88ff11 user: dkf
09:55 • New ticket [1003145fff]. artifact: 762a0e6e92 user: s_a_white

expr int operation giving different results for same values

Closed / Invalid1003145fffffffffffffffffffffffffffffffffCreated 22 years agoUpdated 22 years ago
Descriptionplain2004-08-04 09:55:09
Perhaps related to bug 866469?  Preferred after int
conversion a consistant output on all operations,
preferrably giving the same results as C for a similar
double to int conversion.

Test:

proc test { val } {
    set scaling 100
    puts "\t$val"
    set out [expr { $val * $scaling }]
    puts "\t$out"
    set out [expr { int($out) }]
    puts "\t$out"
    return $out
}

puts "test1:"
set a "999996.0"
puts "\t$a"
puts "\t[expr { int($a) }]"
puts "test2:"
set a 9999.96
puts "\t$a"
set a [expr { $a * 100.0 }]
puts "\t$a"
puts "\t[expr { int($a) }]"
puts "test3:"
set a 9999.96
puts "\t$a"
set a [expr { $a * 100 }]
puts "\t$a"
puts "\t[expr { int($a) }]"
puts "test4:"
puts "\t[test 9999.96]"


Results:

test1:
        999996.0
        999996
test2:
        9999.96
        999996.0
        999995
test3:
        9999.96
        999996.0
        999995
test4:
        9999.96
        999996.0
        999995
        999995

C result is always 999996 for the provided double value.
dkf Ticket [1003145fff]: 1 change [History diff]2004-08-04 17:25:16
dkfplain2004-08-04 17:25:17
Logged In: YES 
user_id=79902

Floating point is *very* mysterious.
Demystify a bit by setting tcl_precision to 17
C compilers do many aggressive optimizations often, and
floating point math on Intel is actually usually done using
more bits of precision than the IEEE spec calls for (and Tcl
can't really take advantage of that sort of thing.)
s_a_whiteplain2004-08-04 19:42:18
Logged In: YES 
user_id=59929

Well as said it wasn't really the actual maths results that
were the problem, just the fact that you could have two
different results from the same string/operations.  I hunted
around a bit more and found TIP 132 (I think it was)
outlining the problem in more detail.

I can understand the issue now in that internally your
working at a higher resolution than tcl_precision and that
the result of puts is based on a rounded version using
tcl_percision.

I have no idea if all the suggestions recommened in that tip
would fix things in a way that produces sensible output.  To
me (if possible) it would be quite acceptable to round
results at end of maths operations to the required precision
that was set by tcl_precision.  That way results would be
consistant with the output of puts and users can still
increase tcl_precision as required for better results.
dkfplain2004-08-06 15:03:41
Logged In: YES 
user_id=79902

Further comment:
I don't know what C compiler you're using, but I don't get
999996 throughout for C. (See below for details.) This
indicates to me that your expectations of floating point
math are unrealistic and that this is not actually a bug. 
IOW, everything's happening just right, but you don't think
the world should be arranged that way.  Tough luck.

------PROGRAM------
#include <stdio.h>
int main() {
    double d = 999996.0;
    printf("%f\t%d\n", d, (int)d);
    d = 9999.96;
    printf("%f\t%d\n", d, (int)d);
    d *= 100.0;
    printf("%f\t%d\n", d, (int)d);
    d = 9999.96;
    printf("%f\t%d\n", d, (int)d);
    d *= 100;
    printf("%f\t%d\n", d, (int)d);
    return 0;
}
------OUTPUT------
999996.000000   999996
9999.960000     9999
999996.000000   999995
9999.960000     9999
999996.000000   999995
------MISC.INFO------
Compiled with: gcc -O3 -Wall
Compiled and run on: AMD Athlon running RH9 with gcc 3.2.2
Log in (anonymous is fine) to add a comment.