From: Ben Roberts (ben_at_roberts.geek.nz)
Date: Thu Dec 08 2011 - 20:07:16 CST

Hi Gianluca,

On 8/12/2011, at 7:09 p.m., Gianluca Interlandi wrote:

> Hi,
>
> I have a question which concerns more TCL rather than VMD. I noticed somthing strange (bug?) with TCL version 8.5 on 64 bit Linux installations (openSUSE 11.1 and above).
>
> For some reason multiplication of an integer with a decimal number gives a weird result (both in the VMD Tk console and using tclsh in the console).
>
> <snip>
>
> Has anybody else had the same problem? Is there a workaround?

My guess is that it's to do with inexact floating-point representations of some decimal numbers. This is a well-known problem (to do with trying to represent decimal floating points in base 2), and is why, for instance, tests for equality on floating point numbers are strongly discouraged when you're programming.

If you want to just print the number out, your best approach is probably to use a formatted print statement with a relatively small number of decimal places. "Relatively small" may mean only one d.p. if that's all you care about. Bear in mind that this approach is likely to pad your decimal numbers with zeros if they're "needed" according to the format.

If you want to use the floating-point number in a script as a variable, and you need to test some other variable against it, the usual way is to inquire about its value, plus or minus some small delta. Example in pseudocode:

BAD:
if (var = 0.1) then
        do stuff
end if

GOOD:
if (var > 0.099995 and var < 0.100005) then
        do stuff
end if

Hope that helps,

Ben