LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-26-2005, 10:09 PM   #1
thelonius
Member
 
Registered: Aug 2005
Location: Montréal
Distribution: Debian Testing, Slackware 10.2
Posts: 136

Rep: Reputation: 15
log2 not found ?


Hello,

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]

The last warning is related to the code line

Code:
#include<math.h>
...
printf( "%f \n",  log2( value));
what's wrong ?

Thank you
 
Old 09-26-2005, 10:45 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
Well, I just compiled the following code:
Code:
#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.
 
Old 09-26-2005, 11:01 PM   #3
thelonius
Member
 
Registered: Aug 2005
Location: Montréal
Distribution: Debian Testing, Slackware 10.2
Posts: 136

Original Poster
Rep: Reputation: 15
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
The result is always the same - 1.000...
 
Old 09-26-2005, 11:42 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
It's because I'm a moron.... seriously.

Try executing it like this:
Code:
$ titi 1 2 3 4 5 6 7
Log base 2 of 8 is 3.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.
 
Old 09-26-2005, 11:55 PM   #5
thelonius
Member
 
Registered: Aug 2005
Location: Montréal
Distribution: Debian Testing, Slackware 10.2
Posts: 136

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by Dark_Helmet
It's because I'm a moron.... seriously.
i didn't pay attention to the code either... so we are two morons
 
Old 09-27-2005, 12:22 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,511

Rep: Reputation: 2415Reputation: 2415Reputation: 2415Reputation: 2415Reputation: 2415Reputation: 2415Reputation: 2415Reputation: 2415Reputation: 2415Reputation: 2415Reputation: 2415
Also, in C, any undeclared fn is assumed to rtn int....
 
Old 09-28-2005, 04:10 PM   #7
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Rep: Reputation: 32
Re: log2 not found ?

Quote:
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())

Last edited by koodoo; 09-28-2005 at 04:15 PM.
 
Old 09-28-2005, 07:31 PM   #8
kjordan
Member
 
Registered: Jul 2004
Distribution: LFS, I felt the itch and scratched it
Posts: 227

Rep: Reputation: 31
The man page says it all:
Quote:
Compile with -std=c99; link with -lm.
 
Old 09-28-2005, 09:22 PM   #9
thelonius
Member
 
Registered: Aug 2005
Location: Montréal
Distribution: Debian Testing, Slackware 10.2
Posts: 136

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by kjordan
The man page says it all:
this is it. thanks a lot.

but what man page ? 'man log2' doesn't give this
 
Old 09-29-2005, 01:43 PM   #10
kjordan
Member
 
Registered: Jul 2004
Distribution: LFS, I felt the itch and scratched it
Posts: 227

Rep: Reputation: 31
Quote:
Originally posted by thelonius
this is it. thanks a lot.

but what man page ? 'man log2' doesn't give this
Yep, the log2 manpage.

Here's what I get for it:
Quote:
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);

Compile with -std=c99; link with -lm.

DESCRIPTION
The log2() function returns the base 2 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.
 
Old 09-29-2005, 01:54 PM   #11
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
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)
 
Old 09-29-2005, 02:12 PM   #12
kjordan
Member
 
Registered: Jul 2004
Distribution: LFS, I felt the itch and scratched it
Posts: 227

Rep: Reputation: 31
Looks like the man-pages package owns that man file, and I have 2.03 so maybe it was added in later versions? Any idea which version you have?
 
Old 09-29-2005, 02:17 PM   #13
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
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.

Time to upgrade it sounds like.
 
Old 09-29-2005, 02:32 PM   #14
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 347Reputation: 347Reputation: 347Reputation: 347
Please see the CAUTION at the end of this note.

Quote:
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.)
 
Old 09-29-2005, 02:49 PM   #15
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
Interesting point. I'm guessing that may be a copy-paste error in the man page.

The test program spat out base 2 logs:
Quote:
$ titi 1 2 3 4 5 6 7
Log base 2 of 8 is 3.000000
So it looks like the function does what it intends, but the documentation got neglected.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
INIT NOT FOUND + HDA(x) NOT FOUND fred99 Linux - Software 1 02-25-2005 02:10 PM
CDROM found/not found SUSE 9.2 dirdej SUSE / openSUSE 4 01-14-2005 11:58 PM
gcc vs. g++ -> iostream lib found vs. not found CooManChu Linux - General 1 12-11-2004 09:20 AM
weirdness with log2() spuzzzzzzz Programming 5 08-08-2004 05:45 PM
PDC 20276 - Promise Fasttrack 133 found but still not found.. FransE Linux - Hardware 3 02-16-2004 07:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration