1

I have integer num, which can be negative or positive and also I have number P (1 < P <= 9).

What I want to do is to convert num to base-P.

I easily do it when num > 0:

int i = 0;
int A[10000];
while (num != 0)
{
    A[i] = num % p;
    num /= p;
    i++;
}

But I don't know how to achieve it when num < 0.

Mat
  • 202,337
  • 40
  • 393
  • 406
AYETY
  • 698
  • 4
  • 23
  • 38
  • Well i think the question is not duplicate to the one that is referred to, since in representing any number base p, the main problem lies in how you are saying that the negative number is defined in base-p system. – Rakholiya Jenish Jun 07 '15 at 12:55
  • My question is totally different from the one that @πάντα marked – AYETY Jun 07 '15 at 12:58
  • Well, your question is different, but the answers there should answer your problems as well. The sign of modulo operations is implementation defined. – πάντα ῥεῖ Jun 07 '15 at 13:00
  • Just add "negative" flag to it, unless you really really want to store `-1` in base 4 as "333333333333333333333333333333333..." (the way computer does it with binary numbers). – SigTerm Jun 07 '15 at 13:16

2 Answers2

3

Check if number is positive or negative, and remember this in the code. If it's negative - multiply the number by -1 and do the same you did to a positive number. When you output the number, you should do this before:

if (sign == -1)
    cout << '-';

And then write the rest of the number.

Adrian Jałoszewski
  • 1,695
  • 3
  • 17
  • 33
  • As I can observe, the sign should be stored before the operation start. but, when operating the num it doesn't have to be in positive the condition still holds and the % still give positive number even with negative numbers. +1 for you – hasan Jun 07 '15 at 13:25
  • yes, when number is negative we should multiply it by -1, but then the whole logic of conversion should be changed. for example if we want to convert negative decimal to binary we should do the same: let say we have -48. 48 = 110000 -48 = 001111 + 000001 = 1010000 – AYETY Jun 07 '15 at 13:26
  • 2
    @hasan83: No, [`-7%3 == -1`](http://stackoverflow.com/questions/7594508/modulo-operator-with-negative-values) so it will in fact give a negative number. – MSalters Jun 07 '15 at 13:32
  • I think you are right. my assumption does not work for all numbers! – hasan Jun 07 '15 at 13:34
  • I think this is answer is the only solution. no other solutions even for fun. right ppl? – hasan Jun 07 '15 at 13:35
1

The - sign is not something special to base 10. In general if 90base{10} = 1011010base{2} then -90base{10} = -1011010base{2}. What you are doing here is an extension of two's complement. Two's complement is a method to represent both positive and negative numbers in binary without using a - and is used in computing. Just check whether number is positive or negative and put negative sign. You can use for checking Adrian's answer. But, you can also use the function code that returns -1 for negative values, 0 for zero, 1 for positive values of x

int status (int x) {
  int sign = (x > 0) - (x < 0); 
  return sign;
}
NewCoder
  • 183
  • 1
  • 14