0

I add a float variable A values 1.2345E3 to a double variable B values 1.5E100 in c language, when I checkout the answer, I found that the answer is the same with variable B, although I got the answer to 200 decimal places, there's not any trace of A, the answer is the duplication of b.

Within what I have learned, when a float variable adds a double variable, the float one will automatically convert it's type to double, so A, or 1.2345E3 will convert to double before the addtion with B, and get the answer.

So why the addtion answer is the duplication of B in 200 decimal places, will float automatically convert to double during the addtion?

Cor Le
  • 15
  • 4
JNPW
  • 11
  • 1
  • 1
    `float` doesn't have 100 digits of precision, so the small `A` value is not large enough to make a difference when adding to `B` – Barmar Aug 01 '22 at 15:23
  • 1
    Assuming a 64-bit, IEEE-754 `double`, then that can only hold values to approximately 17 significant (decimal) figures. Adding your `A` to `B` won't change anything, because it is *well* below that 17-figure limit. – Adrian Mole Aug 01 '22 at 15:23
  • 1
    The "exact" result of your addition would be 1500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001234.5. Double doesn't have nearly enough precision to handle that without rounding off the less significant digits. – slothrop Aug 01 '22 at 15:24
  • 1
    There are several *big number* libraries that provide support for numbers of arbitrary precision. They could accommodate 200 digits of precision. – Eljay Aug 01 '22 at 15:24
  • You have just learned a vital lesson about ordinary computer floating-point arithmetic: it has *finite precision*. You said "although I got the answer to 200 decimal places", but I guess you mean, you *asked for it to be printed* to 200 places. But that doesn't mean it has 200 places internally. Just about any floating-point format you're likely to use has much, much less precision than that internally, which is why `1.5E100 + 1.2345E3 == 1.5E100`. The exact result simply can't be represented, so the result you get is rounded off to the nearest representable result, which is the same as B. – Steve Summit Aug 01 '22 at 15:53
  • Yes, typically type `float` is bumped up to `double` during many operations, but that only increases your precision from 24 bits to 53 bits (or, in decimal terms, roughly from 7 digits to 16 digits). It doesn't get you anywhere near to 100 decimal digits. 100 decimal digits of precision would require more than 300 bits. – Steve Summit Aug 01 '22 at 15:56
  • Please see [Why Are Floating Point Numbers Inaccurate?](https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) – Weather Vane Aug 01 '22 at 16:26
  • 2
    Thank everyone for let me know what I got stuck in. When I ask the question, I know that in IEEE754, type "double" has a range of about 5e-324 to 2e308, so I'm confused that why double can't handle 1.2345E3 add 1.5E100. Now when I checkout IEEE754 again, I found that the accurate value that "double" can present a "accurate range" about 2^-52 to 2^52. With the multiplication of power of 2, it has the very large range I knew. When addtion of 1.2345E3 add 1.5E100 happens, 1.2345E3 become very small to 1.5E100, which cause the "double" can't really present 1.2345E3 with it's "accurate range". – JNPW Aug 02 '22 at 02:20
  • 1
    Or simply, when the addition happens, 1.2345 in binary will move 97 digits, which is overflowed. – JNPW Aug 02 '22 at 02:28

0 Answers0