Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this code block doing ? (u > 0) - (u < 0)

Tags:

c

ansi-c

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

like image 390
Dhruv Avatar asked Dec 04 '22 03:12

Dhruv


1 Answers

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).

like image 88
Eugene Sh. Avatar answered Dec 25 '22 03:12

Eugene Sh.