0
#include<stdio.h>
void main()
{
 float x=1.1;
 while(x==1.1)
 {
     printf("\n%f",x);
     x=x-0.1;
 }
}

I have learnt that while loop can also involve floating point numbers but the above program doesn't show any output. Why is it so ?

Aksh
  • 71
  • 1
  • 7
  • 1
    Hint: `1.1` is a `double`, not a `float`. Watch out for implicit conversions. – Carl Norum Jan 17 '18 at 16:08
  • 1
    `==` is almost useless for floating point types. – Christian Gibbons Jan 17 '18 at 16:27
  • 2
    @dbush: Please do not promiscuously mark floating-point questions as a duplicate of [that question](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). Floating-point has numerous properties and features that can be individually questioned and answered. This question has source code essentially identical to [this question](https://stackoverflow.com/questions/18986141/why-does-this-program-gives-no-output-for-float-and-double-datatypes), which is a **much** more suitable original. – Eric Postpischil Jan 17 '18 at 22:30
  • 2
    Assuming IEEE 754 binary floating point arithmetic, `x` is the closest 32-bit approximation to 1.1, 1.10000002384185791015625. `1.1` is the closest 64-bit approximation to 1.1, 1.100000000000000088817841970012523233890533447265625. They are not equal. – Patricia Shanahan Jan 17 '18 at 23:12
  • @EricPostpischil Not only the source code, the question itself is the same. It looks like both posters had the same homework. – Bob__ Jan 18 '18 at 00:06
  • @Bob__: 1.1 is popular: [1](https://stackoverflow.com/questions/17343155/double-and-float-comparison), [2](https://stackoverflow.com/questions/32066576/float-variable-comparison-fails), [3](https://stackoverflow.com/questions/41540392/c-programming-anomaly-behaviour-of-while-loop-for-float-condition), [4](https://stackoverflow.com/questions/9014303/comparing-float-and-double). – Eric Postpischil Jan 18 '18 at 01:09

0 Answers0