07-06-2016 10:06 AM
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
07-06-2016 09:16 PM
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
07-07-2016 03:49 AM
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
07-07-2016 07:15 AM
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.
07-07-2016 07:58 AM
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
07-07-2016 09:56 AM
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
07-07-2016 04:30 PM
07-07-2016 05:57 PM
@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...
07-08-2016 08:11 AM
I think you are right. The solution can be only a complex number.
Cheers,
Zied