-3

I have been trying to implement something in C++ but apparently, there's a syntax error.

  1. The following code yields "1 3100" when 31 is entered as input :

    #include<iostream>
    #include<cmath>
    using namespace std;
    int main()
    {
        long long n; cin>>n;
        long long j = floor((log10(n)));
        long long nn = (n*((long long)pow(10,j+1)))+n;
        cout<<j<<" "<<nn;
    }
    
  2. The following code yields "1 3130" for the same input, i.e, 31 :

    #include<iostream>
    #include<cmath>
    using namespace std;
    int main()
    {
        long long n; cin>>n;
        long long j = floor((log10(n)));
        long long nn = (n*(pow(10,j+1)))+n;
        cout<<j<<" "<<nn;
    }
    

And I wished to produced "1 3131" for the input 31. Basically, I am trying to write the number twice in a row: the same thing that you get when you parse the number into string and add the same string twice (like, n=11, parse into s = "11" and then yield s+s).
So I want to multiply the input by a suitable power of ten to get enough "trailing zeros" and then add the input again.

Where am I going wrong? Also, why is there a difference between the two codes above? (Please explain why the first code gives that as an output and the second code that as an output and also help me with a newer code to get the desired output).

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • Where is the syntax error? –  Mar 02 '18 at 16:13
  • If you want to write out 3131 when you enter 31 why not just use `std::cout << n << n;`? – NathanOliver Mar 02 '18 at 16:14
  • Is there a syntax error or doesn't you program do what you expect.Please fix the question. – harper Mar 02 '18 at 16:15
  • There is no syntax error. You are misusing pow() for int, which looses 1 in conversion. I.e. your sum ends up being 3099+31 instead of 3100+31. – Yunnosch Mar 02 '18 at 16:18
  • `pow(10,j+1)` returns a value of type `double`; in the first code snippet, the cast in the calculation of `nn` converts that to `long long`, and all the rest of that calculation is done with `long long`. In the second, there is no cast, and all the rest of that calculation is done with `double`. Different isn't the same. – Pete Becker Mar 02 '18 at 16:18
  • 2
    [Cannot reproduce](https://ideone.com/CR0T2H) – Killzone Kid Mar 02 '18 at 16:21
  • How can you run a program that has syntax errors? A syntax error stops compilation. – Thomas Matthews Mar 02 '18 at 16:33
  • Oh my gosh! (-5)? Anyway, thank you @above,below –  Mar 02 '18 at 16:36
  • @KillzoneKid Your code doesn't seem correct. Please have a look here : https://imgur.com/WrlfsSL I have executed it and the result isn't something –  Mar 02 '18 at 16:49
  • Handy reading: [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – user4581301 Mar 02 '18 at 17:02
  • Unrelated side note: `pow` is designed to handle nasty stuff like e to the power of pi and is utter overkill for an integer to the power of a small integer. Performance wise you're almost always better off multiplying it out. Here's a question from an Asker who ran into something similar yesterday: https://stackoverflow.com/questions/49062894/5th-power-operation-performs-quicker-than-switch-statement . Repeatedly multiplying is surprisingly fast. – user4581301 Mar 02 '18 at 17:06
  • @Mathbg My code? I copied your code – Killzone Kid Mar 02 '18 at 17:20
  • @KillzoneKid Yes, I know. I meant that it works in that ideone but not in my system –  Mar 02 '18 at 17:34

1 Answers1

3

There is no syntax error, otherwise your code would not end up in an executeable to run.

The explanation for the unexpected output of "3130" is a misuse of a floating point function in an integer context.

long long n; cin>>n; // n becomes 31
long long j = floor((log10(n))); // j becomes 1
long long nn = (n*(pow(10,j+1)))+n; // the result from pow is a floating point just below 100
// integer-multiplied by 31 gives 3099
// adding 31 results in 3130
cout<<j<<" "<<nn; // output 3130
Yunnosch
  • 26,130
  • 9
  • 42
  • 54