I have been working on coding a program that works for all bases, including non-integers. I have been checking it against wolframalpha, and so far, my code works for a decent amount of bases, but at the same time has trouble with others... For instance it seems to have a tolerance in the magnitude and the digit length of the bases. So, far I have tested and it seems to work for bases larger than 1.5 (I have tested it up to 64 so far) and up to 6 digit bases(6 including the digits after the decimal) with some exceptions. I have noticed that for the decimal bases that my program computes that don't match wolfram, the string will match until a certain digits place, say k, and at k, k-1 wolfram will have a 10 respectively, while my computed value will have the floor of the base in the k-1 place. I figured that it may be that my algorithm wasn't finding the highest power divisor right, but then I realized that would not make since. So, I was hoping that fresh eyes may be able to help me out with it... By the way, this is just a rough draft, but the procedure should be fairly simple to follow.(adsbygoogle = window.adsbygoogle || []).push({});

Code (Text):

#include <stdio.h>

#include <math.h>

void convert_base(double x, double b)

{

double y, z;

int digit, i, j = 0, k;

/* Find the terms in power series expansion with base power > 0 */

z = x;

while(z >= 1)

{

/* Compute largest power of base which divides x giving a value >= b*/

y = z;

digit = y;

i = 0;

while(digit >= b)

{

y = y / b;

digit = y;

i++;

//printf("y = %lf\n", y);

}

//printf("i = %d\n", i);

//printf("digit = %d\n", digit);

/* Subtract off this term in series expansion */

z = z - (digit * pow(b, i));

//printf("z = %lf\n", z);

/* Fill in digits in between the last calculated digit and this calculated digit */

if((abs(i - j) > 1) && (j != 0))

{

for(k = 1; k < abs(i - j); k++)

{

printf("0");

}

}

printf("%d", digit);

j = i;

}

if(j != 0)

{

for(k = j; j > 0; j--)

{

printf("0");

}

}

printf(".");

/* Find the terms in the power series expansion with base power < 0 */

while((z > 0) && (z <= 1) && (i > -6))

{

/* Compute smallest power of base which multiplies x giving a value >1 */

y = z;

digit = y;

i = 0;

while(digit < 1)

{

y = y * b;

digit = y;

i--;

//printf("y = %lf\n", y);

}

//printf("i = %d\n", i);

//printf("digit = %d\n", digit);

/* Subtract off this term in series expansion */

z = z - (digit * pow(b, i));

//printf("z = %lf\n", z);

/* Fill in digits in between the last calculated digit and this calculated digit */

if(abs(i - j) > 1)

{

for(k = 1; k < abs(i - j); k++)

{

printf("0");

}

}

printf("%d", digit);

j = i;

}

if(z == 0)

{

printf("000");

}

printf("...\n");

}

int main(void)

{

double x, b;

printf("Enter a number: x = ");

scanf("%lf", &x);

printf("Enter the base for conversion: b = ");

scanf("%lf", &b);

printf("(x)_b = %lf = ", x);

convert_base(x, b);

return 0;

}

**Physics Forums | Science Articles, Homework Help, Discussion**

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Base conversion in C

**Physics Forums | Science Articles, Homework Help, Discussion**