1. How when dividing two numbers to output 40 characters after the point?
  2. Is it possible to save these 40 characters in a variable?
  • one
    The easiest way to implement the division in the column ... - knes
  • Well, so right now? can a person decide to calculate pi number ^^ - Zowie
  • dada =)))) - nullptr
  • one
    Pi is irrational, which means that it cannot be represented as a quotient from dividing two numbers - renegator

3 answers 3

Formally, like this:

std::cout << std::fixed << std::setprecision(40) << 1.5 

But the problem is that it does not make sense, because double, much less float, does not store numbers with such high precision.

For example, in the double type, 52 bits are reserved for storing the mantissa. 2 ^ 52 is equal to about 4 * 10 ^ 15, so the maximum number of decimal places that a double can store, according to this rough estimate, does not exceed 16.

    Look for example (the first in Google in c ++ biginteger):

    https://mattmccutchen.net/bigint/

       int main(int argc, char *argv[]) { int a=171, b=37, i, r; printf("\n%.17lf", (double) a/b); printf("\n%d,", a/b); r = a; for ( i=0; i < 40; i++) { r = (rb*(r/b)) * 10; printf("%d", r/b); } return 0; 

      }

      • Very interesting, only ** is it possible to believe these discharges? ** By the way, explain the following result of your program: int a = 1171, b = 377, i, r; 3.10610079575596830 3.1061007957559681697612732095490716180371 You see, the last 2 digits (well, I would agree with one more) with the floating-point division no longer coincide with those calculated in your iteration. - avp
      • one
        who has at hand mathematics, maslab, etc., can check in them. if not, go for example to tungsten and enter there "= 1171/377 " wolframalpha.com/input/?i=%3D1171%2F377 you need to believe, because the algorithm is correct - renegator
      • one
        the algorithm itself is simple - the representation of the number a in the form of division with the remainder: a = b * n + r. n is the next digit in the representation of a number, and the remainder r is decomposed according to the same principle - renegator
      • Those. the results do not coincide with the correct algorithm due to Intel errors (x86 processor)? Or does gcc crookedly compile the right program ? - avp
      • one
        the quotient of two numbers is a mathematical construct. in the form of the formula a \ b, it is displayed ideally, and in the form of numbers - with some approximation. the processor is not mistaken, just its limit of approximation is limited - renegator