Path: news.mathworks.com!newsfeed-00.mathworks.com!panix!bloom-beacon.mit.edu!newsswitch.lcs.mit.edu!elk.ncren.net!newsflash.concordia.ca!canopus.cc.umanitoba.ca!not-for-mail
From: Walter Roberson <roberson@hushmail.com>
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$1@canopus.cc.umanitoba.ca>
References: <hqnrsh$qjg$1@fred.mathworks.com> <hqnth0$o0d$1@fred.mathworks.com>
NNTP-Posting-Host: ibd-nat.ibd.nrc.ca
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: canopus.cc.umanitoba.ca 1271896092 17511 132.246.133.10 (22 Apr 2010 00:28:12 GMT)
X-Complaints-To: abuse@cc.umanitoba.ca
NNTP-Posting-Date: Thu, 22 Apr 2010 00:28:12 +0000 (UTC)
User-Agent: Thunderbird 2.0.0.24 (X11/20100317)
In-Reply-To: <hqnth0$o0d$1@fred.mathworks.com>
Xref: news.mathworks.com comp.soft-sys.matlab:628713

Roger Stafford wrote:
> "Jennifer Faggi Renault" <asma_192@live.fr> wrote in message 
> <hqnrsh$qjg$1@fred.mathworks.com>...

>> 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 =
3feccccccccccccd
 >> fprintf('%lx\n', typecast(.9,'uint64'))
3feccccccccccc00

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