0

In my code, I have command line arguments, and they are being passed correctly. I am converting a base 2-10 number into base 10 using coefficient base exponent form. it works on many numbers, but many others don't work. It is noticeable with you convert base 10 numbers, 3 digits, to base 10.

This is my function:

int decimal(int inbase, int dig, int argc, char *argv[])
{

    int total = 0, place = dig - 1;

    for(int i = 0; i < dig; i++)
    {
        total = total + (argv[2][place] - '0')*pow(inbase, i);
        cout<<"Argv: "<<argv[2][place] - '0'<<endl;
        cout<<"Power: "<<pow(inbase, i)<<endl;
        cout<<"Total After: "<<total<<endl;
        place--;
    }

    return total;
}

Argv[2] is being passed as a character sting, an example input at compiling this program is: ./a.exe 10 100 10 Expected output in base 10: 100 What I get: 99

  • Show the actual input to that function and the function's actual output. As the quest ion is currently written, nobody can reproduce your results without guessing. – Pete Becker Feb 15 '20 at 17:31
  • What is the output of your `cout` statements during function execution? Is the `total`, `power`, and `argv` outputs correct on each iteration? – JohnFilleau Feb 15 '20 at 17:34
  • You'll surprised to learn that, for example, `pow(10,2)` is not 100, because floating point math is broken, see the linked question for more information, and the shown code requires it to be exact, else truncation occurs when converting the inexact floating point result to an integer. – Sam Varshavchik Feb 15 '20 at 17:37

1 Answers1

0

C++ does not have pow for integers: https://en.cppreference.com/w/cpp/numeric/math/pow

You are probably losing data somewhere in the midst of all the floating point / integer conversions.

Also, how about simply:

int total = 0;
for (int i = 0; i < len; ++i) {
    total = total * base + arr[i];
}

or, with the variables you have:

int total = 0;
for (int i = 0; i < dig; ++i) {
    total = total * inbase + int(argv[2][i] - '0');
}
DeducibleSteak
  • 1,398
  • 11
  • 23
  • Floating point numbers are approximations, sometimes the approximation is a little bigger than the actual value, sometimes it's a little smaller. Implicit conversion from float or double to int uses truncation. So if the float is a little smaller than the number you are trying to represent (pow(10,2) in this case) it gets truncated to 99. You could still use pow(), but make sure that the conversion rounds instead of truncates by adding 0.5 `total = total + (argv[2][place] - '0')*pow(inbase, i) + 0.5;` – DaveB Feb 15 '20 at 17:50
  • What would the correct math be if I was to go from base 2 to 36 in that sense? Assuming 10-36 would be 'A' - 'Z', I know I need to add 10, but instead of '0', what would I put? – Eian Knudsen Feb 15 '20 at 17:58
  • The same. Just figure out how to convert 'A'-'Z' to 10-36, the same way you convert '0'-'9' to 0-9. – DeducibleSteak Feb 15 '20 at 18:01
  • I got it, instead of '0' I use '7' because there are 7 characters between the numbers and the capital letters. – Eian Knudsen Feb 15 '20 at 18:29
  • Yes, or, you can do: i = c - 'A' + 10; – DeducibleSteak Feb 15 '20 at 18:30