"Fixed precision" integers

point14 at magma.ca point14 at magma.ca
Wed Aug 29 15:07:00 CEST 2007


On Wed, 29 Aug 2007, Jim White wrote:

>> From: point14 at magma.ca
>> Subject: fixed size integer arithmetic
>
>> Is it possible in GMP to have the size of integers
>> (like mpz_t) to be fixed in size
>
>
> I think I know what you mean - you want to put a bound
> on the maximum number of digits (bits, whatever). If
> an integer "overflows" you just want to keep the most
> significant digits.

No. I want to keep the least significant digits. The carry bit is to be 
lost, the least significant bits are to roll over, and the lower digits 
are kept. Here is an example of the behavior where a 3 bits number is 
delcared and is incremented by one repeatedly:

...
101
110
111
000
001
010
...

Realize that this could be any size, three bits is just for the purpose of 
the example, it would be a bit excessive to show an example with thousands 
of bits, but the behavior would be the same: roll over when maximum is 
reached.

Here is another example with three fixed size 3 bit numbers, showing 
6+3=9:

   110
+ 011
-----
   001

Of course the final answer is not 9, it's 1, but this is the behavior I 
need. Since all variables have a fixed size of 3 bits, the "correct" 
result cannot fit so only the lowest bits are kept.

Can GMP data types do this? It seem that if I use the low level functions 
I might be able to do this.

Thanks,
Martin


More information about the gmp-discuss mailing list