-1

As we know, data type float in C comply with IEEE754. And float has 24 significant binary digits. Therefore, if it exceeds 24 bits, accuracy loss will occur.

In that way, how many decimal digits can float represent at most without loss of accuracy? And What's that maximum decimal number?

zhenyoung
  • 21
  • 3
  • 1
    There are about 3.3 bits per decimal digit. `=ln(10)/ln(2)` – stark Jul 28 '22 at 11:35
  • 1
    You might first take a look at [Wikipedia: Single-precision floating-point format](https://en.wikipedia.org/wiki/Single-precision_floating-point_format). It contains information about numbers that can be represented accurately. – Gerhardh Jul 28 '22 at 11:35
  • See [Is floating-point math broken?](https://stackoverflow.com/q/588004/15168) – Jonathan Leffler Jul 28 '22 at 14:39
  • Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – pringi Jul 28 '22 at 22:54

1 Answers1

2

As we know, data type float in C comply with IEEE754.

The C standard does not require this. Many C implementations use IEEE-754 formats for floating-point types but do not fully comply with the standard. Some C implementations use formats for floating-point types other than the IEEE-754 types.

In that way, how many decimal digits can float represent at most without loss of accuracy?

This is not a clear question to ask. Consider the number 0.1. As a decimal numeral, it is represented with a single digit, but, as a binary numeral, it cannot be represented with any finite number of digits. It would be .0001100110011001100…

The IEEE-754 binary32 format, commonly used for float, has sufficient precision that, if any decimal numeral with six significant digits is converted to the nearest value representable in then binary32 format, and that binary32 number is converted back to a decimal numeral rounded to six significant digits, the result will equal the original number, as long as the first conversion did not overflow or underflow the finite range of the format.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Uh, so what's the value of max decimal number which contains six significant digit without overflow? – zhenyoung Jul 28 '22 at 11:51
  • @zhenyoung: That is not a clear question. The maximum finite value representable in binary32 is 2^128−2^104, which is 340282346638528859811704183484516925440. This implies that converting the six-significant-digit decimal numeral 3.40282e+38 `float` and converting the result back to a six-significant-digit decimal numeral yields the same number. However, converting 3.40283e+38 to `float` will overflow. – Eric Postpischil Jul 28 '22 at 11:57