A Double divided by zero is returning a Divide by Zero error
I am experiencing an unexpected behaviour and was hoping someone could help with some guidance as to what areas to focus an investigation on.
开发者_运维百科I have two methods, one essentially performs a divide by zero test on a double, the second calls an extern
method for an unmanaged dll.
Note: In the .Net runtime, dividing a Double by Zero should return an Infinity value (amusingly of either positive or negative flavours).
Pseudocode for what I am doing looks something like this:
InfinityTest(); // Returns an Infinity value as expected
DllCall();
InfinityTest(); // Divide by zero error on second call.
The first call to InfinityTest()
returns the value Infinity as expected. The second call to InfinityTest()
throws a Divide by Zero exception that I didn't expect.
Update
The effective InfinityTest()
code below. For brevity I've removed try/catch elements, etc. I do not have permission to go into details about the DllCall()
pseudocode element, apologies.
private double InfinityTest()
{
double a = 1.0;
int b = 0;
return a / b;
}
Since it sounds like your DLL is changing the FP status word on you, your only choice may be to change it back. I would suggest P/Invoke to _clearfp
or _fpreset
. Here are their P/Invoke signatures:
[DllImport("msvcrt.dll")]
static extern UInt32 _clearfp();
[DllImport("msvcrt.dll")]
static extern void _fpreset();
This may not reset things back to exactly the way they were, but hopefully it will be close enough.
精彩评论