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

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

## Recommended Posts

I can't figure this out. I think it is from roundoff error, but I don't know how to correct it.

##### Share on other sites
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 on other sites
thanks. that works

-Garrett(Squicklid)

##### 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 on other sites
MaulingMonkey is right. [wink]

##### Share on other sites
..thanks.. got it.

##### Share on other sites
Quote:
 .7 * 3 = ~2.1.3 * 3 = ~0.9~2.1 + ~0.9 = 2.99999int(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 on other sites
Quote:
 Original post by keen.7*3 equals exactly 2.1and .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 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 on other sites
Quote:
Original post by _DarkWIng_
Quote:
 Original post by keen.7*3 equals exactly 2.1and .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

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633736
• Total Posts
3013601
×