?
=?ISO-8859-1?Q?Bj=F8rn_Augestad?=
Below is a program which converts a double to an integer in two
different ways, giving me two different values for the int. The basic
expression is 1.0 / (1.0 * 365.0) which should be 365, but one variable
becomes 364 and the other one becomes 365.
Does anyone have any insight to what the problem is?
Thanks in advance.
Bjørn
$ cat d.c
#include <stdio.h>
int main(void)
{
double dd, d = 1.0 / 365.0;
int n, nn;
n = 1.0 / d;
dd = 1.0 / d;
nn = dd;
printf("n==%d nn==%d dd==%f\n", n, nn, dd);
return 0;
}
$ gcc -Wall -O0 -ansi -pedantic -W -Werror -o d d.c
$ ./d
n==364 nn==365 dd==365.000000
$ gcc -v
Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-java-awt=gtk
--host=i386-redhat-linux
Thread model: posix
gcc version 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)
$
different ways, giving me two different values for the int. The basic
expression is 1.0 / (1.0 * 365.0) which should be 365, but one variable
becomes 364 and the other one becomes 365.
Does anyone have any insight to what the problem is?
Thanks in advance.
Bjørn
$ cat d.c
#include <stdio.h>
int main(void)
{
double dd, d = 1.0 / 365.0;
int n, nn;
n = 1.0 / d;
dd = 1.0 / d;
nn = dd;
printf("n==%d nn==%d dd==%f\n", n, nn, dd);
return 0;
}
$ gcc -Wall -O0 -ansi -pedantic -W -Werror -o d d.c
$ ./d
n==364 nn==365 dd==365.000000
$ gcc -v
Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-java-awt=gtk
--host=i386-redhat-linux
Thread model: posix
gcc version 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)
$