Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isn't there an operator in c to change the sign of a int float etc from negative to positive or vice versa?

trying to find absolute value and i thought there was a simple way to just invert the sign with '~' or something.

like image 275
nickthedude Avatar asked Jan 08 '10 05:01

nickthedude


People also ask

What does << mean in C?

<< is the left shift operator. It is shifting the number 1 to the left 0 bits, which is equivalent to the number 1 .

How do I remove negative sign in CPP?

Abs. If you want to remove the sign from a string, you can use math. Abs, or String.

What is the letter for float in C?

The short answer is: for a numerical literal with a decimal point, a suffix of f or F will tell the compiler your numerical literal should be taken as a float number, instead of as a double ; it will be taken as a long double if the suffix is l or L .

Can we use with float and int in C?

Of course you can add an int to float . C will convert the int to float before adding. However, in the expression (float)15/2 + 15/2 , the first 15/2 will be calculated as float and the second 15/2 will be an integer division first, and that result converted to float. So you'll be adding 7.5+7.


1 Answers

float newValue = oldValue * -1;

or

float newValue = -(oldValue); //() aren't needed, I just use them out of habit
like image 189
Beep beep Avatar answered Sep 17 '22 02:09

Beep beep