if (abs(u) > Vdc)
u = Vdc*((u > 0) - (u < 0));
This code is in C considering we enter the if condition what will happen ? Vdc = 24; consider any arbitrary value of u for an explanation
if (abs(u) > Vdc)
u = Vdc*((u > 0) - (u < 0));
This code is in C considering we enter the if condition what will happen ? Vdc = 24; consider any arbitrary value of u for an explanation
If u > 0
the statement will become 1 - 0
(true - false) = 1
. If u < 0
it will become -1
. If it is zero, it will become 0
as well. So basically it is returning the "sign" of u
(or more precisely 1
with corresponding sign). The overall code snippet is for clamping u
between +Vdc
and -Vdc
. (As suggested, it will work only for positive Vdc
).
The expression in parentheses is the sign function.
If u > 0
holds, the expression becomes
(u > 0) - (u < 0) -> 1 - 0 -> 1
because the first condition is true and the second is false.
Same for the u < 0
case.
This is a technique to model the function
|0, if u = 0
f= |1, if u > 0
|-1, if u < 0
It avoids using an if clause to do this comparison, and evaluates like so
//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
This is mathematical function Sign it's value is
this is how it works:
as per C standard section 6.5.8 relational operators
shall yield 1 if the specified relation is true and 0 if it is false. The result has type int.
now if u is greater than 0 then u > 0
returns 1 and u < 0
returns 0. 1-0
is 1, e.g. any u
greater than 0 converted to 1. Similarly, any u
less than 0 is converted into -1.