[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

bug on cast from double to int

>Submitter-Id:	net
>Originator:	Uwe Strempel
>Confidential:	no
>Synopsis:	casting int to double with got wrong result
>Severity:	critical
>Priority:	 medium 
>Category:	c
>Category:  c++
>Class:	sw-bug
>Release:	3.3.5 (Debian 1:3.3.5-13) (Debian testing/unstable)
System: Linux lux 2.6.9 #2 Tue Dec 28 22:27:56 CET 2004 i686 GNU/Linux
Architecture: i686

host: i486-pc-linux-gnu
build: i486-pc-linux-gnu
target: i486-pc-linux-gnu
configured with: ../src/configure -v --enable-languages=c,c++,java,f77,pascal,objc,ada,treelang --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-gxx-include-dir=/usr/include/c++/3.3 --enable-shared --enable-__cxa_atexit --with-system-zlib --enable-nls --without-included-gettext --enable-clocale=gnu --enable-debug --enable-java-gc=boehm --enable-java-awt=xlib --enable-objc-gc i486-linux
	I've written an macro for log2 calculation to calculate the exponent of one value.
	Than I cast this value to from double to int.
	The 2 values double and int differs. 
	log2(8) should be 3 but after casting to int its 2.
	This bug could be reproduced on 2 different linux distributions (Debian GNU/Linux and Fedora Core 2) with different compiler versions.
	use this code to reproduce it
	--- code ---
	#include <math.h>
	#include <stdlib.h>
	#define log2(val) (log((val))/log(2.0))
	int main()
	      int n1=log2(8);
		double d1=log2(8);
		printf("%d %f\n",n1,d1);
		return 0;
	--- end ---						  
	For workaround I implemented a function for log2.

Reply to: