[Issue 5765] ^^ and << with BigInts

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Mar 23 07:00:40 PDT 2011


http://d.puremagic.com/issues/show_bug.cgi?id=5765



--- Comment #5 from Don <clugdbug at yahoo.com.au> 2011-03-23 06:57:19 PDT ---
(In reply to comment #4)
> (In reply to comment #3)
> 
> > The thing I'm really worried about is this:
> > BigInt a, b, c;
> > 
> > a = (a ^^ b) % c;
> > This is an attempt to do modular exponentiation, which comes up frequently in
> > cryptography. The code is mathematically correct, but completely wrong in
> > practice.

> I suggest to add a similar function to std.math or std.bigint. 

Of course, that's what I was implying. Note that it is a totally different
operation to ^^.

> Also the D
> compiler may recognize the (x^^y)%z pattern and replace it with a power mod
> function call.

I think that's asking too much. 
Also note that it doesn't cover the two step pattern where someone does:
x = x ^^ y ;
x %= z;

 > > So I would rather give an informative static assert for the BigInt ^^
BigInt
> > case.
> 
> Python allows you to use the power/shift with multi-precision numbers too, if
> you want. The multi-precision numbers can be used transparently, syntax-wise. 
> If you have D templated code that you want to use with both BigInt and int you
> will have to use a static if to change the code from x^^y to
> BigInt(x)^^y.toInt() (or call a little pow template function that does the
> same, losing infix operator syntax again), this isn't good. 

That's a false consistency. T ^^ int  is the common operation, not T ^^ T.
Really. BigInt ^^ BigInt isn't a BigInt. It's too big to be representable.

> BitInt are meant to
> work as integers, mostly. I'd like a D program to work with as few changes as
> possible if I replace int with BigInts or BigInts with ints (in some situations
> I may write the code with BigInts, see the results and then try to write the
> same with ints/longs, to spot the overflows).

So? These ones which currently don't compile, will definitely cause problems
with ints/longs as well. You're just getting the error message at compile time
rather than at run time.

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


More information about the Digitalmars-d-bugs mailing list