logo elektroda
logo elektroda
X
logo elektroda

[C]Converting Negative Integers to Positive in C: Using Absolute Value (abs) Function

Marcin_xx1 15276 9
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 6195837
    Marcin_xx1
    Level 21  
    Hello, I am asking for help because I can't figure out how to reverse the sign of an int number.
    example : int variable =-50;
  • ADVERTISEMENT
  • ADVERTISEMENT
  • #3 6195875
    Marcin_xx1
    Level 21  
    Thanks !!
  • ADVERTISEMENT
  • #4 6195919
    elektryk
    Level 42  
    Multiplying by -1 can be very time consuming on some architectures.
    The easiest way is to save:
    variable=-variable
  • #5 6196113
    ed-ek
    Level 34  
    If the variable int a; can take positive or negative values, using a=-a is an error. There is an abs() function and its use is a must.
    a=abs(a);
  • ADVERTISEMENT
  • #6 6196300
    elektryk
    Level 42  
    ed-ek wrote:
    If the variable int a; can take positive or negative values, using a=-a is an error. There is an abs() function and its use is a must.
    a=abs(a);
    Why a mistake? The question was about reversing the sign, not calculating the absolute value.
  • #7 6196322
    Manif
    Level 12  
    Assuming that we only have negative numbers as input - you're right.
    However, assuming the value is arbitrary... reversing the sign is a bad idea.

    It's best to let the author specify, because now we can only speculate. :)
  • #8 6198232
    Marcin_xx1
    Level 21  
    The variable has different values, sometimes positive and sometimes negative.
    This is the ADC support in the Atmega 88, when connecting the lm 335 temperature sensor, I have to convert the Kelvin scale to Celsius for this it subtracts the number 273
    I wanted to have the result with tenths, but when the measurement variable is declared as float or double, it eats up too much space in the code, maybe there is another solution?
    [code:1:da9ffbb86a]void pomiar_temp(){

    int pomiar=0;

    ADCSRA |= _BV(ADSC); // Rozpoczęcie przetwarzania
    while(bit_is_set(ADCSRA,ADSC)){}; // Oczekiwanie na zakończenie przetwarzania

    //1023 /(100*Uodniesienia(5V)) = 2,046;

    pomiar= ADC/2.046;
    pomiar=pomiar-273;


    if(pomiar
  • #9 6200199
    elektryk
    Level 42  
    Marcin_xx1 wrote:
    I wanted to have the result with tenths, but when the measurement variable is declared as float or double, it eats up too much space in the code, maybe there is another solution?
    Then keep int tenths of degrees instead of units and only convert before displaying.
  • #10 6200859
    Marcin_xx1
    Level 21  
    Thanks, but I will give up these tenths because the result will jump every half a degree and at
    the accuracy of this sensor is not so important anymore.

Topic summary

The discussion centers on converting negative integers to positive in C programming. The primary method suggested is multiplying the integer by -1 or using the expression `variable = -variable`. However, there are concerns about performance on certain architectures. An alternative approach mentioned is using the `abs()` function, which calculates the absolute value. The author later clarifies that the variable can hold both positive and negative values, particularly in the context of ADC support for the Atmega 88 microcontroller when interfacing with an LM 335 temperature sensor. The author seeks to convert Kelvin to Celsius while minimizing memory usage, ultimately deciding against using tenths of degrees due to sensor accuracy considerations.
Summary generated by the language model.
ADVERTISEMENT