kamal@wpi.WPI.EDU (Kamal Z Zamli) (02/21/91)
In article <1991Feb20.170152.5401@ux1.cso.uiuc.edu> mead@uxh.cso.uiuc.edu (Alan David Mead) writes: >I finally tracked down a problem with an application writen in >TP5.5: Since my values are all positive, I used words instead of >integers. The ABS() function, however, does not operate as (I) >expected with word parameters. This code demonstrates. While I >can imagine how the value is produced by ABS(), I think the manual's >description is deceptive. What does the net think? >============================================================ >program damnit; > >const > Start : integer = 900; > >var > w1,w2 : word; > i1, i2 : integer; > > function Value:word; > begin > Start := Start - 200; > Value := Start; > end; > >begin > w1 := Value; > w2 := Value; > writeln( 'ABS( ',w2,' - ',w1,' ) =? ',abs( w2 - w1 )); > > i1 := Value; > i2 := Value; > writeln( 'ABS( ',i2,' - ',i1,' ) =? ',abs( i2 - i1 )); >end. I might be wrong but I dont think you can change a constant.....and expect it to be the same again.. In order to avoid such problem just declare a dummy var and assign it to var start each time the fucntion is called. hope that help
rind@popvax.uucp (747707@d.rind) (02/21/91)
In article <1991Feb20.170152.5401@ux1.cso.uiuc.edu> mead@uxh.cso.uiuc.edu (Alan David Mead) writes: >I finally tracked down a problem with an application writen in >TP5.5: Since my values are all positive, I used words instead of >integers. The ABS() function, however, does not operate as (I) >expected with word parameters. This code demonstrates. While I >can imagine how the value is produced by ABS(), I think the manual's >description is deceptive. What does the net think? [Code deleted.] I don't see this as a bug. It's not ABS that's not functioning as you expect. Any other function would have behaved the same way. When you took the difference of two WORD variables inside a function call, the result was a WORD. Since a WORD is unsigned, it could never go negative, and so the difference had to be a number between 0 and 65535. The function call ABS(w2-w1) is equivalent to creating a third word variable w3 and doing w3 := w2-w1 followed by ABS(w3). I can't see the point in ever taking the absolute value of a WORD type variable, but perhaps someone else has some thoughts on this. David Rind rind@popvax.harvard.edu
terra@diku.dk (Morten Welinder) (02/22/91)
mead@uxh.cso.uiuc.edu (Alan David Mead) writes: >I finally tracked down a problem with an application writen in >TP5.5: Since my values are all positive, I used words instead of >integers. The ABS() function, however, does not operate as (I) >expected with word parameters. This code demonstrates. While I >can imagine how the value is produced by ABS(), I think the manual's >description is deceptive. What does the net think? >============================================================ >program damnit; >const > Start : integer = 900; >var > w1,w2 : word; > i1, i2 : integer; > function Value:word; > begin > Start := Start - 200; > Value := Start; > end; >begin > w1 := Value; > w2 := Value; > writeln( 'ABS( ',w2,' - ',w1,' ) =? ',abs( w2 - w1 )); > i1 := Value; > i2 := Value; > writeln( 'ABS( ',i2,' - ',i1,' ) =? ',abs( i2 - i1 )); >end. >-- >Alan Mead : mead@uxh.cso.uiuc.edu (w1-w2) has type 'word' and so assumed positive; you force an overflow since w2<w1, but the result is positive. You have to convert either w1 or w2 to type 'integer' or 'longint' in order to allow negative results: integer(w1)-integer(w2) Morten Welinder
reino@cs.eur.nl (Reino de Boer) (02/22/91)
In <5777@husc6.harvard.edu> rind@popvax.uucp (747707@d.rind) writes: >I don't see this as a bug. It's not ABS that's not functioning as you >expect. Any other function would have behaved the same way. When >you took the difference of two WORD variables inside a function call, >the result was a WORD. Since a WORD is unsigned, it could never >go negative, and so the difference had to be a number between >0 and 65535. The function call ABS(w2-w1) is equivalent to creating >a third word variable w3 and doing w3 := w2-w1 followed by ABS(w3). >I can't see the point in ever taking the absolute value of a WORD >type variable, but perhaps someone else has some thoughts on this. You are ABSolutely right, but when the original poster really wants the equivalent of if w1 > w2 then (hope you see the point) w3 := w1 - w2 else w3 := w2 - w1 to get the absolute difference between two words, the following code seems to work: w3 := word(abs(integer(w1 - w2))) but, as anyone who understands the problem knows, this fails for any values of w1 and w2 with: w1 < w2 and w2 - w1 > 32768 since that large a difference cannot be represented as an abs(integer(i)) So, your best bet would be to write something like function abs_diff( w1, w2 : word ) : word; begin if w1 > w2 then abs_diff := w1 - w2 else abs_diff := w2 - w1 end; Hope this helps -- Reino >David Rind >rind@popvax.harvard.edu -- Reino R. A. de Boer "We want to build the right product right, right?" Erasmus University Rotterdam ( Informatica ) e-mail: reino@cs.eur.nl