473,763 Members | 9,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Standard C and math library

I have a quick question about the math library included in the standard
C90 (and 99).

The gcc, xlc and possibly other compilers/linkers on some unix
platforms cannot use any math functions in math.h if a switch (-lm) is
used, so *explicitly* having the linker use a file called libm.so or
libm.a/dylib/whatever.

Is it true on all the platforms? (answer: probably no)

Is this a break on the standard?

Since abs(), sqrt() and all the math.h defined funcions/types ARE
(correct me if I make a mistake) put into the ISO C, any compiler and
any linker should NOT use an external library other than the standard C
library for those funcions. Is it so? Is the libc defined in the
standard having also math.h? If not, the separation seems to say ``math
is not in the standard c library, you must use something from outside''.

--
Sensei <se******@tin.i t>

cd /pub
more beer

Nov 15 '05 #1
8 2922
Sensei wrote:
I have a quick question about the math library included in the standard
C90 (and 99).

The gcc, xlc and possibly other compilers/linkers on some unix platforms
cannot use any math functions in math.h if a switch (-lm) is used, so
*explicitly* having the linker use a file called libm.so or
libm.a/dylib/whatever.

Is it true on all the platforms? (answer: probably no)

Is this a break on the standard?
No, it's not -- specifically because the standard says nothing about how
programs are compiled or linked.
Since abs(), sqrt() and all the math.h defined funcions/types ARE
(correct me if I make a mistake) put into the ISO C, any compiler and
any linker should NOT use an external library other than the standard C
library for those funcions. Is it so? Is the libc defined in the
standard having also math.h? If not, the separation seems to say ``math
is not in the standard c library, you must use something from outside''.


No, the math functions *are* in the standard library -- but, again,
since the magic words used for compilation and linking are external to
the standard, it doesn't matter.

[In a lot of ways, it's a strange historical wart; I suspect you don't
remember the days of external floating point coprocessors and such. In
addition, unlike today where it's mostly embedded systems (and
particularly memory constrained ones at that) where memory footprint
matters at that level of granularity, it once *did* matter --
particularly at the time the convention was adopted.

Periodically, a plea is made to make the standard library monolithic on
*nix systems -- and perhaps one day it will occur.]

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 15 '05 #2
In article <3j************ @individual.net >,
Artie Gold <ar*******@aust in.rr.com> wrote:

[In a lot of ways, it's a strange historical wart; I suspect you don't
remember the days of external floating point coprocessors and such. In
addition, unlike today where it's mostly embedded systems (and
particularly memory constrained ones at that) where memory footprint
matters at that level of granularity, it once *did* matter --
particularly at the time the convention was adopted.


It was strange even then. Any static linker would strip out unused
math functions or simply skip them in the first place. It was unlikely
that "-lm" would bloat your program if you didn't actually use any
floating point stuff.

But sometimes, "-lm" meant not to just link in fp libraries, but
to take an extra pass over the code and do some inlining of
hand-optimized assembly to replace certain fp operations, depending
on what stuff your fpu actually did in hardware vs. what your
program called for. The extra pass could actually take up some
cpu time and back then we didn't have a lot to spare.
--
7842++
Nov 15 '05 #3
Anonymous 7843 wrote:
In article <3j************ @individual.net >,
Artie Gold <ar*******@aust in.rr.com> wrote:
[In a lot of ways, it's a strange historical wart; I suspect you don't
remember the days of external floating point coprocessors and such. In
addition, unlike today where it's mostly embedded systems (and
particularl y memory constrained ones at that) where memory footprint
matters at that level of granularity, it once *did* matter --
particularl y at the time the convention was adopted.

It was strange even then. Any static linker would strip out unused
math functions or simply skip them in the first place. It was unlikely
that "-lm" would bloat your program if you didn't actually use any
floating point stuff.


Not so strange, really. The linker that needed to make
a pass over a great big barrel of functions that weren't used
required time to do so, and the time to pass over and ignore
all those math functions (count them; they are, one might say,
numer-ous) was noticeable. If you argue against separable
math libraries in 197x, you argue against precompiled headers
today -- it's a matter of scale, nothing more.

--
Er*********@sun .com
Nov 15 '05 #4
In article <J8************ ********@comcas t.com> Eric Sosman <es*****@acm-dot-org.invalid> writes:
....
Not so strange, really. The linker that needed to make
a pass over a great big barrel of functions that weren't used
required time to do so, and the time to pass over and ignore
all those math functions (count them; they are, one might say,
numer-ous) was noticeable.


Not really. Back in the early 70s I used a CDC Cyber for my work.
Most common was *not* to prelink before execution. It was on
execution that the linker came into play and created the binary
actually used. Linking from the compiled (relocatable) binary
to an executable took no noticable time. Consider a Fortran source
consisting of a few hundred subroutines. When compiling, each
routine was compiled on its own, relocatable, and whatever. When
executing you just entered LGO and linking and whatever began.
You could do all that before execution (and make the equivalent of
an EXE), but in most cases it was not worth the trouble. Linking
took no noticable time. For me it was really a culture shock when
I shifted to Unix. It took really extremely much time just to link.
(And to put it in perspective, that CDC machine had 65536 60-bits
words of memory, and served about 100 interactive users. The
interactive response was not worse than what I am getting at my
workstation.)
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 15 '05 #5
>> [In a lot of ways, it's a strange historical wart; I suspect you don't
remember the days of external floating point coprocessors and such. In
addition, unlike today where it's mostly embedded systems (and
particularly memory constrained ones at that) where memory footprint
matters at that level of granularity, it once *did* matter --
particularly at the time the convention was adopted.
It was strange even then. Any static linker would strip out unused
math functions or simply skip them in the first place. It was unlikely
that "-lm" would bloat your program if you didn't actually use any
floating point stuff.


The tricks get cuter than that. For example, some implementations
have two copies of printf(), one with floating point support and
one without it. The one with floating point support would drag in
other internal functions that wouldn't be used without floating
point support (for example, the routines that formatted floating-point
numbers as text). All this was based on implementor-known order
of loading functions in case there were more than one with the same
name. I believe at least one required use of -lm to get the version
of printf() which used floating point.
But sometimes, "-lm" meant not to just link in fp libraries, but
to take an extra pass over the code and do some inlining of
hand-optimized assembly to replace certain fp operations, depending
on what stuff your fpu actually did in hardware vs. what your
program called for. The extra pass could actually take up some
cpu time and back then we didn't have a lot to spare.


Gordon L. Burditt
Nov 15 '05 #6
Sensei wrote:

I have a quick question about the math library included in the
standard C90 (and 99).

The gcc, xlc and possibly other compilers/linkers on some unix
platforms cannot use any math functions in math.h if a switch (-lm)
is used, so *explicitly* having the linker use a file called
libm.so or libm.a/dylib/whatever.

Is it true on all the platforms? (answer: probably no)

Is this a break on the standard?

Since abs(), sqrt() and all the math.h defined funcions/types ARE
(correct me if I make a mistake) put into the ISO C, any compiler
and any linker should NOT use an external library other than the
standard C library for those funcions. Is it so? Is the libc
defined in the standard having also math.h? If not, the separation
seems to say ``math is not in the standard c library, you must use
something from outside''.


The standard says what facilities are to be available, and what
include files to specify. It DOES NOT specify a mechanism for
running the system, such as "pull lever A, wait for a ball to pop
out of slot B, then throw toggle switch C". For that you have to
look to your compiler/system documentation, and anything goes.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 15 '05 #7
On 2005-07-12 19:15:04 -0500, Artie Gold <ar*******@aust in.rr.com> said:
No, it's not -- specifically because the standard says nothing about
how programs are compiled or linked.
Ah! Ok.
No, the math functions *are* in the standard library -- but, again,
since the magic words used for compilation and linking are external to
the standard, it doesn't matter.
I see.
[In a lot of ways, it's a strange historical wart; I suspect you don't
remember the days of external floating point coprocessors and such. In
addition, unlike today where it's mostly embedded systems (and
particularly memory constrained ones at that) where memory footprint
matters at that level of granularity, it once *did* matter --
particularly at the time the convention was adopted.
Well, I've grown under C64 and Prodest, I had a 286 without 287
coprocessor... I know history in this case :) Anyway the standard C99
is far too young to be affected by math coprocessors and DX/SX
distinction. That's why I'm puzzled.
odically, a plea is made to make the standard library monolithic on
*nix systems -- and perhaps one day it will occur.]


One thing I thought is that using floating point library when not
needed would be a ``waste of space''... even though is just some kb...

--
Sensei <se******@tin.i t>

cd /pub
more beer

Nov 15 '05 #8
On Wed, 13 Jul 2005 01:18:35 GMT, in comp.lang.c ,
an******@exampl e.com (Anonymous 7843) wrote:
It was strange even then. Any static linker would strip out unused
math functions or simply skip them in the first place.
Euh, in the Olden Days, the time taken to run through and decide you
didn't need the stuff was appreciable. Imagine linking one of today's
apps on a 4.77Mhz chip.....
It was unlikely
that "-lm" would bloat your program if you didn't actually use any
floating point stuff.


Some compiler suites were especially braindead and included all
functions in Library X, whether or not you used them. Some still do
(try this with MFC sometime...) Its a QOI issue.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2440
by: F. Petitjean | last post by:
I have written a script to find the modules which export the largest number of names. The gc.getreferrers(*objs) function gives also an idea of the dependencies between the modules. The code (statsmod.py) : #!/usr/bin/env python # -*- coding: latin-1 -*- """
16
3601
by: Vadim Biktashev | last post by:
Hello all I would like to give a certain name to a certain global variable. Unfortunately, this name is already used in math.h for a mathematical function. Worse, I do need to use maths library and therefore have to include math.h. Thus my compiler reports conflicting declarations of the same name. Does anyone know any reasonably elegant way to resolve this conflict? I understand that I can give up and choose another name, but somehow not...
15
29217
by: puzzlecracker | last post by:
does anyone know how to implement this function efficiently?
12
1752
by: theoderich | last post by:
Following is possible with gcc and g++: #include <math.h> double sin(double) { return 1; } int main()
6
2245
by: junky_fellow | last post by:
On what basis it is decided that a particular funtion should be a part of Standard C library ? Do I get these standard C libraries along with the C compiler ? Or I can use the c complier from one vendor and the standard C libraries from the different vendor ? Also, the C standard (N869), page 164, refers to "standard headers". Do I get these standard headers along with the C compiler ?
5
9999
by: Martin Vilcans | last post by:
Hi, I'm new to this mailing list and fairly new to Python as well. I'm working on a prototype for a 3D game using OpenGL, and take this opportunity to learn Python better. I'm looking for a good library for vector math. I need to do vector addition, cross products, dot products etc. and probably in the future I'll need matrix math as well. So far I've used the Scientific library, which is very nice, but unfortunately it crashes when I...
21
2840
by: Kannan | last post by:
Its been a while I have done pure C programming (I was coding in C++). Is the following function valid according to standard C? int main(int argc, char *argv) { int x; x = 9; printf("Value of x is: %d\n", x);
39
2619
by: Jim Showalter | last post by:
Does a hosted implementation require any particular libraries in order to be considered a standard C compiler? -- _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ _/ Everything should be made as simple as possible, but not simpler. _/ -- Albert Einstein _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
14
2194
by: artifact.one | last post by:
It'd be really pleasant (in my opinion) if the next revision of the C language actually allowed some portable control over data alignment. Compiler-specific mechanisms for this stuff are so varied that it becomes impossible to even abstract the details away behind preprocessor macros. What I'd like to see:
0
9564
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9387
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10002
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9823
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.