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.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

What wrong w/ this formula node

The output is always -2,-2,-2... for the formula code below. I was expecting -2,-3.-4 for each iteration in a For loop.
 
The formula below is enclosed in a For loop, i is the loop counter and S=2.
 
I don't see why this order should matter Please confirm. The syntax help for formula node is crude.
 
int16 x;
if (s=1) x=-2;
 else
 x = -1*(s+i);
 
If I reverse the if else to then all is OK.
 
int16 x;
if (s>1) x = -1*(s+i);
else
(s=1) x=-2;
 
0 Kudos
Message 1 of 10
(3,561 Views)
Is "if (s=1)" a valid equality test? I was thinking more along the lines of "if (s==1)", but then again I have never used a formula node.
~~~~~~~~~~~~~~~~~~~~~~~~~~
"It’s the questions that drive us.”
~~~~~~~~~~~~~~~~~~~~~~~~~~
0 Kudos
Message 2 of 10
(3,550 Views)
Yep, you beat me.  You are using asignment = instead of evaluation == .

Hope that this helps,
Bob Young

0 Kudos
Message 3 of 10
(3,544 Views)
Your comparsion of 's=1' is incorrect. The single '=' is an assignment. The valid equal comparison is '==' (double equal sign). This is basic C syntax.
0 Kudos
Message 4 of 10
(3,542 Views)
Thanks, the equality operator solved the issue. This must be quite as common as in writing C code. The LV help on Formula Nodes needs TLC, I'm using LV 7.1.
0 Kudos
Message 5 of 10
(3,538 Views)
Well, to be fair it is not NI's strict responsibility to provide a C tutorial in the help for Formula Nodes.  I think that they are included so that it is easier to integrate already written code, and possibly to do things LV cannot do on its own (if there are indeed things C can do but LV cannot). 

I'm sure they'd much prefer to have extensive help on LV itself, and have you use LV to write your program, not C.

Just my 2 cents
0 Kudos
Message 6 of 10
(3,529 Views)
I'm looking for consistency when going to help in LV. I'm doing LV also not C. A tutorial on C is not the idea, its far from it. This paste out of the help was a total waste the effort.
 
"The Formula Node syntax is summarized below using Backus-Naur Form (BNF notation). "
 
First who and what is Backus-Naur and just give me how the formula node syntax is typed. The big advantage is just that, i.e.  reading text not 20 or more arithmetic nodes.
 
Rich J
0 Kudos
Message 7 of 10
(3,526 Views)
Oops, my apologies. It's been a long day.  For some reason "Formula Node" = "Code Interface Node" in my head.  I apologize for the oversight.  I agree with your points. Here's some info on Backus-Naur Form if you're interested.
0 Kudos
Message 8 of 10
(3,513 Views)

This page from help helps somewhat, was found by searching formula, hope it helpshappy smiley

 

Precedence of Operators in Formula Nodes and Expression Nodes

The precedence of operators is as follows, from highest to lowest. Operators on the same line all have the same precedence.

** exponentiation
+, -, !, ~, ++, and –– unary plus, unary negation, logical not, bit complement, pre- and post-increment, pre- and post-decrement

++ and –– are not available in Expression Nodes.
*, /, % multiplication, division, modulus (remainder)
+ and addition and subtraction
>> and << arithmetic shift right and shift left
>, <, >=, and <= greater, less, greater or equal, and less or equal
!= and == inequality and equality
& bit and
^ bit exclusive or
| bit or
&& logical and
|| logical or
? : conditional evaluation
= op= assignment, shortcut operate and assign

op can be +, , *, /, >>, <<, &, ^, |, %, or **.

= op= is not available in Expression Nodes.
Note  In versions of LabVIEW earlier than 6.0, the ^ operator represented exponentiation. The ^ operator now represents the bitwise exclusive or (XOR) operation. The new operator for exponentiation is **, for example, x**y.

The assignment operator = is right associative (groups right to left), as is the exponentiation operator **. All other binary operators are left associative.

The numeric value of TRUE is 1, and FALSE is 0 for output. The logical value of 0 is FALSE, and any nonzero number is TRUE. The logical value of the conditional expression

<lexpr> ? <texpr>: <fexpr>

is <texpr> if the logical value of <lexpr> is TRUE and <fexpr> otherwise.

~~~~~~~~~~~~~~~~~~~~~~~~~~
"It’s the questions that drive us.”
~~~~~~~~~~~~~~~~~~~~~~~~~~
0 Kudos
Message 9 of 10
(3,506 Views)
try

if (s==1) x;-2; note the double == for a conditional, otherwise you
set s=1.
Hmmm, you also need to change the else condition to either
-1*(-s+i) or just s-i to get the result you want.

TJW


On Wed, 31 May 2006 14:40:10 -0500 (CDT), richjoh <x@no.email> wrote:

>The output is always -2,-2,-2... for the formula code below. I was expecting -2,-3.-4 for each iteration in a For loop.
>&nbsp;
>The&nbsp;formula below&nbsp;is enclosed in a For loop, i is the loop counter and&nbsp;S=2.
>&nbsp;
>I don't see why this order should matter&nbsp;Please confirm. The syntax help for formula node is crude.
>&nbsp;
>int16 x;if (s=1) x=-2;&nbsp;else&nbsp;x = -1*(s+i);
>&nbsp;
>If I reverse the if else to then all is OK.
>&nbsp;
>
>int16 x;if (s&gt;1) x = -1*(s+i);
>else
>(s=1) x=-2;&nbsp;

0 Kudos
Message 10 of 10
(3,495 Views)