2
votes

for the whole day I've been fighting with something I believed being a bug but then (with some nasty printf-debugging) it came out that it was a weird exp math function behaviour ... maybe there's something I'm missing here because I really don't get it.

Could you please explain me the following behaviour:

#include <cmath>

printf( "%lf\n", exp( -100.0 ) ); // <--- I know this should be %e, see the edit above

gcc 4.2.1 ( both on mingw and Mac OS X, both 64bit ):

0.000000

msvc 2012 ( 64bit )

3.720075....e-44

( Where '....' are decimals I don't remember and can't test right now since I'm at home on my Mac ).

GCC flags:

g++ -Wall -g -pg *.cpp -o mytest

MSVC with default flags.

What am I missing here? Isn't exp a standard function with standard precision?

EDIT 2

Ok this seems to be related to MSVC default floating point model ( /fp:precise ) ... any chance to have the same model with GCC ? I'd like to release the project as opensource but there's not point in it if it's not gonna work with the most popular opensource compiler due to a lack of precise floating point model.

EDIT

Ok so I was using a wrong format for the printf call ( I should've used %e or %.50lf to print scientific notation or enough decimals ), but the point is that the whole project doesn't give me the expected results ( I'm developing a SVM with SMO algorithm and Gaussian kernel ) if compiled with GCC, while it works flawlessly if I compile it with MSVC (of course with no changes whatsoever ). What could cause that?

( Picture of the same source code running on a Mac and on a VM with Windows 7 )

Picture of the **same** source code running on a Mac and on a VM with Windows 7

2
sure but you are relying on the C library to print it out and that is not a standard conversion. if you print the raw binary value for the floating point result, if the hardware conforms to the ieee spec, then you should get the same answer. but here again there are different rounding modes and different compilers or operating systems may have a different opinion on rounding. - old_timer
why so old version of gcc? - PiotrNycz
@PiotrNycz I don't update gcc very often since it's not the compiler I use for my job, I was just playing around with it for a small Makefile based project. - Simone Margaritelli
What version of MSVC are you using? For a very long time, MSVC compilers have not been standard-compliant. Even now their conformance is spotty, although I think that most C99 is covered after only 15 years :-) - Sergey Kalinichenko
MSVC C support is ****ed up, I'm compiling it as C++ of course with visual studio 2012 updated to the latest revision. - Simone Margaritelli

2 Answers

7
votes

The computations are the same, you are not printing enough digits:

printf( "%.50lf\n", exp( -100.0 ) );

This produces the following output with gcc:

0.00000000000000000000000000000000000000000003720076

That's the 3.720076e-44

Using %le specifier produces 3.720076e-44, too.

0
votes

I just compiled and executed

printf("%lf\n", exp(-100.0));

in my Dell Laptop (Windows 7, 64 bit and Visual Studio 2012) and my Macbook Pro (Mountain Lion, 64 bit and XCode). Both are displaying

0.000000

as the result.

Are you sure, you didn't change the format specifier in Visual Studio?