Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tcl 8.5 floating point operation

Status
Not open for further replies.

rahulvramteke

Programmer
Jun 18, 2012
1
US
Hi I am using tcl 8.5.
When I do division 1.0/1e-9 i get 999999999.9999999 instead of 1e9.
The same operation on tcl 8.4 works fine.
How to get around this problem to get the correct value/


for {set x 0} {$x<20} {incr x} { puts "$x [expr 1/1e-$x]"}
-----------
OUTPUT
-----------
0 1.0
1 10.0
2 100.0
3 1000.0
4 10000.0
5 99999.99999999999
6 1000000.0
7 10000000.0
8 100000000.0
9 999999999.9999999
10 10000000000.0
11 100000000000.0
12 1000000000000.0
13 10000000000000.0
14 100000000000000.0
15 999999999999999.9
16 10000000000000000.0
17 1e+17
18 9.999999999999999e+17
19 1e+19
 
Use format, for example:
Code:
for {set x 0} {$x<20} {incr x} { 
  puts [format "%2d %g" $x [expr 1/1e-$x]] 
}
outputs
Code:
00 1
01 10
02 100
03 1000
04 10000
05 100000
06 1e+006
07 1e+007
08 1e+008
09 1e+009
10 1e+010
11 1e+011
12 1e+012
13 1e+013
14 1e+014
15 1e+015
16 1e+016
17 1e+017
18 1e+018
19 1e+019
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top