C – Why is PRINTF round floating point number?

I am trying to use printf to print some floating point numbers.
For example:

int main()
{
printf("%.1f",76.75);
return 0;
}

Output: 76.8

I have some results Question.

First of all, why not print 76.7?

Second, how does it revolve around this number?

In addition to the existing answers, please note that many C compilers try to follow IEEE 754 floating point issues IEEE 754 recommends rounding the conversion from binary floating point to decimal according to the current rounding mode. The default rounding mode is “Round to the nearest and related to even numbers”. Some compilation platforms do not consider the rounding mode and are The conversion from floating point to decimal is always rounded according to the default nearest even number mode.

Since 76.75 completely represents 7675/100, it happens to be between 76.7 and 76.8. When applying “round to Nearest-even”, the next number is considered “even”. This may be the reason why your compilation platform chose to generate this decimal representation as the decimal conversion of the floating point number 76.75.

I tried to use printf to print some floating point numbers.
For example:

int main()
{
printf( "%.1f",76.75);
return 0;
}

Output: 76.8

I have some questions about the result.

< p>First of all, why not print 76.7?

Second, how does it revolve around this number?

In addition to the existing answers, please note that many C compilers try to follow IEEE 754 floating point issues. IEEE 754 recommends rounding from the current rounding mode Conversion from binary floating point to decimal. The default rounding mode is “round to nearest and related to even numbers”. Some compilation platforms do not consider the rounding mode, and the conversion from floating point to decimal is always based on the default nearest Even number mode is rounded.

Since 76.75 completely represents 7675/100, it happens to be between 76.7 and 76.8. When “round to nearest-even” is applied, the latter number is considered to be “Even number”. This may be the reason why your compilation platform chose to generate this decimal representation as the decimal conversion of the floating point number 76.75.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 376 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.