Solved

Numbers & Delphi

Posted on 2009-05-10
25
499 Views
Last Modified: 2012-05-06
Hello,

    I am using Delphi 2009, And I have the below code, Which make some calculations and the Result should appear in (Edit2) Editbox. I have 7 Machines running my Application, I run my application on all 7 machines to get the result  and Result was like follows:

Machine1 (Dual Core) (XP Home) : Result is : 0.555
Machine2 (Dual Core) (XP Home) : Result is :  0.554
Machine3 (Dual Core) (XP Home) : Result is :  0.554
Machine4 (Dual Core) (XP Home) : Result is :  0.554
Machine5 (Core 2 Duo) (XP Professional) : Result is :  0.554
Machine6 (Core 2 Duo) (Vista Business) : Result is :  0.554
Machine7 (Dual Core) (Vista Basic) : Result is :  0.554

   Do you have any explanation why Machine1 gave me the Result (0.555) while other 6 machines gave the result (0.554)?





var

  R, X, Z : Real;

  S, W : String;

begin

 

R := 1.1085884101;

 

S := FloatToStrF(R, ffFixed, 15, 3 );

 

Edit1.Text := S;

 

X := 0.500;

 

Z := X * StrToFloat(Edit1.Text);

 

W := FloatToStrF(Z, ffFixed, 15, 3 );

 

Edit2.Text := W;

 

end;

Open in new window

0
Comment
Question by:i7mad
  • 12
  • 8
  • 4
  • +1
25 Comments
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 24349088
Interesting.

It sounds like a different floating point library is being invoked on the first pc.  I thought that maybe Machine 1 is your development pc, but then maybe not, as it's running Home Edition.
0
 
LVL 6

Expert Comment

by:JosephGlosz
ID: 24349110
there are only two possibilities for the discrepancy here: either the computation is different between the machines, or the computation is the same across all the machines but the conversion (FloatToStr) worked differently.  

So to see which this is, I would add more edit boxes that show the same result, but with additional precision. So you can see exactly what is going on.

In addition, perhaps you could also add a different method of conversion, such as using the "format" statement.  

So for example, do this instead of your code:

R := 1.1085884101;
Edit1.Text := FloatToStrF(R, ffFixed, 15, 3);
Edit3.Text := FloatToStrF(R, ffFixed, 15, 7);  //additional precision here
Edit5.Text := format('%15.7f",[R]);              

Z := 0.500 * StrToFloat(Edit1.Text);
Edit2.Text  := FloatToStrF(Z, ffFixed, 15, 3);
Edit4.Text  := FloatToStrF(Z, ffFixed, 15, 7);
Edit6.Text := format('%15.7f",[Z]);

After you get these results, you can narrow down the causes...


0
 

Author Comment

by:i7mad
ID: 24349130
moorhouselondon: The development pc is Machine7
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 24349396
What is the spec of the cpu in m/c 1 compared with the others?
0
 

Author Comment

by:i7mad
ID: 24350062
It is a Dual Core, like the majority

  But Excat model and spec. I will provide them tomorrw , because I am home now. and the machine at my work place.

  I will run Everest and provide detailed information for all machines.

  Do you think calculating in high level langauges such as delphi depends on CPU?

  Then, How large Database Applications (such as Cost calculations) will guarantee  exact results on many machines which has differenet types of processors?
0
 

Author Comment

by:i7mad
ID: 24350099
JosephGlosz:

  Your Code is ready now, But I am home and I will make this Test Tomorrow at work (since all 7 machines are at my work place)

  and I will make a screen shot for all PCs and provide them to you.
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 24350175
Apparently this is the standard to which floating point implementations should adhere to:-

http://en.wikipedia.org/wiki/IEEE_754

Many years ago Intel had a bug in their Pentium fpu - from recollection I believe some software vendors coded in a method of detecting the flaw and compensate for it.

http://en.wikipedia.org/wiki/Pentium_FDIV_bug
0
 

Author Comment

by:i7mad
ID: 24352183
Machine7 (buggy machine) spec. :
        http://www.alfaisalcorp.com/ahmad/BugMachine.htm


development machine spec.:
   http://www.alfaisalcorp.com/ahmad/Developmachine.htm
0
 

Author Comment

by:i7mad
ID: 24352408
I found something STRANGE guys!!!!!!!!!

   it is the Value of X

 when X = 0.500 , Machine1 Result  (0.555)
                              All Others Result (0.554)

 when X = 1.500 , Machine1 Result  (1.664)
                              All Others Result (1.663)

 when X = 876.500 , Machine1 Result  (972.039)
                                 All Others Result (972.038)
---------------------
 when X = 0.200 , Machine1  Result (0.222)
                              All Others Result (0.222)  SAME!

 when X = 1150 , Machine1 Result  (1275.350)
                             All Others Reslut (1275.350) SAME!

 when X = 1.555 , Machine1 Result (1.724)
                             All Others Reslut (1.724) SAME!

 when X = 1.050 , Machine1 Result (1.164)
                             All Others Reslut (1.164) SAME!


 when X = 1.005 , Machine1 Result (1.115)
                             All Others Reslut (1.115) SAME!

  That is mean when X floating point is .500 then the difference happen!

I am confused !!!!!!!!! what the hell with .500 in that machine

0
 

Author Comment

by:i7mad
ID: 24353221
is int the result before using the FloatToStrF 0.5545 and the precision should be 0.555 not 0.554?

I have the feeling the Machine1 is correct result and others are not !!!
0
 

Author Comment

by:i7mad
ID: 24353543
oh no

applying ffFixed on 0.5545 will result 0.554 not 0.555

so question cam to the front again, why Machine one result 0.555 !
0
 
LVL 45

Expert Comment

by:aikimark
ID: 24355950
Are these .Net assemblies or Win32 executables?

The round-up and round-down looks like banker's rounding.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 31

Expert Comment

by:moorhouselondon
ID: 24356375
I thought the question was more about why the results are different on different pc's, rather than how the result was arrived at.  I'm not complaining btw, just interested to know the outcome as to how the results came to be different :-)  This looks to me like some kind of chipset difference.  Looking at the Everest snapshots of the machines, they are both Intel cpu's, but very different in spec.
0
 

Author Comment

by:i7mad
ID: 24358553
aikimark: they are Win32.

what do you mean by banker's rounding??
0
 

Author Comment

by:i7mad
ID: 24358606
moorhouselondon: yes my questions is about why the results are different on different pc's

I show you how the result was arrived at because I want to give you full details and results, many inputs.

  It is not that big difference between machines.

  how about the operating system? The buggy machine is the only machine runing on XP SP2, all other XP machines are running on XP SP3

 Vista machine, SP1 .

 all of them are up tp date with microsoft, except the buggy machine,

can we consider this matter? or the OS has nothing to do with it?

 
0
 
LVL 45

Accepted Solution

by:
aikimark earned 250 total points
ID: 24358945
Banker's Rounding is also known as Gaussian rounding.
Reference: http://en.wikipedia.org/wiki/Rounding#Round-to-even_method

Some functions in Delphi employ this, such as the Round() function:
http://www.delphibasics.co.uk/RTL.asp?Name=Round

Discussion:
http://groups.google.com/group/borland.public.delphi.non-technical/browse_thread/thread/6bf1cb6a7a1a3acf/45f08c0a884dc02e?#45f08c0a884dc02e

Nice floating point explanation including conversions:
http://www.merlyn.demon.co.uk/pas-chop.htm#nDP

======================
* What version of Delphi is this application?
* Is there any difference in the maintenance levels for these systems?
* Are there any Regional Settings differences between Machine1 and the other PCs?

======================
Since Real is obsolete, try using Double as the data type.  see snippet


  R, X, Z : Double;

Open in new window

0
 
LVL 31

Assisted Solution

by:moorhouselondon
moorhouselondon earned 250 total points
ID: 24358966
This link:-

http://www.stats.uwo.ca/faculty/murdoch/software/compilingDLLs/pascal.html

see the section which starts:-

Preserving registers

Delphi uses the same register preserving conventions as R, so this is mostly done automatically. The only problem that is likely to arise is with the floating point processor (FPU) registers. Some versions of Delphi change the FPU control word upon entry to a DLL (but this is not true of Delphi 5); all Windows programs are at risk of changing the registers whenever they call a sloppily written DLL, such as the ones that are used in the common dialogs (File Open, etc.).

To protect against messing up R by changing the FPU control word, the following strategy works:

etc.


Ok the article is quite old, but it points the finger at DLL's that do not correctly initialise the FPU.  The facts are probably that you are running this test with nothing else of consequence running, and you would get inconsistent answers on different occasions if that were not the case - so it looks to me like a DLL your program calls is a different version on Machine 1.  Quite likely if this is SP2 and the rest are SP3  The sure-fire way to determine this is to visit www.sysinternals.com and download something from there.  I'll just have a look at the site and will report back in a few mins.
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 24359004
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 24359054
Found this too, ok nowhere near Delphi, but relevant nonetheless:-

http://support.microsoft.com/kb/178527

Rather than drop into low level code to reset the fpu, this article suggests a simple workaround.
0
 

Author Comment

by:i7mad
ID: 24359219
aikimark:
* What version of Delphi is this application?   Delphi 2009

* Is there any difference in the maintenance levels for these systems? no

* Are there any Regional Settings differences between Machine1 and the other PCs? I checked them all, no difference

I will try using Double instead of Real and inform you the result.

Thanks for the links, very useful
0
 

Author Comment

by:i7mad
ID: 24359269
moorhouselondon: much appreciate your kind interest.

   I will read these articles carefuly and then give you my feedback and I hope we can solve this problem.

But I suggest for myself to update Machine1 first for the latest windows updates and then try, if problem solved then it is something related to OS, otherwise... hmmm .. I don't know

let me read articles and understand ur point of view

it is almost 12:30 am now, so sleepy, Tomorrow will continue...

Thanks alot guys

much appreciate your kind efforts.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 24359362
I looked at the first moorhouselondon link.  Given the currency of the hardware (dual core and 2 core dual) and the lessons learned from the  Pentium II debacle, I don't think the FPU (hardware) is suspect.
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 24361619
Yes I think a lot was learned from the Pentium II prob - but there's new hardware, new ways to prod it.  Could be a glitch in the way it is being prodded, which is alluded to in other links.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 24363124
@i7mad

What did you do to make Machine1 results the same as the other systems?
0
 

Author Comment

by:i7mad
ID: 24363141


aikimark: just updated the windows to SP3.

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This algorithm (in C#) will resize any image down to a given size while maintaining the original aspect ratio. The maximum width and max height are both optional but if neither are given, the original image is returned. This example is designed t…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now