1

The code below shows error

#include<bits/stdio.h>
using namespace std;
int main() {
        int n; 
        cin>>n; 
        if(log2(n)=="-inf") 
        cout<<"you entered zero"; 
}

The error it shows

stack_question.cpp: In function ‘int main()’:
stack_question.cpp:6:19: error: invalid operands of types ‘__gnu_cxx::__enable_if::__type {aka double}’ and ‘const char [5]’ to binary ‘operator==’
if(log2(n)=="-inf")
~~~~~~~^~~~~~~~

One more doubt

Is it a runtime error or compile time error??

  • 1
    Does this answer your question? [Negative infinity](https://stackoverflow.com/questions/20016600/negative-infinity) or [Best way to check if double equals negative infinity in C++](https://stackoverflow.com/questions/28683702/best-way-to-check-if-double-equals-negative-infinity-in-c) – walnut Dec 17 '19 at 05:18
  • 1
    You are not running the program. You are trying to compile it. It is a compile-time error. `#include` is just wrong. You need `#include` for `cout` and `cin` and `#include` for `log2`. Also note that `log2` only returns `-inf` if `n` is `+0` or `-0`, so you can just test that beforehand. – walnut Dec 17 '19 at 05:20
  • @walnut, **yes**, reading that question helped. **I got my answer** – User17114027 Dec 17 '19 at 05:25

2 Answers2

1

This should be the correct code :

#include<iostream>
#include<cmath>
using namespace std;
int main(){
    int n;
    cin>>n;
    if(log2(n) == - INFINITY){
        cout<<"you entered is zero";
    }
}

The header <cmath> has the Positive INFINITY defined.

And the error you are getting is a compile time error as type checking is really handled by semantics analyzer.

Hope this helps

Suraj Upadhyay
  • 475
  • 3
  • 9
0

Below is the code after correction

Check line number 6 of the code

#include<bits/stdc++.h>
using namespace std;
int main() {
        int n; 
        cin>>n; 
        if(log2(n)==-INFINITY){ 
                cout<<"you entered zero"; 
        }
}
Community
  • 1
  • 1
  • 2
    [Why should I not #include ?](https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H.) – walnut Dec 17 '19 at 05:34