log2 not found in my math.h?

后端 未结 5 903
野的像风
野的像风 2020-12-01 15:25

I\'m using a fairly new install of Visual C++ 2008 Express.

I\'m trying to compile a program that uses the log2 function, which was found by including using Eclipse

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 16:31

    If you're trying to find the log2 of strictly integers, some bitwise can't hurt:

    #include 
    
    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 ) ) ;
    }
    

提交回复
热议问题