From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

why does Labview fail to do a simple calculation

Hello,

I am using labview for a couple of years and I am always of its capability. Today, I spent a full day to find a bug in the my code. In the end, I found that I get NaN when I do a simple calculation. I took a a substract of it and run alone and I still find NaN. Coulod anyone explain the source of the error. I tried to use a calculator and it is working.

The code is attached.

Thanks

0 Kudos
Message 1 of 9
(4,128 Views)

I would say you can't take the log of a negative number.  LabVIEW uses a log function to do the exponent calculation, but log is only defined for positive numbers.  This will fail for all negative numbers.

 

Bruce

Bruce Ammons
Ammons Engineering
Message 2 of 9
(4,113 Views)

Hello,

In this case, what should I do to calculate (-2009.05)**0.217 in Labview?

The simple calculator of Windows can do it.

Cheers,

Zied

0 Kudos
Message 3 of 9
(4,104 Views)

If that's your typical exponent, you can use some trivial math to get around this.

 

Rather than feeding in the negative value, send in the absolute value.  This will give you 2009.05 in this case.  Once you perform the calculation, you can use negate to give it back the correct sign.  Depending on your input, you may need to add a little logic in the front to send positive values through cleanly and negative as their postive counterparts.  But, it'll let you perform the math you're looking for.

 

I've looked for other power options using a negative x and they all result with the same NaN.  It's possible someone else will be able to find a more elegant solution.  But, I did test this one out to see that it works.  As you were hardcoding the exponent, I'd expect this to work.  Obviously, if you were going to get into something like a square you'd have to do something a bit more tricky to determine which sign should apply. 

Message 4 of 9
(4,095 Views)

Thank you guys for your help. But it seems still complicated. I tried to avoid mathscript to avoid later exportation to exe problems but it gets worse as I got a complex number: 2.45178 +1.98966 i

 

Zied

0 Kudos
Message 5 of 9
(4,091 Views)

You get a complex number because that is the only correct solution to the equation.  You can't take a root of a negative number without getting a complex component.  Even with integer roots, you will get complex numbers.  You can't raise a negative number to a non-integer power, either.  Why do you have a negative number to begin with?  I suspect you have an error when you derived your equation.

 

Bruce

Bruce Ammons
Ammons Engineering
0 Kudos
Message 6 of 9
(4,083 Views)
Hello, this exponent is a part of a function Is the result I get from windows calculators is wrong then?
0 Kudos
Message 7 of 9
(4,075 Views)

@ziedhosni wrote:
Hello, this exponent is a part of a function Is the result I get from windows calculators is wrong then?

Windows 7's calculator gives me "invalid input" when I try your values...

Message 8 of 9
(4,071 Views)

I think you are right. The solution can be only a complex number.

Cheers,

Zied

0 Kudos
Message 9 of 9
(4,029 Views)