From: Walter Roberson <>
Newsgroups: comp.soft-sys.matlab
Subject: Re: integer multiplication modulo 2^32
Date: Wed, 21 Apr 2010 19:28:09 -0500
Organization: Canada Eat The Cookie Foundation
Lines: 28
Message-ID: <hqo56s$h37$>
References: <hqnrsh$qjg$> <hqnth0$o0d$>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: 1271896092 17511 (22 Apr 2010 00:28:12 GMT)
NNTP-Posting-Date: Thu, 22 Apr 2010 00:28:12 +0000 (UTC)
User-Agent: Thunderbird (X11/20100317)
In-Reply-To: <hqnth0$o0d$>
Xref: comp.soft-sys.matlab:628713

Roger Stafford wrote:
> "Jennifer Faggi Renault" <> wrote in message 
> <hqnrsh$qjg$>...

>> i have a variabla  B= '2A66311C'
>> so to calculate mod(B*(2*B + 1),2^32)
>> i did Bc=hex2dec(B,8) then i tried:
>> t= mod(Bc*(2*Bc + 1),2^32) but, i get after conversion into hex 
>> t='E1DDA700' instead of t='E1DDA73C' wich i should find !!

>  You are expecting too much of double precision floating point's 53 bits 
> of significand accuracy.  Notice that your error lies in the least six 
> bits of the desired answer.  You need to somehow have about 61 or so 
> bits of accuracy for your calculations or to divide up the hex number 
> into two parts and carry out the desired operations on it in an 
> appropriate piecemeal fashion.

In addition to Roger's entirely correct analysis, note that there is a bug in 
fprintf that cause the lower bits to be lost when %lx format is used with uint64:

 >> num2hex(.9)
ans =
 >> fprintf('%lx\n', typecast(.9,'uint64'))

This bug does not, however, affect any of the routines that have 'hex' in 
their name, such as hex2dec or num2hex.