Ekdeep Singh Lubana
Ekdeep Singh Lubana

Reputation: 113

limit of the data type

The following code is printing a wrong output for some of the values while right for the others. For example the output for 699838110 is 2938640281442109880 while it should be 2938640272852175288

#include <iostream>
#include<cmath>
using namespace std;

int main()
{
    int n;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        int d;
        cin>>d;
        if(d==1)
        {
            cout<<1<<endl;
        }
        else
        {
            long long int y=pow(d,2);
            long long int x=8+6*y-12*d;
            cout<<x<<endl;
        }
    }
    return 0;
}

Is this happening because of the limit of the data type long long int?? If rather than defining x, I straight away write 8+6*d*d-12*d in my cout statement then generates a garbage value. Does this mean that there's a limit to the cout statement. If yes, then what's that limit or is it decided by the data type of the variable which are involved in the computations?

Upvotes: 0

Views: 60

Answers (3)

The pow(d,2) expression - using std::pow - involves floating point computation (probably IEEE floating point with d converted to double so 53 bits of mantissa). Read http://floating-point.de/

Better avoid that and do:

  long long int y= d*d;

or even better

  long long int y = ((long long)d)* d;

so that the squaring occurs in 64 bits arithmetic which is likely to be slightly faster and more accurate.

BTW, you should have compiled with all warnings & debug info (g++ -Wall -Wextra -g if using GCC ...) and run step by step your program in a debugger (gdb). That surely would have helped you to find the bug by yourself.

Upvotes: 2

William Callahan
William Callahan

Reputation: 639

Try the following. I believe the the "d" variable needed to be a long long.

int main() {
    long long n;
    cin>>n;
    for(long long i=0LL; i<n; i++)
    {
        long long d;
        cin>>d;
        if(d==1)
        {
            cout<<1<<endl;
        }
        else
        {
            long long y=d*d;
            long long x=8LL+6LL*y-12LL*d;
            cout<<x<<endl;
        }
    }
    return 0;
}

Credit: @Basile Starynkevitch

Upvotes: 1

Peter
Peter

Reputation: 36617

12*d will be computed as int, not long long int. The fact it is in a larger expression that produces a long long int does not change that.

If your int is 32-bit, then 12*d will overflow. That is undefined behaviour. An int is not required to represent values greater than 32767 actually, even if 32-bit integers (able to hold values up to about 2147483647 or so) are more common in practice.

So your problem has nothing to do with limitations of streams. It has to do with types of variables and results within expressions.

Upvotes: 2

Related Questions