Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <stdlib.h>
- #include <stdio.h>
- #define ipow(n, e) (int)pow(n, e)
- #define buf_update(n) printf("buf: %d\n", n)
- void _connegbase(int n, int r, int e)
- {
- /*
- * Gives 1 1 8 70 1 for digit values which is obviously wrong
- * for -124 converting to base -10
- */
- /* Takes the number to be converted n, and the base to convert to r */
- int m;
- /* Find minimum multiplicator of |r^e| that is > |n| */
- for (m = 0; m * abs(ipow(r, e)) < abs(n); ++m) {
- ;
- }
- buf_update(m);
- if (e && n) {
- /* | m * r^e - |n| | passed as n */
- _connegbase(n - (m * ipow(r, e)), r, e-1);
- }
- return;
- }
- void
- connegbase(int n, int r)
- {
- int _exp = -1;
- int _pow;
- int _powpos = 0;
- int _npos = 0;
- if ( r < 0 ) {
- do {
- _pow = ipow(r, ++_exp);
- _powpos = (_pow>=0)?1:0;
- _npos = (n>=0)?1:0;
- if ( abs(_pow) > abs(n) ) {
- if ((!_powpos && !_npos) || (_powpos && _npos)){
- break;
- } else if ((_powpos && !_npos) || (!_powpos && _npos) ) {
- _exp--;
- break;
- }
- }
- } while (1);
- _connegbase(n, r, _exp);
- } // r < 0
- }
- int
- main(int argc, char **argv)
- {
- printf("<\n");
- connegbase(1240, -10);
- printf("<\n");
- connegbase(124, -10);
- printf("<\n");
- connegbase(-1240, -10);
- printf("<\n");
- connegbase(-124, -10);
- printf("<\n");
- connegbase(0,-10);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement