annuler
Affichage des résultats de 
Rechercher plutôt 
Vouliez-vous dire : 

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 Compliments
Message 1 sur 9
5 375 Visites

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 sur 9
5 360 Visites

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 Compliments
Message 3 sur 9
5 351 Visites

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 sur 9
5 342 Visites

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 Compliments
Message 5 sur 9
5 338 Visites

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 Compliments
Message 6 sur 9
5 330 Visites
Hello, this exponent is a part of a function Is the result I get from windows calculators is wrong then?
0 Compliments
Message 7 sur 9
5 322 Visites

@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 sur 9
5 318 Visites

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

Cheers,

Zied

0 Compliments
Message 9 sur 9
5 276 Visites