cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
If statement, float comparison ERROR
If statement, float comparison ERROR
Dec 4, 2015 at 2:47pm UTC
Fl4v10
(1)
Hi,
I am using an if statement to compare 2 floats. You can see the code below.
I have the VE class, where x is a float.
In this function a ve_in struct enters
if (VE[i].x != ((float)ve_in[i].x)/1000.00)
{
printf("Xold = %f , Xnew = %f\n",VE[i].x, ((float)ve_in[i].x)/1000.00);
VE[i].x = ((float)ve_in[i].x)/1000.00;
}
Even if it prints that the values are equal, it still enter in the statement.
Can you give me some advice?
Thanks!
Dec 4, 2015 at 3:16pm UTC
cire
(8284)
Even if it prints that the values are equal, it still enter in the statement.
Try:
printf(
"Xold = %f.20 , Xnew = %f.20\n"
,VE[i].x, ((
float
)ve_in[i].x)/1000.00);
printf
limits decimal precision to 6 by default (which means you get a truncated value) and
float
arguments are promoted to
double
when fed to
printf
You may also find the following link of interest:
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
Topic archived. No new replies allowed.