What does this block of code do? (u> 0) - (u <0)

if (abs(u) > Vdc)
    u = Vdc*((u > 0) - (u < 0));

This code is in C given that we introduce an if condition, what will happen? Vdc = 24; consider any arbitrary value and to explain

+4
source share
4 answers

If the u > 0statement becomes 1 - 0(true - false) = 1. If it u < 0does -1. If it is zero, it will also become 0. Thus, basically it returns a "sign" u(more precisely, 1with the corresponding sign). A common code snippet is for fixing ubetween +Vdcand -Vdc. (As suggested, it will only work for the positive Vdc).

+7

. u > 0 ,

(u > 0) - (u < 0) -> 1 - 0 -> 1

, - false. u < 0.

+5

    |0,  if u = 0
 f= |1,  if u > 0
    |-1, if u < 0

if

//For positive values of u 
(u>0) - (u<0) = 1 - 0 = 1
//For negative values of u
(u>0) - (u<0) = 0 - 1 = -1
//For u = 0
(u>0) - (u<0) = 0 - 0 = 0
+2

Sign

  • 1, u > 0
  • 0, u = 0
  • -1, u < 0

:

C 6.5.8

1, 0, . int.

, u 0, u > 0 1 u < 0 0. 1-0 1, . u 0, 1. , u 0 -1.

+2

All Articles