0
 long double a = 497;
 long double b = 951258;
 long double c = 392673418417;
 v = a * b * c;

when I watch the value of v, it is 185646264136289737248, why does this happen? the correct value should be 185646264136289737242.

winston.xie
  • 81
  • 1
  • 13
  • 2
    check this topic https://stackoverflow.com/questions/14637621/c-calculating-more-precise-than-double-or-long-double – Paulo Pereira Mar 27 '22 at 03:36
  • 497 is a 9-bit number, 951258 is a 20-bit one and 392673418417 is a 39-bit one. Multiplying them produces a 68-bit number which obviously doesn't fit in your `long double` as it doesn't have that much precision bits – phuclv Mar 27 '22 at 06:18

1 Answers1

0

This is due to the fact that doubles can't represent all possible values. If you need that precision and you are only dealing with integers then use int or long long int.

If you want to know more about it you can google it or read this article.

FogRex
  • 46
  • 4
  • The max value of long long int is only 9223372036854775807 which cannot store the value 185646264136289737242, is there any way we can store the big int value? – winston.xie Mar 27 '22 at 03:32
  • 2
    Have a look at [this question](https://stackoverflow.com/questions/12988099/big-numbers-library-in-c) – FogRex Mar 27 '22 at 03:38
  • @winston.xie You are not getting extra precision from long double (over a 64 bit int) if it uses 80 bit hardware. Related: [https://en.wikipedia.org/wiki/Extended_precision#x86_extended_precision_format](https://en.wikipedia.org/wiki/Extended_precision#x86_extended_precision_format) – drescherjm Mar 27 '22 at 03:44