ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

Click Here to receive this Complete Guide absolutely free.

in my C code, I make use of 'log2' function clearly described in the 'man log2' command,
but the compilation gives the following:

[QUOTE]
gcc -ansi -pedantic -Wall -c test.c
test.c: In function ‘main’:
test.c:48: warning: implicit declaration of function ‘log2’
test.c:48: warning: format ‘%f’ expects type ‘double’, but argument 2 has type ‘int’
gcc -lm -o test test.o lm.o
[/QOTE]

#include <stdio.h>
#include <math.h>
int main( int argc, char *argv[] )
{
double log_base_2;
log_base_2 = log2( (double)argc );
printf( "Log base 2 of %d is %f\n",
argc,
log_base_2 );
return 0;
}
$ gcc -Wall -lm -o log2_test log2_test.c
log2_test.c: In function `main':
log2_test.c:8: warning: implicit declaration of function `log2'
$ ./log2_test 2
Log base 2 of 2 is 1.000000
$ gcc --version
gcc (GCC) 3.4.1
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I did a locate math.h and found it at /usr/include/math.h
I opened it up, and found no reference to log2(). I didn't check if it included any other headers, but given the warning above, it would seem that it's nowhere to be found. It links as expected though. So, you're probably safe to ignore those warnings.

I've taken your code, compiled a 'titi' command and that's what I get...

Code:

129 ~/tools/LM/src > titi 2
Log base 2 of 2 is 1.000000
130 ~/tools/LM/src > titi 3
Log base 2 of 2 is 1.000000
131 ~/tools/LM/src > titi 6
Log base 2 of 2 is 1.000000
132 ~/tools/LM/src > titi 1
Log base 2 of 2 is 1.000000

I knew what I meant to do, I just didn't implement it. The correct version of the code is:

Code:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main( int argc, char *argv[] )
{
double log_base_2 = 0;
double given_value;
if( argc < 2 )
{
fprintf( stderr, "ERROR! You must give a numeric argument!\n" );
return 1;
}
given_value = strtod( argv[1], NULL );
log_base_2 = log2( given_value );
printf( "Log base 2 of %f is %f\n",
given_value,
log_base_2 );
return 0;
}

In case you're curious, I picked a very poor test value. argc was always equal to 2: the command name itself and one argument (the number). I intended the supplied number get used in the calculation, but that's not what I told the computer to do. The above code fixes that. Sorry.

Originally posted by thelonius
gcc -ansi -pedantic -Wall -c test.c
test.c: In function ‘main’:
test.c:48: warning: implicit declaration of function ‘log2’
test.c:48: warning: format ‘%f’ expects type ‘double’, but argument 2 has type ‘int’
gcc -lm -o test test.o lm.o

Just wanted to add my two cents, on the warnings. This is what Kernighan & Ritchie have to say :

Quote:

If there is no function prototype, a function is implicitly declared by its first appearance in an expression. -- this explains the first warning

If a name that has not been previously declared occurs in an expression and is followed by a left parenthesis, it is declared by context to be a function name, the function is assumed to return an int, and nothing is assumed about its arguments. -- this explains the second warning

Thus, this is because the function log2() has not been declared.
As Dark_Helmet stated, I also did not find any reference to log2() in math.h (though I also didn't check all the headers included by it for references to log2())

It seems I (and probably others) got short-changed on man pages. This is what I have:

Code:

EXP(3) Linux Programmer's Manual EXP(3)
NAME
log2, log2f, log2l - base-2 logarithmic function
SYNOPSIS
#include <math.h>
double log2(double x);
float log2f(float x);
long double log2l(long double x);
DESCRIPTION
The log2() function returns the natural logarithm of x.
ERRORS
The log2() function can return the following error:
EDOM The argument x is negative.
CONFORMING TO
SVID 3, POSIX, BSD 4.3, ISO 9899. The float and the long double vari-
ants are C99 requirements.
SEE ALSO
sqrt(3), cbrt(3)
2002-07-27 EXP(3)

Interesting. I've got 1.67. I'm surprised it's up to 2.03. Mine is an LFS system built only a few months ago. LFS 6.0 I believe. I just checked the LFS website and LFS 6.1 has leapt up to 2.01.

Originally posted by Dark_Helmet It seems I (and probably others) got short-changed on man pages. This is what I have:

Code:

EXP(3) Linux Programmer's Manual EXP(3)
NAME
log2, log2f, log2l - base-2 logarithmic function
SYNOPSIS
#include <math.h>
double log2(double x);
float log2f(float x);
long double log2l(long double x);
DESCRIPTION
The log2() function returns the natural logarithm of x.
ERRORS
The log2() function can return the following error:
EDOM The argument x is negative.
CONFORMING TO
SVID 3, POSIX, BSD 4.3, ISO 9899. The float and the long double vari-
ants are C99 requirements.
SEE ALSO
sqrt(3), cbrt(3)
2002-07-27 EXP(3)

That's what I have, too. Using FC4, and the version 4 compiler.

Code:

$ gcc --version
gcc (GCC) 4.0.1 20050727 (Red Hat 4.0.1-5)
Copyright (C) 2005 Free Software Foundation, Inc.

CAUTION: This manual page EXPLICITLY states that the log2 function returns the NATURAL log, NOT the base-2 log. This may be an incompatibility with older versions. You may need to do a log2(x)/log2(2) to get what you want. (I haven't checked this.)

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.