NI Home
Cart Cart | Help
Hello Events Academic NI Developer Zone Support Solutions Products & Services Contact NI MyNI
You are here: 
NI Home > NI Developer Zone > NI Discussion Forums


Reply
Active Participant
Wolfgang
Posts: 1,929
0 Kudos
Accepted Solution

How to detect underflow in pow()?

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!

Active Participant
ebalci
Posts: 582
0 Kudos

Re: How to detect underflow in pow()?

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

S. Eren BALCI
www.aselsan.com.tr
Active Participant
Wolfgang
Posts: 1,929
0 Kudos

Re: How to detect underflow in pow()?

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?

 

Active Participant
ebalci
Posts: 582
0 Kudos

Re: How to detect underflow in pow()?

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
www.aselsan.com.tr
Member
LoCa
Posts: 83
0 Kudos

Re: How to detect underflow in pow()?

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

Active Participant
Wolfgang
Posts: 1,929
0 Kudos

Re: How to detect underflow in pow()?

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 :smileyhappy:

 

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?

 

Active Participant
LuisG
Posts: 1,806
0 Kudos

Re: How to detect underflow in pow()?

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

 

Active Participant
Wolfgang
Posts: 1,929
0 Kudos

Re: How to detect underflow in pow()?

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

Active Participant
LuisG
Posts: 1,806
0 Kudos

Re: How to detect underflow in pow()?

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

Active Participant
Wolfgang
Posts: 1,929
0 Kudos

Re: How to detect underflow in pow()?

So many looks for the next version :smileyvery-happy: I guess CVI2013 should be released on my birthday :smileywink:

 

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?

By using this web site, you accept the Terms of Use for this web site. Please read these Terms of Use carefully before using any part of this site. Please go here for information on ni.com's copyright infringement policy.
My Profile | Privacy | Legal | Contact NI © 2011 National Instruments Corporation. All rights reserved.    |    E-Mail this Page E-Mail this Page