72c Subj : Re: Serious Problem with precision or if statement To : borland.public.cpp.borlandcpp From : "Sebastian Ledesma" Date : Thu Aug 14 2003 12:51 pm Hi: Did you checked the 'FAST FLOATING POINT' option in Options->Project->Compiler->Floating Point ? Saludos Sebastian "Chris" escribió en el mensaje news:3f3a4f95$1@newsgroups.borland.com... > > Hi everyone. > I am having a problem with the following program. It's very > simple but the results are very unexpected (at least I think so). > > code > ----------------------- > #include > > int main() { > > int Phase = 3; > > if( Phase/3.0 == 1.0 ) > printf("equal"); > else > printf("not equal"); > > return 1; > } > ----------------------- > > Now we would expect "equal" to be printed, but to my > (everyone's) surprise, "not equal" is printed. > > An even more extraordinary thing occurs when tracing the program. > The expression ( Phase/3.0 == 1.0 ) evaluates to TRUE. However, > the else(FALSE) branch is taken printing "not equal"! > > I have tried several modifications to the if statement > to no avail. > > if( (double)Phase/3 == 1.0 ) > if( Phase/3.0 == (double) 1 ) > > It is worthwhile to note that > if( Phase/3 == 1 ) does return the correct answer. > > Assigning the value (Phase/3.0) to an intermediate double x > and then checking if( x == 1.0 ) also works. > > If anyone has time or wants some amusement, run my program. > I am using Borland C++ 5.02 on an Intel Pentium III with Windows > XP. I get the same results with a AMD Duron with Windows 98. > > Under LINUX compiled for i586, compiling and running my program > with gcc returns "equal", the correct answer. > > Thanks. Please post your answers or results. . 0