Advertisement
Guest User

Negative base function

a guest
Feb 20th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.43 KB | None | 0 0
  1. /* The ipow() function is a function I wrote to return the power of an integer without needing a cast
  2.  * The buf_update() function puts the generated digits into a buffer and keeps track of the pointers
  3.  * for said buffer. Getting the input from the terminal and getting the values back from the buffer
  4.  * are handled with separate functions that don't have much to do with this code and that I know work.
  5.  * The functions that have declarations here are the only ones this function depends on         */
  6.  
  7. void connegbase(int n, int r)
  8. {
  9.     /*
  10.      * Gives 1  1  8  70  1 for digit values which is obviously wrong
  11.      * for -124 converting to base -10
  12.      */
  13.    
  14.     /* Takes the number to be converted n, and the base to convert to r */
  15.     static int  e = 0, e_flg = false;
  16.     int     m;
  17.    
  18.     /* These functions are not the problem */
  19.     int     ipow(int n, int e);
  20.     void    buf_update(int n);
  21.    
  22.     /* Find the minimum power of r whose absolute value is greater than |n| */
  23.     while ( !e_flg &&
  24.         abs(ipow(r, e++)) < abs(n)) {
  25.     ;
  26.     }
  27.     e_flg = true;   /* So above loop doesn't execute again */
  28.    
  29.     /* Find minimum multiplicator of |r^e| that is > |n| */
  30.     for (m = 0; m * abs(ipow(r, e)) < abs(n); ++m) {
  31.     ;
  32.     }
  33.     buf_update(m);
  34.     if (e && n) {
  35.     /* | m * r^e - |n| | passed as n */
  36.     connegbase(abs(m * ipow(r, --e)) - abs(n), r);
  37.     }
  38.     e_flg = false;  /* Reset for next invocation */
  39.     return;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement