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

Started by
60 comments, last by Zahlman 19 years, 8 months ago
I can't figure this out. I think it is from roundoff error, but I don't know how to correct it.
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
thanks. that works

-Garrett(Squicklid)
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
MaulingMonkey is right. [wink]
..thanks.. got it.
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
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]
You should never let your fears become the boundaries of your dreams.
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
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
"To some its a six-pack, to me it's a support group."

This topic is closed to new replies.

Advertisement