Advertisement
Shaun_B

The problem with floating point numbers.

Sep 11th, 2012
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.     int i;              // Not initialised at start up.
  7.     float r=0.0;        // Floating point number initialised to zero.
  8.     printf("This will run a loop while r is less than 1.8 but it doesn't work,"
  9.            "\nso why does this happen and how do you fix it?"
  10.            "\n\n");
  11.     printf("Vaules of r and i before loop:\ni = %d r = %f"
  12.            "\n\n",i,r);
  13.     printf("And here's the values outputted during the loop:"
  14.            "\n\n");
  15.     while(r<1.8)
  16.     {
  17.         r+=0.3;
  18.         i=r;
  19.         printf(" i = %d r = %f\n", i, r);
  20.     }
  21.     _getch();
  22.     return 0;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement