pow() function error

Hi,

i have this sinppit of code to get all the individual digits in a number.

1
2
3
4
5
float tempNumber = 011010
for (short i = 6; i > 0; i--)
{
  cout << (static_cast<long>(tempNumber) % static_cast<long>(pow(10, i))) / static_cast<long>(pow(10, i-1)) << endl;
}


when i run this code i get an output of 0,1,1,0,2,0 why is it?

when i output just static_cast<long>(pow(10, i-1)) sometimes i get 99 instead of 100, why is this?

Nat
An exercise for you:

Step through the code with a debbuger, with a watch list on your variables - see how they change line by line. Split the code into 3 lines, to make it easier to see what is going on. You have a % b / c, put a b c on their own lines.

Hint: It could be to do with floating point representation.

It's interesting tempNumber looks like a binary number (or is that coincidence)- does it have to be float?
Last edited on
OK when i split it apart like so:

1
2
3
4
5
6
7
8
9
10
11

for (short i = 6; i > 0; i--)
	{
			long a = 11010;
			long b = static_cast<long>(pow(10, i));
			long c = static_cast<long>(pow(10, i-1));
			cout << a << endl << b << endl << c << endl;
			cout << a % b << endl;
			cout << (a % b)/c << endl << endl;
	}


Things work like a charm. I have no idea how to use a debugger, i have only used c++ for a few days now. I am a long time Java programmer though. you would be correct, i am writing a program to convert a base 2 decimal eg. 0.011010 into base ten eg. 0.42 the user inputs floats and that is why it is in a float type, i multiplied the inputted decimal in previous code so that i could do the % operator and get each individual digit.
Not all floating point values can be stored exactly so rounding errors happens easily. If you want something exact you shouldn't use floating point numbers.

A number literal starting with 0 is an octal number. 011010 is the same as 4616.
Good God, man. Don't use pow() for this.

I'm not entirely sure what it is you're doing... but....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// -- BAD --
for( int i = 6; i > 0; --i )
{
  int a = 11010;
  int b = a / pow(10,i);
  cout << b;
}

// -- BETTER --
for(int i = 1000000; i > 0; i /= 10)
{
  int a = 11010;
  int b = a/i;
  cout << b;
}



EDIT:

As for extracting individual digits from a number (is that what you're doing?):

1
2
3
4
5
int num = 11010;
for(int digit = 1000000; digit > 0; digit /= 10)
{
  cout << ((num / digit) % 10);
}
Last edited on
Peter87 wrote:
A number literal starting with 0 is an octal number. 011010 is the same as 4616.


Good Work Peter !! Eagle Eye. I am guessing a lot of people would never have seen that.

nat45928 wrote:
I have no idea how to use a debugger, i have only used c++ for a few days now. I am a long time Java programmer though.


Surely the process is similar, no matter which language you use. Maybe you are compiling / make from the cmd line? If that is so is a bit more tricky. I am sure you can find some tutorials on the web. There is a gdb one on this site.

Topic archived. No new replies allowed.