Pergunta

I am reading about positive and negative infinity in c++ and i am trying to implement them in a fixed point math arthimethic implementation

i can see that max of a int will be equal to std::numeric_limits<int>::max(); in c++

and min value of the int will be equal to std::numeric_limits<int>::min(); in c++

Here as i am defining the int max and int min manually in my fixed point math implemenation, my doubt is int min = -int max; or int min = -int max -1; ?

can someone clear it?

Foi útil?

Solução

It depends on your sign representation, but I will assume it is two's complement.

In that case, we have -x = ~x + 1 so x + (~x) = -1 for every signed integer x.

So int min = (~max) + -1 = - ( (unsigned int) max) + -1

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top