www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5971] New: Some BigInt ideas

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5971

           Summary: Some BigInt ideas
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



[idea 1 and 2] I'd like this very simple code to work:


import std.bigint, std.conv;
void main() {
    text(BigInt(1));
    to!string(BigInt(1));
}

----------------------------

[idea 3] I'd like this very simple code to work, thanks to a  T
opCast(T:bool)(){} member function:

import std.bigint;
void main() {
    BigInt x = BigInt(0);
    if (x) {}
}

----------------------------

(The ideas 4 and 5 have a lower priority, because they are less commonly
useful.)

[idea 4] I'd like this code to work:

import std.bigint;
void main() {
    BigInt b = BigInt(10);
    auto result = b & 1;
}

---------------------

[idea 5] Sometimes I like to know how many decimal digits a BigInt is long.
Currently (DMD 2.053beta) this is the best way I have found to do it:

import std.bigint;
void main() {
    BigInt b = BigInt(91) ^^ 35;
    const(char)[] bstr;
    b.toString((const(char)[] s){ bstr = s; }, "d");
    int ndigits_b = bstr.length;
}


But a specific method avoids to keep all the decimal digits:

import std.bigint;
void main() {
    BigInt b = BigInt(91) ^^ 35;
    int ndigits_b = bstr.numDigits();
}

(Optionally a base for numDigits(), that defaults to 10.)

-----------------------

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 09 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5971




[idea 6] I'd like this code to work:


import std.bigint;
alias BigInt T; // Error
//alias int T; // OK
void main() {
    T x = 1;
    T y, z;
    y = z = 1;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 10 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5971


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug yahoo.com.au



Re idea 5:

To find number of digits of x, use
p = x.ulonglength();
m = peekUlong(p-1);

then number of digits is between
lowerbound = log10(m) + p*log10(ulong.sizeof*8*2).
upperbound = log10(m+1) + p*log10(ulong.sizeof*8*2)

Make sure that there are enough digits in m that 
(upperbound-lowerbound) < 1.

Optionally use m = m*(ulong+1) + peekUlong(p-2), p-- 
to get a better value for m, which will reduce the probability that an exact
calculation must be performed.

If the range includes an exact integer (eg, the range is 1547.8 .. 1548.2) then
it's necessary to do a full calculation.

if (trunc(lowerbound) == trunc(upperbound)) return lowerbound
else
{
    y = 10^^(lowerbound+1);
    if (x < y) return lowerbound;
    return lowerbound+1;
}
The problem is, that if the number is close to 10^^d, there is no way to avoid
the full calculation. And unfortunately, although the hard case it is
numerically rare, it is a very common case in practice.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 22 2012