简体   繁体   中英

Shows a different result when divided by float 1 and int 1

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM