How to handle big numbers?

前端 未结 6 2066
独厮守ぢ
独厮守ぢ 2020-12-18 12:34

I have a very large number, assume some transaction id or big money involved. So, how I will handle the calculation on these ( add, multiple etc). Does any other was to sto

相关标签:
6条回答
  • 2020-12-18 12:59

    If your numbers are larger than int, long or double. Use type long long and do not worry about adding, multiplication etc.

    • long long largenum = 100000000000LL
    0 讨论(0)
  • 2020-12-18 13:00

    You use a library that handles big numbers, like The GNU Multiple Precision Arithmetic Library which seems to be the most common. Or if you want Boost specifically, there's always the Multiprecision library (which can use GMP as backend).

    0 讨论(0)
  • 2020-12-18 13:03

    The closest to a standard is The GNU Multiple Precision Arithmetic Library.

    0 讨论(0)
  • 2020-12-18 13:08

    Please check boost multiprecision library .. It will be handy if your project is already using boost. boost multiprecision library

    0 讨论(0)
  • 2020-12-18 13:08

    you can Wrote a large number of classes handle the calculation. use the Array of characters store your data.

    0 讨论(0)
  • 2020-12-18 13:16

    For This Question We Should Create a
    Divided And Conquer Recursive Function And I Show You
    How You can Do That :

    Problem = Multiple U*V Tow 100 Digit numbers ..
    Function Output = (Prod) Return U*V
    Long Long Long Integer Numbers ...
    Like 9999999999999999*99999999999999999
    Create Class Like That in your Programming Language ..

    Long.Integer Prod(large.integer u , large.integer v)
    {
    large.integer x,y,w,z;
    int m,n;
    n = max("Digits of Number" u , "Digits Of Number" v)
    if(u == 0 || v == 0)
    {
    return 0;
    }
    else if (n {
    return (u*v);
    }else
    {
    m= n/2; // n = Digits Of Number u
    x= u div (10^m);
    y= u rem (10^m);
    w = v div (10^m);
    z= v rem (10^m);
    return prod(x,w) * (10^ (2*m)) + prod(x,z) + prod(w,y) * (10^m) + prod(y,z);
    }
    }
    }

    In This Project t(n) = 4t(n/2) + CN !!

    Sample For Tow Little Number In Mathematica :
    4795 * 2412 = ?
    n =4 (Digits of 4795)

    Result :

    (47 * 10^2 +95) * (24 * 10^2 + 12) =>
    => (47 * 24 * 10^4) + (47 * 12 * 10^2)+ (95 * 24 * 10^2) + 95 *12
    => 47 *24 * 10^4 + 47*12 + 95 * 24 + 10^2 + 95 * 12 = Result Of Function

    Best Regards Aj.Duende (Persian Guy)

    0 讨论(0)
提交回复
热议问题