0

Recently while practicing I found this problem, Please run this code in your local machine.

#include <bits/stdc++.h>
using namespace std;
double power(double a, double b) {
    double ans = 1;
    for(int i = 1; i <= b; i++){
        ans *= a;
    }
    return ans;
}

int main()
{
    double anaconda = 0;
    for(int i = 0; i < 25; i++){
        anaconda += power(10,i);
        cout << setprecision(30) << fixed <<i << " "<< anaconda << endl;
    }
}

If you run this code you should see something like this

output

Your can also see this code running in IDEONE You can see there from i = 16 , the program is giving wrong output. There should be all 1s in every number. Can anyone please tell me

  • Why this is giving wrong result even in this small range ?
  • And how to avoid this type of calculation problem ?
    Thanks in advance ❤️❤️❤️
  • 4
    `std::numeric_limits::digits10` is `15`. Your range is not "small". You need to use a wider type, do you really need more than 15 significant digits? – 463035818_is_not_an_ai Aug 19 '21 at 10:37
  • 2
    btw calling `power(10,i)` in each iteration is a waste of cpu. You only need to multiply `10` in each iteration instead of starting from `1` every time – 463035818_is_not_an_ai Aug 19 '21 at 10:39
  • 4
    A `double` has only 15 digits of decimal precision - https://en.wikipedia.org/wiki/Double-precision_floating-point_format#IEEE_754_double-precision_binary_floating-point_format:_binary64 - setting the display precision doesn't affect the internal representation. – gavinb Aug 19 '21 at 10:42
  • 1
    This "practice" looks like a coding puzzle that came from a web site that has lists of random coding puzzles. Unfortunately, those web sites are just lists of puzzles and don't have any actual C++ tutorials or any learning material that explain fundamental C++ concepts, like floating point precision, why floating point math is broken, etc. That can only be found in a good textbook. Someone who wants to learn C++ is not going to learn very much by trying to solve programming puzzles, but only by following an organized, structured, curriculum from a quality C++ textbook. – Sam Varshavchik Aug 19 '21 at 10:45
  • 2
    to get higher precision, use a third party library for high precision math – M.M Aug 19 '21 at 10:57
  • 1
    Ashiqur Rahman "There should be all 1s in every number. " makes sense if `double` could encode **every** number. As a 64-bit floating point number, only about 2^64 numbers are encodeable. Something has to give. – chux - Reinstate Monica Aug 19 '21 at 11:02
  • 1
    duplicates: [Why IEEE754 single-precision float has only 7 digit precision?](https://stackoverflow.com/q/19130396/995714), [Double precision - decimal places](https://stackoverflow.com/q/9999221/995714), [Precision loss with double C++](https://stackoverflow.com/q/12096852/995714), [Losing precision with floating point numbers (double) in c++](https://stackoverflow.com/q/48148235/995714) – phuclv Aug 19 '21 at 13:31

1 Answers1

2

The C++ double type is typically stored (in memory) in 64-bit "double-precision floating-point" format.

While this allows you to represent and calculate on both very large, and very small, numbers (relative to other common c++ datatypes) it only does this with a certain precision.

This means that you can only rely on 15-17 digits being exact. Anything beyond that is the result of rounding to the nearest value that can be represented in the specific amount of memory (again, typically 64-bits).

I hope this answers the first question. There's much more to learn about this, for example in this Wikipedia article: https://en.wikipedia.org/wiki/Double-precision_floating-point_format

As for the second question, the answer is that it depends. It depends on just how big the numbers you want to represent are.

For example, if you are just working with positive integers, you can extend the range somewhat by switching to unsigned long long int which get's you to 18,446,744,073,709,551,615 (not quite enough for the code at hand, so I'm mostly mentioning it for completeness).

If that's not sufficient you probably want to be looking for a c++ library for big numbers.

Google c++ big number library and/or c++ big integer library.

marpor
  • 153
  • 3