Base Conversion in C | Check WolframAlpha Results

  • Thread starter epr2008
  • Start date
  • Tags
    Base
In summary, the conversation discusses a program that converts numbers to different bases, including non-integers. The program has been tested against WolframAlpha and works for some bases, but has trouble with others. The code has been tested for bases larger than 1.5 and up to 6 digits, but there are some exceptions. The program also has a difference in output compared to WolframAlpha, with the latter trying to get the highest possible digits in the highest possible places. The conversation ends with the speaker expressing the need for fresh eyes to help with the algorithm.
  • #1
epr2008
44
0
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.

Code:
#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;
}
 
Technology news on Phys.org
  • #2
Code:
if(j != 0)
	{
		for(k = j; j > 0; j--)
		{
			printf("0");
		}
	}

sorry, I'm not that familiar with the subtleties of C ... why have you got j>0;j-- in what appears to be the k loop?

Can you give a couple of examples of where your code differs from WolframAlpha?
 
  • #3
Sorry about that, I was trying to format the code in the physics forum box and add comments and I guess I messed that loop up. But here are some examples:

10 in base 3.14159:
My code: [tex](x)_{b} = (10.000000)_{3.14159} = 30.121201...[/tex]

Wolfram: [tex]100.01022123001..._{3.14159} [/tex]1230293 in base 3.10293

My code: [tex](x)_{b} = (1230293.000000)_{3.10293} = 1120103012012.122122...[/tex]

Wolfram: [tex]1.1201100020201..._{3.10293}×3.10293^{12}[/tex]

The last one really shows what I was talking about the digits are the same until the 3 in mine which you notice is the floor of the base while theirs is a 1 in the preceding digits place and 0 where I have a 3, I pointed the floor of the base thing out because all we are really doing is modular arithmetic. And I am guessing it has something to do with that. You will notice that after that mine differs from theirs completely. In the first one this happens at the first digit, so the entire string is different.
 
Last edited:
  • #4
I was thinking that it may actually be machine error.
 
  • #5
I figured that I would post a few where they are the same also:

3623847 in base 39:

My code: [tex](x)_{b} = (3623847.000000)_{39} = 1223216.000...[/tex]

Wolfram: [tex]1223216_{39}[/tex]

10 in base 4.16273

My code: [tex](x)_{b} = (10.000000)_{4.16273} = 21.231202...[/tex]

Wolfram: [tex]21.2312021013..._{4.16273}[/tex]

10 in base 2.718281828:

My code: [tex](x)_{b} = (10.000000)_{2.718282} = 102.1120101...[/tex]

Wolfram: [tex]102.1120101111..._{2.71828}[/tex]
 
Last edited:
  • #6
epr2008 said:
I was thinking that it may actually be machine error.

They are both proper representations of the numbers involved.
The difference is that Wolfram tries to get the highest possible digits in the highest possible places and apparently your algorithm does not.

Note that if the base is a whole number, the representation is unique.
But if the base is a fractional number, there is more than 1 representation.

You can verify this by multiplying out the numbers.
 
  • #7
Oh thank you! I did not know that. Is there somewhere that I can read more about this?
 
  • #8
Sorry, I don't really know.
I've learned about it in old books that you won't have available.
So what's left is googling for it.
 
  • #9
I'll do that when I have some free time. Thanks for the information though. It's always a good feeling when you get a program right the first time through :)
 

What is base conversion in C?

Base conversion in C refers to the process of converting a number from one base system to another. This is commonly done in computer programming to represent numbers in different formats, such as binary, decimal, or hexadecimal.

How do I perform base conversion in C?

To perform base conversion in C, you can use the built-in functions itoa() or atoi() to convert between string and integer representations. You can also use bitwise operations and mathematical operations to manually convert between bases.

What are some common uses for base conversion in C?

Base conversion in C is commonly used in computer programming for tasks such as data encoding and decoding, data compression, and cryptography. It can also be used to convert numbers to different units of measurement or to represent numbers in a more compact format.

What are some potential errors when performing base conversion in C?

Some potential errors when performing base conversion in C include overflow or underflow of integer values, incorrect input or output formats, and loss of precision when converting between bases with different number of digits.

Are there any tools available to assist with base conversion in C?

Yes, there are several tools available to assist with base conversion in C, such as the strtol() and strtoul() functions for converting strings to long integers, and the printf() function for formatting output in different bases. Additionally, there are online resources and libraries that can help with more complicated base conversion tasks.

Similar threads

  • Programming and Computer Science
Replies
4
Views
896
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
1
Views
945
  • Programming and Computer Science
2
Replies
47
Views
4K
  • Programming and Computer Science
Replies
4
Views
602
  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
Replies
17
Views
1K
Back
Top