0

I have the following loop :

for {set i 1} {$i<=$nbrx} {incr i 1} {
    for {set j 1} {$j<=$nbry} {incr j 1} {

            node $numberer [expr ($i-1)*$dx] [expr ($j-1)*$dy]

        etc. etc.
    }
}

nbrx and nbry are very high valued variable (around 2000) I give the following value to dx and dy = 0.1 I end up, on the fourth cycle (like i = 1 and j = 4) with the evaluation of [expr ($j-1)*$dy] as being 0.30000000000000004
Then if the loops keep going in, at some point (always the same) I will have this kind of value with .000000000000004 or 0.000000000001 whilst most of the other value will be singled value (like I would expect them to be).

Any1 got a idea of why this is happening? Thanks for the help!

John
  • 61
  • 5
  • Now I tried to use 3*0.1 and it gives me 0.3000000000000004 ... ?? – John Jan 27 '17 at 15:51
  • In short: there is no such number as 0.3. Only a limited number of values (exact multiples of powers of 2) are exactly representable in the binary floating point format that is generally used for non-integer calculations in computers. – jasonharper Jan 27 '17 at 15:52
  • This is what floating point numbers do. You need to read a good article on how these work, try the Wikipedia page for a start. https://en.wikipedia.org/wiki/Floating_point – Jackson Jan 27 '17 at 15:56
  • I get the point... but is there a way to "round" the value (like I get 0.300000000004 can't I "tell" the computer to only consider the first two or three digits? If yes, what would be the command to use ? Something like [expr round2digits ( ... ) ] would be great :) – John Jan 27 '17 at 16:18
  • @John You can use `format %f` (or a variant on it) to *render* the value with fewer digits, but the reality remains the same: there is unavoidable precision loss. – Donal Fellows Jan 27 '17 at 18:44
  • There *is* the math::decimal package in Tcllib, though. – Peter Lewerin Jan 27 '17 at 18:56

0 Answers0