1

I found a strange behavior of std::cin (using VS 17 under Win 10): when a large number is entered, the number that std::cin is reading is close but different. Is there any kind of approximation done with large numbers ? How to get the exact same large number than entered ?

double n(0);

cout << "Enter a number > 0 (0 to exit): ";
cin.clear();                                // does not help !
cin >> n;                                   // enter 2361235441021745907775
printf("Selected number %.0f \n", n);       // 2361235441021746151424 is processed ?.

Output

Enter a number > 0 (0 to exit):
2361235441021745907775
Selected number 2361235441021746151424
pia51
  • 13
  • 2
  • Is the goal of your program to work with big integers? If so, then using `double` isn't going to work for numbers this large. – PaulMcKenzie Mar 15 '22 at 12:56
  • The problem is that the number you entered has no exact representation as a `double`. – molbdnilo Mar 15 '22 at 12:57
  • Any [decent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) should have information about types the their *limits*. All natural types in C++ are limited in what they can store. – Some programmer dude Mar 15 '22 at 12:57
  • 1
    `double` only has ~16 significant digits that it can store. – NathanOliver Mar 15 '22 at 12:57
  • 1
    *How to get the exact same large number than entered ?* -- You need to tell us what high-level goal you are trying to accomplish with numbers being exact. As stated in the first comment, if the goal is to work with large *integer* numbers, using `double` is a non-starter. You should use a "big integer" library or class. – PaulMcKenzie Mar 15 '22 at 13:06
  • 1
    https://stackoverflow.com/questions/588004/is-floating-point-math-broken – stark Mar 15 '22 at 13:25
  • 1
    Well thanks to all for your comments. I actually want to use very large integers for which long is not enough. I need this for an programming exercice on the Collatz conjecture. What prevent a double to be a very large integer ? It does not explain why the cin is getting a wrong number. – pia51 Mar 15 '22 at 13:45
  • Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). – François Andrieux Mar 15 '22 at 14:34

1 Answers1

3

You need to learn about number of significant digits. A double can hold very large values, but it will only handle so many digits. Do a search for C++ doubles significant digits and read any of the 400 web pages that talk about it.

If you need more digits than that, you need to use something other than double. If you know it's an integer, not floating point, you can use long long int which is at least 8 bytes and can hold 2^63-1. If you know it's a positive number, you can make it unsigned long long int and you get the range 0 to at least 18,446,744,073,709,551,615 (2^64-1).

If you need even more digits, you need to find a library that supports arbitrarily long integers. A google for c++ arbitrary precision integer will give you some guidance.

MSalters
  • 173,980
  • 10
  • 155
  • 350
Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
  • Thanks a lot. That was very helpful. Got it. Probably unsigned long long int should suffice. – pia51 Mar 15 '22 at 13:58