LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

cvi divide by zero fault

Solved!
Go to solution

Hello,

 

I am using NI LabWindows CVI 2012.

I have a very simlpe piece of code:

double a = 4/0;  

 

I would like for this piece of code to return NaN, or atleast do anything that isn't stopping execution upon compile.  Is there a way to either turn on NaN handling funtionality, or add an exception to the compiler that says: if divide by zero is found, return zero" or something?

Any help here would be greatly appreciated

0 Kudos
Message 1 of 3
(2,918 Views)
Solution
Accepted by brentjustice

Hi,

 

It's probably trying to do an integer divide, and then converting the result to a double.  Dividing by zero with an integer will crash your application.  I would try this:

double a = 4.0/0.0;  

 

Where adding the .0 at the end tells the compiler that it's a floating point type, where dividing by zero isn't as bad.  Alternatively,

 

double a;

a = NotANumber ();

 

May also accomplish what you want.  If you want to check for NaN you can do something like this:

 

if(IsNotANumber(a))

   a = 0.0;

 

Message 2 of 3
(2,911 Views)

Thank you for the response, this is very interesting.  You are correct, it seems that it is acceptable to divide by zero with floats or doubles, but not integers.  This is kinda odd that they wouldn't extend the functionality to integers.

 

 

0 Kudos
Message 3 of 3
(2,903 Views)