I am struck with some floating-point precision. Please help me to get through this.

main()
{
        int fact,i=5,div=8;
        float dec=0.427,temp,sum=0;

while(i)
					{
                     temp=dec*10;  printf("%f\t",temp);
                     fact=(int)temp;
                  	 sum=sum+(fact/(float)div);	printf("%f\n",sum);
 					 dec=temp-fact;
                     div=div*8;
					 i--;
					}

	  printf("\n The Decimal part= %f",sum);
getch();
}
Output
4.270000         0.500000
2.700000         0.531250
6.999998         0.542969
9.999981         0.545166
9.999809         0.545441

 The Decimal part= 0.545441

Thanks in advance :)

Recommended Answers

All 12 Replies

What results were you expecting?

Are you serious "Narue"?

What results were you expecting?

I was expecting the third line in o/p as 7.000000 not 6.999998

Are you serious "Narue"?

Yes, of course. You posted code and no real question. Were you expecting us to be psychic?

I was expecting the third line in o/p as 7.000000 not 6.999998

That's a typical precision error in floating-point. If you want to round, it's easily done with a utility function:

double round(double x)
{
    return x > 0 ? floor(x + 0.5) : ceil(x - 0.5);
}

Alternatively, you can add a precision to your format specifier and printf will round it properly for display. For example, "%.4f" will round to four places. But due to increasing imprecision, you still might not get the results you're expecting.

Narue u r kind enough to reply me!
But I really don't know how to put ur code to make my program work.
Can I ask u to help me with full working codes?
Please I've just started learning C.:-/

Member Avatar for b1izzard
#include<stdio.h>
#include<math.h>
double round(double x)
{
    return x > 0 ? floor(x + 0.5) : ceil(x - 0.5);
}

int main()
{
    int fact,i=5;
    float dec=0.427,temp,sum=0,div=8;
	while(i)
	    {
			temp=dec*10;  
			printf("%f\t",round(temp));
			fact=(int)temp;
			sum=sum+(fact/div);	
			printf("%f\n",sum);
			dec=temp-fact;
			div=div*8;
			i--;
	   }
    printf("\n The Decimal part= %f",sum);	
    return 0;
}

Thanks ravi for your reply but, I am not getting the desired answer!
The sum result should be 0.544921875.
there is no change in the result even after the insertion of the code..

Then convert all values into individual digits in an integer array. Then you can ignore the fact that floating point values are not exact.

Then convert all values into individual digits in an integer array. Then you can ignore the fact that floating point values are not exact.

Walt please help how to apply your solution with the full C source code.
Please modify my C source code.

Walt please help how to apply your solution with the full C source code.
Please modify my C source code.

You want me to write the full C source code for you? I can send you a contract and you can hire me as your personal programmer. I'll give you the student rate of $50 and hour, minimum 10 hours.

Thats very tempting offer but I'll pass!
Is there anybody who is kind enough to help me...

I will still help. But you must do the work. What part of my suggestion do you not understand?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.