cvs.gedasymbols.org/archives/browse.cgi   search  
Mail Archives: djgpp/1998/04/09/15:22:09

Date: Thu, 9 Apr 1998 15:20:05 -0400 (EDT)
From: Michael Phelps <loverns AT welchlink DOT welch DOT jhu DOT edu>
To: djgpp AT delorie DOT com
Subject: Differences between -lm and not -lm
Message-ID: <Pine.SOL.3.96.980409150133.20230A-100000@welchlink.welch.jhu.edu>
MIME-Version: 1.0

	I know that there are differences between compiling with -lm and
using the default math library, but I didn't know that it was that
significant.  I wrote a little routine to extract a variable number of
digits from an int and return them.  Observe:

#include <stdio.h>
#include <math.h>

int	get_digits(int, int, int);

int	main()
{
	int	x;

	x = get_digits(123456, 4, 2);
	printf("Results of 12346, 4, 2:  %d\n", x);

	x = get_digits(535135735, 7, 4);
	printf("Results of 535135735, 7, 4:  %d\n", x);

	x = get_digits(2, 1, 1);
	printf("Results of 2, 1, 1:  %d\n", x);

	x = get_digits(13, 1, 1);
	printf("Results of 13, 1, 1:  %d\n", x);

	return 0;
}

int	get_digits(int number, int digit_to_start_with, int length)
{
	number %= (int)pow(10, digit_to_start_with);
	number /= (int)pow(10, (digit_to_start_with - length));

	return number;
}

When I compile this program as such:
	gcc test.c -o test.exe -O2
The results are:
	Results of 123456, 4, 2:  35
	Results of 535135735, 7, 4:  5135
	Results of 2, 1, 1:  2
	Results of 13, 1, 1:  3

(The first results should be 34, not 35, right?)

When I compile it with the math library linked in:
	gcc -O2 test.c -o test.exe -lm
The results are:
	Results of 123456, 4, 2:  34
	Results of 535135735, 7, 4:  5135
	Results of 2, 1, 1:  2
	Results of 13, 1, 1:  3

This is correct!  I assume that it is the pow() function that differs
here.  Should this be the correct behavior when it is not linked with the
math library?  (I remember that in version 1.x DJGPP required the math
libary to be explictly linked, but I thought that with DJGPP V2.x that
situation changed--shouldn't it work the same either way?)  I'm using
DJGPP V2.01, by the way.

						---Michael Phelps


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019