Log2 not found in my math.h?

I am using a fairly new installation of Visual C ++ 2008 Express.

I am trying to compile a program that uses the log2 function, which was found by enabling Eclipse on a Mac, but this Windows computer cannot find the function (error C3861: "log2": identifier not found).

The way I figured this out includes IDE-specific directories, right? math.h is not in the Microsoft SDK \ Windows \ v6.0A \ Include \ directory, but I found math.h in this directory: Microsoft Visual Studio 9.0 \ VC \ include. There is cmath in this directory ...

Where is log2?

+29
c math visual-studio
Apr 16 '09 at 20:51
source share
4 answers

From here :

Prototype: double log2 (double number);
Header File: math.h (C) or cmath (C ++)

Alternatively emulate it like here

#include <math.h> ... // Calculates log2 of number. double Log2( double n ) { // log(n)/log(2) is log2. return log( n ) / log( 2 ); } 

Unfortunately, Microsoft does not provide it .

+56
Apr 16 '09 at 20:55
source share

If you are trying to find log2 strictly integers, some bitwise ones cannot hurt:

 #include <stdio.h> unsigned int log2( unsigned int x ) { unsigned int ans = 0 ; while( x>>=1 ) ans++; return ans ; } int main() { // log(7) = 2 here, log(8)=3. //for( int i = 0 ; i < 32 ; i++ ) // printf( "log_2( %d ) = %d\n", i, log2( i ) ) ; for( unsigned int i = 1 ; i <= (1<<30) ; i <<= 1 ) printf( "log_2( %d ) = %d\n", i, log2( i ) ) ; } 
+10
Mar 08 2018-12-12T00:
source share

log2() defined only in the C99 standard, and not in the C90 standard. Microsoft Visual C ++ is not fully compatible with C99 (hell, there is not one fully compatible with C99 compiler, I believe - even GCC does not fully support it), so log2() not required.

+9
Apr 20 '09 at 2:33
source share

Visual Studio 2013 adds log2() . See C99 Library Support in Visual Studio 2013 .

+3
Feb 23 '15 at 15:44
source share



All Articles