LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 12-03-2005, 08:02 PM   #1
zahadumy
Member
 
Registered: May 2005
Location: Cluj, Romania
Distribution: Fedora Core 6
Posts: 226

Rep: Reputation: 31
trunc function returns a double? :confused:


The question is I need to use trunc function, but i saw in the manual page this:
Code:
NAME
       trunc, truncf, truncl - round to interger, towards zero

SYNOPSIS
       #include <math.h>

       double trunc(double x);
How come it returns a double? Shouldn't it return a int? I tried to make a conversion to int (something like this)
Code:
(int)trunc(sqrt(n))
but it doesn't work. Thank you.
 
Old 12-03-2005, 08:17 PM   #2
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
Unhappy

Quote:
Originally Posted by zahadumy
Code:
(int)trunc(sqrt(n))
but it doesn't work. Thank you.
It must work
What did you mean?
Did you "#define __USE_ISOC99" before "#include math.h"? It will help you if you see some warnings about implicit declarations.
 
Old 12-03-2005, 08:40 PM   #3
zahadumy
Member
 
Registered: May 2005
Location: Cluj, Romania
Distribution: Fedora Core 6
Posts: 226

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by Ygrex
Did you "#define __USE_ISOC99" before "#include math.h"?
What do I have to include before that define? I used these functions a lot, but I've never got these error messages, they worked fine until now.
This is a simple program and the error messages I get when I try to compile it.
Code:
[cosmin@localhost fara sincronizari]$ cat test.c
#include <stdio.h>
#include <math.h>

int
main ()
{
  int n = 5, x;
  x = (int) trunc (sqrt (n));
  printf ("Radical din n: %d\n", x);
}
[cosmin@localhost fara sincronizari]$ gcc -o test test.c
/tmp/cc8bYcrs.o(.text+0x31): In function `main':
: undefined reference to `sqrt'
/tmp/cc8bYcrs.o(.text+0x43): In function `main':
: undefined reference to `trunc'
collect2: ld returned 1 exit status
Thank you.
 
Old 12-03-2005, 08:47 PM   #4
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
Ok, you have no need in 'define', but:
Code:
gcc -o test test.c -lm
 
Old 12-03-2005, 08:58 PM   #5
zahadumy
Member
 
Registered: May 2005
Location: Cluj, Romania
Distribution: Fedora Core 6
Posts: 226

Original Poster
Rep: Reputation: 31
What is -lm option for gcc?

Quote:
Originally Posted by Ygrex
Code:
gcc -o test test.c -lm
It works. Thanks.
But why? What is this? Are you sure this is what I need? I told you I used these a lot, but I think I have never used the functions in math.h in C (only C++), and there they work. Is it possible to put some code instead of "-lm" option? Thank you.
 
Old 12-03-2005, 09:35 PM   #6
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
Post

Well, you tried to use function trunc. You know its name. But do you know its code? What the algorithm does it execute whenevre you use 'trunc()'?
You can divide the compilation process into two steps. The first - creating of the object file. It contains your code and etry-points - the functions you have not written by yourself but you use them, they are declared in the included headers.
The second stage - linking your object file with different libraries, which contain all necessary functions (e.g. trunc() or sqrt()). It defines where entry-points should find their according function codes (exact algorithms). These functions are stored in the binary state in the shared libraries. I know that anything from math.h is in the libm.so library. So you should point the linker (an application, which executes the second stage of the compiling) to the appropriate library (libm.so in you case). You can do it with the '-l' option followed by the library name.
Library name could be either the explicit path (/usr/lib/libm.so) or implict, in the last case linker will add 'lib' prefix and '.so' extension to the name you specify, for any library begins with 'lib' and ends with '.so' (and, maybe a version number as well).
The most important thing. Where can one find which library contains the function one find in any header? One should read man-page abit more attentively. In your case see trunc(3) manpage, the SYNOPSIS finishes with:
Quote:
Link with -lm.
 
Old 12-03-2005, 10:20 PM   #7
zahadumy
Member
 
Registered: May 2005
Location: Cluj, Romania
Distribution: Fedora Core 6
Posts: 226

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by Ygrex
The most important thing. Where can one find which library contains the function one find in any header? One should read man-page abit more attentively. In your case see trunc(3) manpage, the SYNOPSIS finishes with:
Quote:
Link with -lm.
On Fedora Core 3 and Slackware 10.1, if you type "man trunc" you will see this. Thank you.
 
  


Reply



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
Double the desktop, not double the fun! bizshop SUSE / openSUSE 3 08-26-2005 12:22 PM
C function that returns $HOME cbranje Programming 1 02-12-2005 05:41 PM
open function returns error sudheernair Programming 2 05-07-2004 11:14 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
confused with the following function basics ... purpleburple Programming 3 08-27-2002 12:03 AM

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

All times are GMT -5. The time now is 09:21 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
Open Source Consulting | Domain Registration