By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

floating point

P: n/a
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

Jun 17 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
ed**********@gmail.com wrote:
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/
Jun 17 '06 #2

P: n/a
<ed**********@gmail.com> wrote in message
news:11********************@r2g2000cwb.googlegroup s.com...
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;
}
Jun 17 '06 #3

P: n/a

Jim Langston wrote:
<ed**********@gmail.com> wrote in message
news:11********************@r2g2000cwb.googlegroup s.com... (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.

HTH,
Michiel Salters

Jun 19 '06 #4

P: n/a
Mi*************@tomtom.com wrote:
Jim Langston wrote:
<ed**********@gmail.com> wrote in message
news:11********************@r2g2000cwb.googlegro ups.com...


(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.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jun 22 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.