Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!zephyr.ens.tek.com!vice!bobb From: bobb@vice.ICO.TEK.COM (Bob Beauchaine) Newsgroups: comp.lang.pascal Subject: Re: Is this a bug in TurboPascal Message-ID: <7401@vice.ICO.TEK.COM> Date: 3 May 91 21:16:24 GMT References: <1991May1.021059.2129@ux1.cso.uiuc.edu> <1991May1.060345.26511@newsserver.sfu.ca> <1991May1.134303.7289@maytag.waterloo.edu> Reply-To: bobb@vice.ICO.TEK.COM (Bob Beauchaine) Organization: Tektronix, Inc., Beaverton, OR. Lines: 40 In article <1991May1.134303.7289@maytag.waterloo.edu> dmurdoch@watstat.waterloo.edu (Duncan Murdoch) writes: >Expressions like > > real1 * int1 * int2 >and > int1 * int2 * real1 > >are especially dangerous, because you don't know in advance how TP will pair >the operands, and it makes a difference when overflows can occur. (In TP >6.0, with the values above, the results of those two expressions are >65536.0 and 0.0 respectively, because on Wednesdays it operates from left >to right. I haven't tried the program on other days of the week, so I'm >not sure what happens then.) > The only (?) sure way to get around this is to typecast. Choosing a temporary result variable can guarantee you a correct value, no matter what day of the week it is. For instance, you can't overflow a long integer with the product of two integers, no matter what the magnitude of the two integers. So something like this will work, independent of the compiler's ordering of terms and factors, because of the precedence of parenthesis: real1 * longint(int1 * int2) or longint(int1 * int2) * real1 Note, however, that you can only have two *distinct* operands inside the typecast, or the problem can occur all over again inside the parenthesis. I found this out, as usual, by having a program crash from overflow. /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ Bob Beauchaine bobb@vice.ICO.TEK.COM C: The language that combines the power of assembly language with the flexibility of assembly language.