Problem including <math.h>

Hi there,

I’m having this strange problem after a fresh install. I’ve got to use the exp function from math.h and it doesn’t work.

It compiles fine, the program runs, but the output is ****. Qt says it returns a 0 int. This sample program:


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

int main(void)
{
        int x = 1;
        printf("number: %d
", exp(x));

        return 1;
}

Gives output like this:


number: -1940205048

I’m guessing the library that has the math.h functions isn’t loaded properly but I don’t know how to find out if this is true.

Something that might be related to this, I installed Qt from qt.nokia.com but deleted it and installed it again via zypper.

I use this laptop for school and I can’t do much without being able to use math.h.

Thanks for reading :D.

fabianbadoi wrote:

> int x = 1;
> printf("number: %d
", exp(x));
>
%d formats an integer but exp(1) = 2.7…

the exp function works on doubles and returns a double

double exp( double arg );

you should write
printf("number: %f
", exp(x));

and define x as double

Qt has absolutely nothing to do with it since you use the standard c
libraries here (stdio and math are not in qt).


openSUSE 11.3 64 bit | Intel Core2 Quad Q8300@2.50GHz | KDE 4.5 | GeForce
9600 GT | 4GB Ram
openSUSE 11.3 64 bit | Intel Core2 Duo T9300@2.50GHz | KDE 4.5 | Quadro FX
3600M | 4GB Ram

On Wed, 24 Nov 2010 19:36:02 +0000, fabianbadoi wrote:

> I’m guessing the library that has the math.h functions isn’t loaded
> properly but I don’t know how to find out if this is true.

In addition to Martin’s response, if the function wasn’t found the
program wouldn’t compile at all, but the compilation would generate an
error indicating that exp was an undefined symbol.

Jim


Jim Henderson
openSUSE Forums Administrator
Forum Use Terms & Conditions at http://tinyurl.com/openSUSE-T-C

Well, that created some confusion.

I had a watch in the Qt debugger that called the exp() function with some parameter and it always showed bogus output. Then I tried to see if there was a problem with the lib (by doing that printf), and it showed bogus output too.

I guess the debugger doesn’t like function calls as watches.

Thanks for the response, all is fine now :smiley:

This illustrates two points.

C doesn’t check for type safety in printf.

I believe that if you turn on warnings in gcc, it warns you about the mismatch between the return type of exp and %d.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Wrote your code to the file pp.c, and then I tryed to compile it…


[vampird@sharwyn]:~/devel/tmp$ gcc -Wall -Wextra -Werror -ansi -pedantic
- -O2 -march=native pp.c -o pp
cc1: warnings being treated as errors
pp.c: In function ?main?:
pp.c:7:3: error: format ?%d? expects type ?int?, but argument 2 has type
?double?
[vampird@sharwyn]:~/devel/tmp$


VampirD

Microsoft Windows is like air conditioning
Stops working when you open a window.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.15 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org/

iEYEARECAAYFAkzug6QACgkQJQ+0ABWtaVm/YgCguc0dK6J5xCwOxOzBXxo4maAi
4hQAoM5SbFhSQFoLbht4xArd+9xpD+NN
=k3am
-----END PGP SIGNATURE-----