How to calculate database 2 in C ++?

Here is my code.

#include <iostream> #include<stdlib.h> #include<stdio.h> #include<conio.h> #include<math.h> #include <cmath> #include <functional> using namespace std; void main() { cout<<log2(3.0)<<endl; } 

But the above code gives an error. Error code: Error C3861: "log2": identifier not found. How can I calculate log2 using C ++?

+4
source share
5 answers

e.g. for log 3 in base 2

 log (3) / log(2) 

will do it.

 #include <iostream> #include <cmath> using namespace std; int main() { cout << log(3.0) / log(2.0) << endl; } 
+5
source

Using high school math:

 log_y(x) = ln(x)/ln(y) 

But I agree that it’s a little strange that there is no such utility. This is probably due to the almost direct mapping of these functions to the FPU.

However, do not worry about using this “advanced” method. Math will not change. The formula will be valid for at least the next few lives.

+7
source

The following code works with the gcc compiler

 #include <iostream> #include<stdlib.h> #include <stdio.h> #include <math.h> #include <cmath> #include <functional> using namespace std; main() { cout<<log2(3.0)<<endl; } 
+4
source

This should be a general function to search for a log with the base of any given number.

 double log_base_n(double y, double base){ return log(y)/log(base); } 

So:

 cout<<log_base_n(3.0,2.0); 

gotta do the trick.

+3
source

use log(3.0)/log(2.0) . log2 not included in the C90.

 double log_2( double n ) { return log(n) / log(2); } 
+1
source

All Articles