home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / pascal / 7940 < prev    next >
Encoding:
Internet Message Format  |  1993-01-07  |  1.8 KB

  1. Path: sparky!uunet!spool.mu.edu!caen!zaphod.mps.ohio-state.edu!swrinde!cs.utexas.edu!ut-emx!ccwf.cc.utexas.edu!mmigdol
  2. From: mmigdol@ccwf.cc.utexas.edu (michael a migdol)
  3. Newsgroups: comp.lang.pascal
  4. Subject: TP 6.0 Strange rounding problem (Debugger is *lying* to me!)
  5. Message-ID: <86277@ut-emx.uucp>
  6. Date: 7 Jan 93 14:32:19 GMT
  7. Sender: news@ut-emx.uucp
  8. Reply-To: mmigdol@ccwf.cc.utexas.edu (michael a migdol)
  9. Organization: The University of Texas at Austin, Austin TX
  10. Lines: 43
  11. Originator: mmigdol@grumpy.cc.utexas.edu
  12.  
  13. Here's a fragment of a program that was causing me some major grief last night.
  14.  
  15. PROGRAM FOO;
  16.  
  17. VAR
  18.     HighFreq, ToneFreq : Integer;
  19.  
  20. Procedure Foo1(...VAR factor : Real;...)
  21. VAR
  22.     ...
  23.     diff : Real;
  24.     ...
  25.  
  26. BEGIN
  27.     diff := HighFreq - ToneFreq;
  28.     ...
  29.     diff := diff / Factor;
  30.     HighFreq := Round(ToneFreq + diff);
  31.     ...    
  32. END; { Procedure }
  33.  
  34.     Ok, boys and girls, assuming that upon entry to Foo1, variables have the
  35. following values, what should HighFreq be after the last statement is executed?
  36.     HighFreq = 525
  37.     ToneFreq = 500
  38.     factor = 2.0
  39.  
  40.     Well, the first statement sets diff to 25.0. The second sets it to 12.5.
  41. ToneFreq + diff is then 512.5, and round of this is...
  42.     512???
  43.  
  44.     The plot thickens even more. If I trace variables up to the last line, and
  45. o a watch on Round(ToneFreq + diff), THAT returns 513! But execution of
  46. the last statement puts 512 in HighFreq! It's this discrepency that has kept
  47. me from dismissing this as a simple rounding error somewhere. (12.5 really 
  48. equals 12.4999999999999 or something like that). 
  49.  
  50.     I've got a temporary fix for the problem by replacing Round(ToneFreq + diff)
  51. with Trunc(ToneFreq + diff + 0.5), but I'd still like to know exactly what's 
  52. going on. Does anyone have any ideas?
  53.  
  54. Thanks in advance...
  55.      Michael
  56.