-2
#include <stdio.h>
#include <conio.h>
#include <math.h>   
float t, delt = 0.004000,n;

printf("enter the value for t=");
scanf("%f", &t);
n = t/ delt;
printf("n is %f",n);
getch();

output: enter the value for t=1 n is 249.999985

I am expecting 250. What am I doing wrong??

1 Answers1

3

You are up against the all-too-common problem that floating point representations are approximations. You may wish to do one of the following:

  • Round your answer using an unbiased rounding strategy.
  • Truncate your answer to zero decimal places before printing it (using something like %.0f).
  • Use an arbitrary-precision arithmetic library such as GMP.

Which one of these is sensible is entirely dependent on the constraints of your application.

Gian
  • 13,735
  • 44
  • 51