I would like to ask you a question in C language.

Asked 2 years ago, Updated 2 years ago, 86 views

int main(void) { double x; x = (1.0e17 + 5.0) - 1.0e17; printf("%lf \n", x); return 0; }

int main(void) { double x; x = (1.0e16 + 5.0) - 1.0e16; printf("%lf \n", x); return 0; }

int main(void) { double x; x = (1.0e15 + 5.0) - 1.0e15; printf("%lf \n", x); return 0; }

When EXP is on the 17th, 0 will be printed When EXP is 16, 4 is printed out When the EXP is 15, 5 is output.

It seems to be output normally when it is EXP15, but why does the value come out weird from above 16?

double

2022-09-22 12:39

1 Answers

Here's what Wikipedia says.

Just as 1.0e1 is a two-digit number with 10, for 1.0e15, 16 digits for 1.0e15, 17 digits for 1.0e16, and 18 digits for 1.0e17. So it's beyond the scope of what Double can handle. This phenomenon is often referred to as overflow. When this overflow occurs, the value changes randomly.(Of course, it depends on the mod's rules.)

If you look for the rest more about overflow, it would be helpful.

Wikipedia links corresponding to the image above.

(https://ko.wikipedia.org/wiki/C_%EC%96%B8%EC%96%B4_%EC%8B%A4%EC%88%98%ED%98%95_%EB%B3%80%EC%88%98)


2022-09-22 12:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.