floating point

E

edward.birch

The follwoing program produces "6" when compiled/run under MSDEV
2005/Windows and
"6" when compiled/run under g++/Linux.

Anyone have any ideas on how to make MSDEV behave the same as Linux.
--
#include <stdio.h>
int main(int argc, char * argv[])
{
double v = 0.6;
printf("%d\n", int(v * 10.0));
return 0;
}
MSDEV 2005/Windows ==> 6
MSDEV 2003/Windows ==> 6
g++/Linux ==> 5
 
P

Peter Jansson

The follwoing program produces "6" when compiled/run under MSDEV
2005/Windows and
"6" when compiled/run under g++/Linux.

Anyone have any ideas on how to make MSDEV behave the same as Linux.
--
#include <stdio.h>
int main(int argc, char * argv[])
{
double v = 0.6;
printf("%d\n", int(v * 10.0));
return 0;
}
MSDEV 2005/Windows ==> 6
MSDEV 2003/Windows ==> 6
g++/Linux ==> 5

Why convert the (v*10.0) expression to an int by truncating? I suggest
you consider to printf a double value instead of an int. Or perhaps v
should an int (6) and you could printf (v*1)=v instead?


Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
 
J

Jim Langston

The follwoing program produces "6" when compiled/run under MSDEV
2005/Windows and
"6" when compiled/run under g++/Linux.

Anyone have any ideas on how to make MSDEV behave the same as Linux.
--
#include <stdio.h>
int main(int argc, char * argv[])
{
double v = 0.6;
printf("%d\n", int(v * 10.0));
return 0;
}
MSDEV 2005/Windows ==> 6
MSDEV 2003/Windows ==> 6
g++/Linux ==> 5

Rounding issue. 0.6 can probably not be precisly represented in a float.
Perhaps it is .599999 or something similar. The "normal" way to get rid of
this problem is to round the number instead of truncate it. Rounding is
done by adding (for a whole number) .5. In your case you would want to add
0.05. This should produce the same number in all 3 compilers (though not
tested):

#include <stdio.h>
int main(int argc, char * argv[])
{
double v = 0.6;
printf("%d\n", int( ( v + 0.05 ) * 10.0));
return 0;
}
 
M

Michiel.Salters

Jim said:
#include <stdio.h>
int main(int argc, char * argv[])
{
double v = 0.6;
printf("%d\n", int(v * 10.0));
return 0;
}
MSDEV 2005/Windows ==> 6
MSDEV 2003/Windows ==> 6
g++/Linux ==> 5

Rounding issue. 0.6 can probably not be precisly represented in a float.
(snip) Certainly not, as it is 3/5. 5 is not a power of two.
This should produce the same number in all 3 compilers (though not
tested):

#include <stdio.h>
int main(int argc, char * argv[])
{
double v = 0.6;
printf("%d\n", int( ( v + 0.05 ) * 10.0));
return 0;
}

Obviously, that will work, but so will printf("%d\n", 6);
I guess he really wants int (v*10.0 + 0.5). And even then he should
be aware that the result is unpredictable if v==0.65

The +0.5 trick is a trick to change round-down to round-to-nearest, and
works at the end of any floating-point expression. Adding +0.05 to an
intermediate result is not a generic solution.

HTH,
Michiel Salters
 
K

Kevin Handy

Jim said:
(snip)
#include <stdio.h>
int main(int argc, char * argv[])
{
double v = 0.6;
printf("%d\n", int(v * 10.0));
return 0;
}
MSDEV 2005/Windows ==> 6
MSDEV 2003/Windows ==> 6
g++/Linux ==> 5

Rounding issue. 0.6 can probably not be precisly represented in a float.

(snip) Certainly not, as it is 3/5. 5 is not a power of two.

This should produce the same number in all 3 compilers (though not
tested):

#include <stdio.h>
int main(int argc, char * argv[])
{
double v = 0.6;
printf("%d\n", int( ( v + 0.05 ) * 10.0));
return 0;
}


Obviously, that will work, but so will printf("%d\n", 6);
I guess he really wants int (v*10.0 + 0.5). And even then he should
be aware that the result is unpredictable if v==0.65

The +0.5 trick is a trick to change round-down to round-to-nearest, and
works at the end of any floating-point expression. Adding +0.05 to an
intermediate result is not a generic solution.
The +.05 trick doesn't always round to nearest.
Try it with 'v=-0.6' and see what happens.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top