RE: Really strange problem, for very bright people

"Jeff Steinkamp - N7YG" <[email protected]> Fri, 8 Aug 2003 00:53:12 -0700
Newsgroups gmane.comp.lang.delphi.programming
Message-ID <000001c35d82$1e15e970$1d250142@HAMRADIO>
Try this and see what happens

vary vCalc1,vCalc2,vCalc3 : real;  //works with the single type also
    vCalc11,vCalc21,vCalc31 : real;
    vCalc111,vCalc211,vCalc311 : real;
begin
vCalc1 := (4.328247 * ( 33660 / 1000 ) * 1.06 * 1 * 1 * 1.5 * 1.035 );
vCalc11 := ( 1.5 * 1 * ( 33660 / 1000 ) * 4.328247 * 1.06 * 1.035 );
vcalc111 := vcalc1-vcalc11;

vCalc2 := (4.328247 * ( 33660 / 1000 ) * 1.06 * 1 * 1 * 1.5 * 1.035 );
vCalc21 := (4.328247 * 1.06 * 1.5 * 1 * ( 33660 / 1000 ) * 1.035 );
vcalc211 := vcalc2 - vcalc21;

vCalc3 := (4.328247 * ( 33660 / 1000 ) * 1.06 * 1 * 1 * 1.5 * 1.035 );
vcalc31 := (4.328247 * 1.06 * 1.035 * 1.5 * 1 * ( 33660 / 1000 ) );
vcalc311 := vcalc3 - vcalc31;

The reason you are not having problems with the Comp and Currency type
is because of rounding of the numbers.  The anomoly you are seeing is a
floating point problem that has been around for years that I am aware
of.  If you break the equation into smaller chucks it works much better,
but if you are looking for extremely small resolution this could be a
problem.

Jeff Steinkamp





> -----Original Message-----
> From: Stephen Wood [mailto:[email protected]] 
> Sent: 08 August, 2003 00:00
> To: '[email protected]'
> Subject: [Delphi] Really strange problem, for very bright people
> 
> 
> Hi all,
>  
> I'm looking for some really bright people to help with this 
> one, because in
> my entire life, I've never seen something as strange as this.
>  
> We have a calculation engine that does a whole whack of 
> calculations...on we
> picked up a bug with it. We then stripped down the 
> calculation to bare bones
> and were quite amazed at what we found....below is a code 
> snippet to explain
> what's happening...
>  
> var
>   vCalc : extended;
>  
> vCalc := (4.328247 * ( 33660 / 1000 ) * 1.06 * 1 * 1 * 1.5 * 
> 1.035 ) - ( 1.5
> * 1 * ( 33660 / 1000 ) * 4.328247 * 1.06 * 1.035 );
>  
> This gives an answer of 2.7755575616e-17
>  
> Now do this....
>  
> vCalc := (4.328247 * ( 33660 / 1000 ) * 1.06 * 1 * 1 * 1.5 * 
> 1.035 ) - (
> 4.328247 * 1.06 * 1.5 * 1 * ( 33660 / 1000 ) * 1.035 );
>  
> You'll notice that I just moved the 4.328247 and 1.06 to the 
> front of the of
> the second brackets and you get an answer of 1.3877787808e-17
>  
> Now try this...
>  
> vCalc := (4.328247 * ( 33660 / 1000 ) * 1.06 * 1 * 1 * 1.5 * 
> 1.035 ) - (
> 4.328247 * 1.06 * 1.035 * 1.5 * 1 * ( 33660 / 1000 ) );
>  
> All I've done now is move the 1.035 after the 1.06 in the 
> second brackets
> and I get an answer of 0 (which is actually the correct 
> answer, which we
> expected, and which Excel gives us as well)
>  
> Just moving the value around in a multiplication calculation produces
> different results....the different results are consistent with single,
> double and extended types, but with comp and currency types, 
> the calculation
> is correct every time....
>  
> Can anyone explain this?
>  
> Thanks
> Steve
>  
> 
> 
> [Non-text portions of this message have been removed]
> 
> 
> ------------------------ Yahoo! Groups Sponsor 
> ---------------------~-->
> Buy Ink Cartridges or Refill Kits for Your HP, Epson, Canon or Lexmark
> Printer at Myinks.com. Free s/h on orders $50 or more to the 
> US & Canada. http://www.c1tracking.com/l.asp?cid=5511
> http://us.click.yahoo.com/l.m7sD/LIdGAA/qnsNAA/i7folB/TM
> --------------------------------------------------------------
> -------~->
> 
> CodeCoffer - new Delphi code protection Tool!
> http://www.delphicollection.com/public/CodeCoffer.htm
> ---------------------------------------------------------------
> Unsubscribe:[email protected]
> List owner:[email protected]
> --------------------------------------------------------------- 
> 
> Your use of Yahoo! Groups is subject to 
> http://docs.yahoo.com/info/terms/ 
> 
> 
> 


------------------------ Yahoo! Groups Sponsor ---------------------~-->
Buy Ink Cartridges or Refill Kits for Your HP, Epson, Canon or Lexmark
Printer at Myinks.com. Free s/h on orders $50 or more to the US & Canada. http://www.c1tracking.com/l.asp?cid=5511
http://us.click.yahoo.com/l.m7sD/LIdGAA/qnsNAA/i7folB/TM
---------------------------------------------------------------------~->

CodeCoffer - new Delphi code protection Tool!
http://www.delphicollection.com/public/CodeCoffer.htm
---------------------------------------------------------------
Unsubscribe:[email protected]
List owner:[email protected]
--------------------------------------------------------------- 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/