# slight rounding problem

Discussion in 'C Programming' started by JNY, Jan 11, 2005.

1. ### JNYGuest

Hello,

I've searched for a solution, but can't find one.
When adding two numbers I'm not getting the expected result:

int testInt = 3056;
float testMant = 0.9001;
float testTotal;

testTotal = testInt+testMant;

This is producing 3056.9, not 3056.9001 as I would like. Using
doubles doesn't seem to help. Can anyone else?

JNY, Jan 11, 2005

2. ### Martin AmbuhlGuest

JNY wrote:
> Hello,
>
> I've searched for a solution, but can't find one.
> When adding two numbers I'm not getting the expected result:
>
> int testInt = 3056;
> float testMant = 0.9001;
> float testTotal;
>
> testTotal = testInt+testMant;
>
> This is producing 3056.9, not 3056.9001 as I would like. Using
> doubles doesn't seem to help. Can anyone else?

Three questions
a) Why do you think floats have infinite precision?
b) Why didn't you check the FAQ before posting?
c) Why didn't you follow the newsgroup before posting. In the last week
similar questions were posted by at least three other people clueless
checking the FAQ or following the newsgroup before posting.

See if you can explain this (recompile for the limits of your system):

#include <stdio.h>
#include <float.h>

int main(void)
{
int testInt = 3056;
float testMantf = 0.9001, testTotalf;
double testMant = 0.9001, testTotal;
long double testMantl = 0.9001L, testTotall;

testTotalf = testInt + testMantf;
testTotal = testInt + testMantf;
testTotall = testInt + testMantf;
" %d (int) + %.*g (float) = %.*g (float)\n"
" %d (int) + %.*g (float) = %.*g (double)\n"
" %d (int) + %.*g (float) = %.*Lg (long double)\n\n",
testInt, FLT_DIG, testMantf, FLT_DIG, testTotalf,
testInt, FLT_DIG, testMantf, DBL_DIG, testTotal,
testInt, FLT_DIG, testMantf, LDBL_DIG, testTotall);

testTotalf = testInt + testMant;
testTotal = testInt + testMant;
testTotall = testInt + testMant;
" %d (int) + %.*g (double) = %.*g (float)\n"
" %d (int) + %.*g (double) = %.*g (double)\n"
" %d (int) + %.*g (double) = %.*Lg (long double)\n\n",
testInt, DBL_DIG, testMant, FLT_DIG, testTotalf,
testInt, DBL_DIG, testMant, DBL_DIG, testTotal,
testInt, DBL_DIG, testMant, LDBL_DIG, testTotall);

testTotalf = testInt + testMantl;
testTotal = testInt + testMantl;
testTotall = testInt + testMantl;
" %d (int) + %.*Lg (long double) = %.*g (float)\n"
" %d (int) + %.*Lg (long double) = %.*g (double)\n"
" %d (int) + %.*Lg (long double) = %.*Lg (long double)\n\n",
testInt, LDBL_DIG, testMantl, FLT_DIG, testTotalf,
testInt, LDBL_DIG, testMantl, DBL_DIG, testTotal,
testInt, LDBL_DIG, testMantl, LDBL_DIG, testTotall);
return 0;
}

3056 (int) + 0.9001 (float) = 3056.9 (float)
3056 (int) + 0.9001 (float) = 3056.90014648438 (double)
3056 (int) + 0.9001 (float) = 3056.900146484375 (long double)

3056 (int) + 0.9001 (double) = 3056.9 (float)
3056 (int) + 0.9001 (double) = 3056.9001 (double)
3056 (int) + 0.9001 (double) = 3056.90009999999984 (long double)

3056 (int) + 0.9001 (long double) = 3056.9 (float)
3056 (int) + 0.9001 (long double) = 3056.9001 (double)
3056 (int) + 0.9001 (long double) = 3056.9001 (long double)

Martin Ambuhl, Jan 11, 2005

3. ### Joe WrightGuest

JNY wrote:
> Hello,
>
> I've searched for a solution, but can't find one.
> When adding two numbers I'm not getting the expected result:
>
> int testInt = 3056;
> float testMant = 0.9001;
> float testTotal;
>
> testTotal = testInt+testMant;
>
> This is producing 3056.9, not 3056.9001 as I would like. Using
> doubles doesn't seem to help. Can anyone else?

You need a little more C practice.

#include <stdio.h>
int main(void) {
int testInt = 3056;
float testMant = 0.9001;
float testTotal;
testTotal = testInt+testMant;
printf("testTotal is %.4f\n", testTotal);
return 0;
}

testTotal is 3056.9001 (on my system)

But also, you should be conscious of the fact that floats are binary
and your expected outputs are decimal and that conversions among
number systems are usually inexact.

And more practice is always good.

"Programming is like shaving. If you don't do it every day, you're a
bum."

--
Joe Wright mailto:
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---

Joe Wright, Jan 12, 2005