LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to detect underflow in pow()?

Solved!
Go to solution

Hi,

 

In CVI the pow() function does not set the errno variable to indicate that an underflow condition has occurred - is this by design, and is there a workaround? (I would have assumed errno set to ERANGE).

 

Thanks!

0 Kudos
Message 1 of 14
(4,457 Views)

I am not sure that's what you are referring to, but here is a possible workaround.

S. Eren BALCI
IMESTEK
0 Kudos
Message 2 of 14
(4,445 Views)

Hi Eren,

 

thanks a lot for addressing the issue! I have seen this article before when searching for a solution, but I have not studied it further because to me it looked like a MS C specific solution.

 

In my understanding whether or not errno is set to ERANGE is implementation specific, and it appears to me that in the present implementation of CVI the pow() function does not set this value. Maybe I should try using the clang compiler to see if it behaves differently...

 

Or did I miss your point?

 

0 Kudos
Message 3 of 14
(4,443 Views)

Firstly, I would not think CVI and MS implementations of such a standart function will be different.

 

Even if they are different, since it looks like they both suffer from the same problem, the workaround can be applicable to both.

 

When I searched for more info on _matherr. I found this. It says "_UNDERFLOW is not supported" in _matherr.

So the workaround still may not work.

S. Eren BALCI
IMESTEK
0 Kudos
Message 4 of 14
(4,435 Views)

Hello Wolfgang,

the pow() function is a mathematics function. It will only appear a error in the environment. In your application, you have to programm your own error-handling for that function.

You can check the values of the parameter befor the function or check the result at the end (=NAN [not a Number]). I recommend to check the values befor the function.

I hope this helps you further!

Best regards

Lorenz

0 Kudos
Message 5 of 14
(4,425 Views)

Hello Lorenz,

 

thanks for your email, it made me smile... Yes, I am aware that pow() is a mathematics function, I even know what it is supposed to do Smiley Happy

 

OK, so now let's be more serious: I have seen that CVI's C99 extensions provide the classification of FP_SUBNORMAL, may be that's something to try...? How would one use it?

 

0 Kudos
Message 6 of 14
(4,418 Views)

Hi Wolfgang,

 

Is the behavior you're seeing that errno is being set to EDOM instead of ERANGE? Or is it not being set at all?

 

Luis

 

0 Kudos
Message 7 of 14
(4,396 Views)

Hi Luis,

 

Thanks for your reply!

 

errno is not set at all. That's 'o.k.' because that's how it is documented (the case of underflow is not addressed in the docs), but for me it would be better if I could detect underflow events. Say you want to calculate pow ( 8553.0, -165.5 ) the return value is zero because the result is close to zero, i.e. too small to be represented within the 64 bit data range of doubles.

 

Wolfgang

0 Kudos
Message 8 of 14
(4,383 Views)
Solution
Accepted by topic author Wolfgang

Okay, I see. At first, I was considering that the docs did address this: I was interpreting the sentence "If the magnitude of Input Value and Power are such that the computed value cannot be represented as a double..." to be referring to underflows as well, but since the rest of the paragraph says that, in that case, the function returns Infinity, then it's clearly not trying to address underflow.

 

The value of errno on underflow is not specified by the standard ("If the result underflows, the function returns a value whose magnitude is no greater than the smallest normalized positive number in the specified type and is otherwise implementation- defined; whether errno acquires the value ERANGE is implementation-defined.") but we'll take a look at it for the next version.

 

Luis

0 Kudos
Message 9 of 14
(4,374 Views)

So many looks for the next version Smiley Very Happy I guess CVI2013 should be released on my birthday Smiley Wink

 

OK, the question is almost solved, may be you could kindly enlighten me about the functionality of FP_SUBNORMAL, what is it supposed to do?

0 Kudos
Message 10 of 14
(4,369 Views)