Yet another bigint

Started by
28 comments, last by DevFred 15 years, 5 months ago
Quote:Original post by b1gjo3
can someone tell me how exactly i can tell my compiler to show (2 * (MAX_UINT+1)^1) + (2 * (MAX_UINT+1)^0) as 8589934593.

thanks everyone
You may be able to reinterpret_cast your bigint to an __int64 or long long etc, but beyond that, there isn't any way to view the actual number using the debugger. For a 50 decimal digit number (for example) your only option is to write a function to convert it to a string and insert code to output that.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Advertisement
i guess the only question i have now is how do i know if two numbers are going to overflow, i dont really know how to check for that.
Quote:Original post by b1gjo3
how do i know if two numbers are going to overflow

There is no overflow in unsigned arithmetic, you probably mean if there's a carry? ;-)

I guess there must have been a carry if (a + b) < a, not sure if that always works though.
Quote:Original post by DevFred

I guess there must have been a carry if (a + b) < a, not sure if that always works though.


yes, if the "overflow" when 4,294,967,295 + 1 it goes to 0, it just wraps around. which in this case the carry is 1.

and if a = 4,294,967,295 and b = 1 then...

(a + b) yields 0 and thats less than 4,294,967,295 this definetly does not take care of multiple carrys for instance..

a = 4,294,967,295 b = 4,294,967,295

(a + b) yields 0 and the carry is 2.

Anyone have any ideas?
Where do you get two from?
Quote:Original post by b1gjo3
this definetly does not take care of multiple carrys for instance..

a = 4,294,967,295 b = 4,294,967,295

(a + b) yields 0 and the carry is 2.


No, it yields 4,294,967,294 with a carry of 1. You can only get a carry > 1 by adding up more than two numbers at a time. Proof: Suppose we are working in base N. To get a carry "out" of 2, the sum of the carry "in", and the two current digits, would have to be at least 2N. But each current digit is at most N-1, so we can only get a carry "out" of 2 if the carry "in" is at least 2. But the carry "in" for the least significant digits is 0, and thus the carry "in" to the next digit is at most 1, and the carry "in" to all other digits is also at most 1, by induction.
At this point, a crash course in basics of logicmajigs is in place.

Of course, the fun starts once you get to multiplication, division, fractions, and the rest of useful operations.

Fixed-point math is also a fun learning experiment.
i have tried
if ( (a >> 31) & (b >> 31) ) // overflow


but that isnt always the case of an overflow, for some reason i think i have to learn to check bits.
if (a > (a + b)) // addition of a with b will overflow, assuming unsigned treatment
Quote:Original post by b1gjo3
i have tried
if ( (a >> 31) & (b >> 31) ) // overflow

That's a sufficient condition, but not a necessary one, e.g. whenever a and b are too big, their sum will produce a carry, but there are other cases where only one of them is too big and the other isn't, and the sum will produce a carry nonetheless.

(a + b) < a seems to be the solution.

This topic is closed to new replies.

Advertisement