PC Review


Reply
Thread Tools Rate Thread

Excel is messing with the math coprocessor

 
 
Lynn McGuire
Guest
Posts: n/a
 
      15th Mar 2012
I have just noticed that Excel is changing the floating
point round off handling in the math coprocessor for my
C++ and Fortran DLL. Has anyone run into this ? I am
getting ready to try venturing into _Control87 calls
when my DLL is called from Excel.

I have verified this problem in both Excel 2003 and 2010.

Thanks,
Lynn
 
Reply With Quote
 
 
 
 
GS
Guest
Posts: n/a
 
      15th Mar 2012
Lynn McGuire was thinking very hard :
> I have just noticed that Excel is changing the floating
> point round off handling in the math coprocessor for my
> C++ and Fortran DLL. Has anyone run into this ? I am
> getting ready to try venturing into _Control87 calls
> when my DLL is called from Excel.
>
> I have verified this problem in both Excel 2003 and 2010.
>
> Thanks,
> Lynn


Excel has its own method of handling floating point rounding and so
expect it to be different compared to another methodology.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


 
Reply With Quote
 
 
 
 
Martin Brown
Guest
Posts: n/a
 
      15th Mar 2012
On 15/03/2012 16:16, Lynn McGuire wrote:
> I have just noticed that Excel is changing the floating
> point round off handling in the math coprocessor for my
> C++ and Fortran DLL. Has anyone run into this ? I am


A long time ago and not specifically with Excel. It isn't safe to assume
inside a DLL that you will be handed the FPU in the state that your
compiler expects to find it. If your computation is sensitive to
rounding then you can get systematic errors introduced.

This sort of thing usually stems from rounding values for display. I
suspect Excel is setting the coprocessor to the state it requires and
that setting is not what Fortran and C++ compilers expect.

> getting ready to try venturing into _Control87 calls
> when my DLL is called from Excel.
>
> I have verified this problem in both Excel 2003 and 2010.
>
> Thanks,
> Lynn


--
Regards,
Martin Brown
 
Reply With Quote
 
joeu2004
Guest
Posts: n/a
 
      15th Mar 2012
"Lynn McGuire" <(E-Mail Removed)> wrote:
> I have just noticed that Excel is changing the floating
> point round off handling in the math coprocessor for my
> C++ and Fortran DLL. Has anyone run into this ?


I do not use C++ or Fortran, but what you describe does not surprise me,
speaking as a computer and software architect.

The floating-point unit is a global resource. I would not expect any
application to save and restore the FPU's previous state before making
application-specific changes.

Instead, I would expect each application to set the FPU state as it requires
before using it.

So the fault is not Excel's.

Instead, I would expect C++ and Fortran to configure the FPU as each
language requires it, especially the floating-point rounding option.

Alternatively, perhaps your application should configure the FPU as you
require it.

 
Reply With Quote
 
Lynn McGuire
Guest
Posts: n/a
 
      15th Mar 2012
On 3/15/2012 11:34 AM, joeu2004 wrote:
> "Lynn McGuire" <(E-Mail Removed)> wrote:
>> I have just noticed that Excel is changing the floating
>> point round off handling in the math coprocessor for my
>> C++ and Fortran DLL. Has anyone run into this ?

>
> I do not use C++ or Fortran, but what you describe does not surprise me, speaking as a computer and software architect.
>
> The floating-point unit is a global resource. I would not expect any application to save and restore the FPU's previous state before
> making application-specific changes.
>
> Instead, I would expect each application to set the FPU state as it requires before using it.
>
> So the fault is not Excel's.
>
> Instead, I would expect C++ and Fortran to configure the FPU as each language requires it, especially the floating-point rounding
> option.
>
> Alternatively, perhaps your application should configure the FPU as you require it.


It looks like that I need to save the current state
of the FPU, reconfigure the FPU and set the FPU back
to the saved state at each entry point in my DLL.

Lynn


 
Reply With Quote
 
joeu2004
Guest
Posts: n/a
 
      16th Mar 2012
"Lynn McGuire" <(E-Mail Removed)> wrote:
> It looks like that I need to save the current state
> of the FPU, reconfigure the FPU and set the FPU back
> to the saved state at each entry point in my DLL.


I would think you only need to configure the FPU the way that you require
it, not save and restore the previous state.

As I said, I would expect each application to set up the FPU as it requires.
It really cannot assume that the FPU is in some "default" state, AFAIK.

(However, it is true that the FPU is in some well-known state at power-on.)

In any case, I would think you only need to program this once, then call the
procedure from each entry point -- at worst.

Alternatively, perhaps you can put the code into a single procedure that is
called when the DLL is loaded. I am not familiar with Windows DLLs. But it
is common practice in other architectures.

 
Reply With Quote
 
Lynn McGuire
Guest
Posts: n/a
 
      16th Mar 2012
On 3/15/2012 7:23 PM, joeu2004 wrote:
> "Lynn McGuire" <(E-Mail Removed)> wrote:
>> It looks like that I need to save the current state
>> of the FPU, reconfigure the FPU and set the FPU back
>> to the saved state at each entry point in my DLL.

>
> I would think you only need to configure the FPU the way that you require it, not save and restore the previous state.
>
> As I said, I would expect each application to set up the FPU as it requires. It really cannot assume that the FPU is in some
> "default" state, AFAIK.
>
> (However, it is true that the FPU is in some well-known state at power-on.)
>
> In any case, I would think you only need to program this once, then call the procedure from each entry point -- at worst.
>
> Alternatively, perhaps you can put the code into a single procedure that is called when the DLL is loaded. I am not familiar with
> Windows DLLs. But it is common practice in other architectures.


The FPU is is the state 0x4000 every time my DLL
is called from Excel. I believe that 0x4000
means round up on the SSE2.

I am using the following code to reset the FPU in
my DLL:

unsigned checkMathCoprocessorStatus ()
{
unsigned old87Status = 0;
unsigned new87ControlWord = 0;
unsigned new87ControlMask = 0;
unsigned new87result = 0;

old87Status = _status87 ();
if (old87Status != 0)
{
char msg [4096];
sprintf (msg, "\nThe Math Coprocessor status is 0x%x, resetting to zero\n\n", old87Status);
writeLineToScreen (msg);
new87result = _control87 (new87ControlWord, new87ControlMask);
}

return old87Status;
}

Lynn
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel and the Math Coprocessor for DLLs Lynn McGuire Microsoft Excel Discussion 55 7th Apr 2012 05:26 AM
Excel and the Math Coprocessor for DLLs Lynn McGuire Microsoft Excel Programming 54 7th Apr 2012 05:26 AM
Excel is messing with the math coprocessor Lynn McGuire Microsoft Excel Programming 6 16th Mar 2012 04:34 PM
excel math forumla? (simple math problem inside for math people!) Jason Microsoft Excel Discussion 3 16th Feb 2006 11:54 AM
math coprocessor? =?Utf-8?B?R2F5bGU=?= Microsoft Access 9 18th May 2005 03:19 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:37 PM.