Soumadeep Saha
Soumadeep Saha

Reputation: 335

long long int type function returning decimal value

I wrote a piece of code that will return the square root (without the decimal places i.e floor of the square root) given an integer. But when I am calling it it is giving me a decimal value.Why is this happening? Here is the code `

#include<iostream>

using namespace std;

long long int sqrt(long long int a){
    long long int upper = a,lower=0;
    long long int mid = 0;
    while (!(mid*mid >= a and (mid-1)*(mid-1) < a)){
        mid = (lower+upper)/2;
        if(mid*mid > a){
            upper = mid;
        }
        else if(mid*mid <= a){
            lower = mid;
        }
    }
    return mid;

}    
int main(){
    int a;
    while(cin>>a){
        cout<<sqrt(a)<<endl;
    }
}

Upvotes: 1

Views: 1990

Answers (3)

Walter
Walter

Reputation: 45424

The reason for the problem has been correctly identified in the other answers, but the solutions are not satisfactory. You want all overloads of sqrt (your own function and those provided in namespace std) to be available and not require any scope resolution like mymath::sqrt.

The solution must look at the function call. You essentially have

long long int sqrt(long long int);  // your function
float sqrt(float);                  // from namespace std
double sqrt(double);                // from namespace std
long double sqrt(long double);      // from namespace std
double sqrt(integral_type);         // from namespace std in C++11

Since you call sqrt(int) the last overload will be picked (if you have C++11) and the 3rd otherwise. In order to pick your own function you must call it:

int main(){
    long long int a;
    while(cin>>a) {
        cout<<sqrt(a)<<endl;
    }
}

Upvotes: 3

Dimitrios Bouzas
Dimitrios Bouzas

Reputation: 42899

  • Microsoft implemented the <iostream> header in way that indirectly includes <cmath> header.

  • This is not a bug, the C++ standard doesn't dictate that indirect inclusion of other STL headers in a STL header is forbidden.

  • As such, this indirect inclusion of header <cmath> shadows your sqrt.

  • In other compilers like Clang or GCC you don't experience such a behaviour.

Solution:

  1. Put your sqrt function in a separate namespace (e.g., mymath).
  2. Call your function in code as mymath::sqrt.

#include<iostream>

namespace mymath {
  long long int sqrt(long long int a) {
    long long int upper = a;
    long long int lower = 0;
    long long int mid   = 0;

    while (!(mid * mid >= a && (mid - 1) * (mid - 1) < a)){
      mid = (lower + upper) / 2;
      if (mid * mid > a){
        upper = mid;
      } else if (mid * mid <= a){
        lower = mid;
      }
    }
    return mid;
  }
}

int main(){
  int a;
  while (std::cin >> a){
    std::cout << mymath::sqrt(a) << std::endl;
  }
}

Upvotes: 2

tmyklebu
tmyklebu

Reputation: 14205

You're probably getting std::sqrt here instead of the ::sqrt you defined. Depending on the C++ implementation you're using, qualifying your sqrt call as ::sqrt might work. Better yet, call your custom sqrt something else and call it directly.

Upvotes: 3

Related Questions