RE: Really strange problem, for very bright people

Stephen Wood <[email protected]> Fri, 8 Aug 2003 12:07:40 +0200
Newsgroups gmane.comp.lang.delphi.programming
Message-ID <74D67F1862B5D711A9CA00B0D0D1F4C981AB64@dhexchange3.discoveryhealth.co.za>
Thanks Jeff,

What I can't understand is that the calculation actually works with the
other types (extended, double, etc) only if the order is changed, so
therefore rounding issues, and insignificant digits are not the problem,
because the calculation does work....but just in a different order.

And secondly, how is a developer supposed to understand in what order to
write the calculations, unless the bug actually manifests itself?

And thirdly, this calculation is used many times with different input
parameters and calculates correctly, but it seems that the bug only happens
when "certain" input values are used....really really weird...

But thanks anyway!!!

-----Original Message-----
From: Jeff Steinkamp - N7YG [mailto:[email protected]] 
Sent: Friday 08 August 2003 09:53
To: [email protected]
Subject: RE: [Delphi] Really strange problem, for very bright people

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/ 
> 
> 
> 



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/ 



[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/