www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - bigint

reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
why does the following code fail?


import std.bigint;
void main(){
     BigInt b1 = 1;
     BigInt b2 = 3;
     BigInt e1 = 1;
     BigInt e2 = 3;
     BigInt[] b = [b1,b2];
     BigInt[] e = [e1,e2];
     assert(b == e);
}
Nov 26 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
Ellery Newcomer:

 why does the following code fail?
Reduced case for bugzilla: import std.bigint; void main() { assert([BigInt(1)] == [BigInt(1)]); } Bye, bearophile
Nov 27 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
 Reduced case for bugzilla:
http://d.puremagic.com/issues/show_bug.cgi?id=5281
Nov 27 2010
parent reply Matthias Walter <xammy xammy.homelinux.net> writes:
On 11/27/2010 02:05 PM, bearophile wrote:
 Reduced case for bugzilla:
     
http://d.puremagic.com/issues/show_bug.cgi?id=5281
I investigated into the bug and the reason is the signature of opEquals, which currently is bool opEquals(Tdummy=void)(ref const BigInt y) const bool opEquals(T: int)(T y) const The only working sigature for array-of-structs-comparison to work is bool opEquals(ref const BigInt y) const But this removes the ability to compare against ints. (btw, it should probably be long in the 2nd signature?!) array-comparison should definitely take templated opEquals functions into account, or is there something mentioned in the spec? Matthias
Nov 28 2010
parent reply Kagamin <spam here.lot> writes:
Matthias Walter Wrote:

 bool opEquals(Tdummy=void)(ref const BigInt y) const
 bool opEquals(T: int)(T y) const
 
 The only working sigature for array-of-structs-comparison to work is
 
 bool opEquals(ref const BigInt y) const
 
 But this removes the ability to compare against ints.
Why are they templated to begin with? Just for the heck of it? bool opEquals(ref const BigInt y) const bool opEquals(long y) const
Nov 28 2010
parent reply Don <nospam nospam.com> writes:
Kagamin wrote:
 Matthias Walter Wrote:
 
 bool opEquals(Tdummy=void)(ref const BigInt y) const
 bool opEquals(T: int)(T y) const

 The only working sigature for array-of-structs-comparison to work is

 bool opEquals(ref const BigInt y) const

 But this removes the ability to compare against ints.
Why are they templated to begin with? Just for the heck of it? bool opEquals(ref const BigInt y) const bool opEquals(long y) const
No, because then it fails for ulong. It's those bloody C implicit conversions.
Nov 28 2010
parent reply Kagamin <spam here.lot> writes:
Don Wrote:

 Why are they templated to begin with? Just for the heck of it?
 
 bool opEquals(ref const BigInt y) const
 bool opEquals(long y) const
No, because then it fails for ulong. It's those bloody C implicit conversions.
hmm... works for me: --- struct A { bool opEquals(ref const A y) const { return false; } bool opEquals(long y) const { return true; } } int main() { A a; ulong b=42; assert(a==b); return 0; } ---
Nov 29 2010
parent Don <nospam nospam.com> writes:
Kagamin wrote:
 Don Wrote:
 
 Why are they templated to begin with? Just for the heck of it?

 bool opEquals(ref const BigInt y) const
 bool opEquals(long y) const
No, because then it fails for ulong. It's those bloody C implicit conversions.
hmm... works for me: --- struct A { bool opEquals(ref const A y) const { return false; } bool opEquals(long y) const { return true; } } int main() { A a; ulong b=42; assert(a==b); return 0; } ---
Yes, but the code is incorrect if b is larger than long.max. The problem is that values in the range long.max+1..ulong.max get turned into values in the range -1 .. -long.max-1 How can you distinguish them?
Nov 29 2010