0

This program was supposed to output zero digits after its decimal point when i is an integer or has no non-zero digit after decimal point. It works for all the cases just not for the last. Can anyone help me fix this?

Code :

#include <stdio.h>
#include<math.h>
int main()
{
    double i,j,k;
    for(i=0;i<=2;i=i+0.2)
    {
        k=3;
        j=i+1;
        while(k--)
        {
            if(fmod(i,1)==0) printf("I=%.0lf J=%.0lf\n",i,j);
            else printf("I=%.1lf J=%.1lf\n",i,j);
            j++;
        }
    }
}

Here's the ideone sample execution

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • please provide excepted output. i think i < 3 in for can be respond. https://ideone.com/8rPVHu – Sanpas Feb 18 '19 at 15:53
  • 3
    Please include the code rather than a link. – Paul Floyd Feb 18 '19 at 16:14
  • I think you need `remainder` instead of `fmod`. – Joseph Wood Feb 18 '19 at 21:46
  • Are you aware that i will never equal 2? Iterate in integer from 0 to 1000 and divide by 5.0 instead – phuclv Feb 19 '19 at 00:50
  • @phuclv i will never equal 2? "Iterate in integer from 0 to 1000 and divide by 5.0 instead" I'm sorry, I dont understand this. Can you break it down? – GeinRovaan Feb 19 '19 at 14:36
  • [`i` will never be equal to 2](https://ideone.com/7VRXkw), because there was no floating-point value equal to 0.2 to begin with, and as you add it the error will accumulate, making the result drifting further from the correct value. See [Is floating point math broken?](https://stackoverflow.com/q/588004/995714). For simple cases like this use `for (int i = 0; i < 10; i++) { printf("%f\n", i/5.0; }`. And put spaces around operators and parentheses to make it readable – phuclv Feb 19 '19 at 14:52
  • Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – phuclv Feb 19 '19 at 14:53
  • some exact duplicates of yours: [Why does this loop never end?](https://stackoverflow.com/q/3351148/995714), [Java the for loop continues to execute even after the condition is fulfilled](https://stackoverflow.com/q/15273447/995714), [Why "for( i = 0.1 ; i != 1.0 ; i += 0.1)" doesn't break at i = 1.0?](https://stackoverflow.com/q/13542220/995714), [Strange loop on Java for 0.1 to 2](https://stackoverflow.com/q/5400565/995714) – phuclv Feb 19 '19 at 14:54

1 Answers1

0

Depending on your precision level, you can use the %g or %lg like below to print just the shortest representable item. In this, I use the .5 size.

#include <stdio.h>
#include<math.h>
int main()
{
    double i,j,k;
    for(i=0;i<=2;i=i+0.2)
    {
        k=3;
        j=i+1;
        while(k--)
        {
            if(fmod(i,1)==0)
            {
                printf("I=%.5lg J=%.5lg\n",i,j);
            }
            else
            {
                printf("I=%.5lg J=%.5lg\n",i,j);
            }
            j++;
        }
    }
}
johnnyb
  • 1,745
  • 3
  • 17
  • 47
  • Thank you so much. Ya, this's one way to do it. But couldn't understand why my code didn't work. – GeinRovaan Feb 19 '19 at 14:30
  • doesn't work. It still won't print the exact 2, simply because `0.2+0.2+0.2+0.2+0.2` doesn't yield 1 https://ideone.com/PNCcd7 – phuclv Feb 21 '19 at 00:03