Jump to content
  • Advertisement
Sign in to follow this  
squicklid

int s = (.7 * 3) + (.3 * 3) is 2 ?

This topic is 5144 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
squicklid: I conqur.

By default I believe float to int conversion rounds down.

Therefore, you probably want "round to nearest" instead. Just add 0.5 to the end of the equasion, and you'll be set.

-Mike

Share this post


Link to post
Share on other sites
Quote:
Original post by MichaelT
.7 * 3 = 2.1

.3 * 3 = 0.9

(int)2.1 = 2;
(int)0.9 = 0;

2 + 0 = 2;


WRONG order of operations for his statement, sorry.

(.7 * 3) gets treated as a float or double (froget which by default).

Next, (.3 * 3) is evaluated (also float or double).

Then, addition is preformed. If (.3 * 3) had been an int, it would be implicitly cast to float. (float * int = float, int * float = int).

What's happening is:

.7 * 3 = ~2.1
.3 * 3 = ~0.9
~2.1 + ~0.9 = 2.99999
int(2.999999) = 2

Share this post


Link to post
Share on other sites
Quote:

.7 * 3 = ~2.1
.3 * 3 = ~0.9
~2.1 + ~0.9 = 2.99999
int(2.999999) = 2

.7*3 equals exactly 2.1
and .3 *3 = exactly 0.9
so 2.1+0.9=3 so that's cant be the case

Share this post


Link to post
Share on other sites
Quote:
Original post by keen
.7*3 equals exactly 2.1
and .3 *3 = exactly 0.9

Nothing is 'exactly' in floating point math. If you want to write 0.9 exacly in FP mathy you need 'exactly' infinite digits. [grin]

Share this post


Link to post
Share on other sites

int main()
{
int a,b;
float c;
a=(.7 * 3) + (.3 * 3);
b=((float)(.7 * 3)) + ((float)(.3 * 3));
c=(.7 * 3) + (.3 * 3);
printf("%i,%i,%f\n",a,b,c);
}

output: 2,3,3.000000

proves my point

Share this post


Link to post
Share on other sites
Quote:
Original post by _DarkWIng_
Quote:
Original post by keen
.7*3 equals exactly 2.1
and .3 *3 = exactly 0.9

Nothing is 'exactly' in floating point math. If you want to write 0.9 exacly in FP mathy you need 'exactly' infinite digits. [grin]


But those numbers are doubles, not floats. I agree with MichaelT on this one.

/MindWipe

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!