0

This is what I am getting and I don't get why this output is coming. when I change float to int it is giving proper output.

int delta=99999997/(float)1;     
cout<<delta<<endl;

Output: 100000000

Image of ide

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 3
    Please provide all your code, inputs, outputs, and errors as text in the post. – rawrex Jun 11 '21 at 11:04
  • 1
    Your number has too many digits, `float` cannot represent it exactly. – n. m. could be an AI Jun 11 '21 at 11:10
  • IEEE-754 single precision float can store about ~7 digits of precision so it can't represent such numbers. [Strange behavior when casting an int to float in C](https://stackoverflow.com/q/27113114/995714) – phuclv Jun 11 '21 at 11:26

1 Answers1

1

When you divide an integer by a floating point number, the integer is converted to a floating-point number.

The reason for the weird result is that 99999997 cannot be represented exactly in a 32 bit floating point number with the precision used by C++’s float on your architecture.

Instead, when converted to a float, this number gets rounded to the next number which is representable. And that number … happens to be 100000000.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214