Fwd: binomial calculation wrong?

13 views
Skip to first unread message

Jason

unread,
Dec 31, 2011, 7:06:15 AM12/31/11
to mpir-...@googlegroups.com

---------- Forwarded Message ----------

Subject: binomial calculation wrong?
Date: Thursday 29 December 2011, 15:39:03
From: "Michael Dambmann" <mic...@dambmann.de>
To: themp...@gmail.com

<html><head></head><body bgcolor='#FFFFFF' style='font-size:10pt;background-color:#FFFFFF;font-family:Verdana, Arial, sans-serif;'>Dear mpir team,<br/>
<br/>
I have played around with the binomial calculation.<br/>
The attached .cpp file is created with vs2010 and win7 64 bit.<br/>
<br/>
The resulting calculation seams to be not correct.<br/>
e.g.:<br/>
<br/>
Normal Calculation : n=57&nbsp;k=25&nbsp;results in&nbsp;9929472283517787 &lt;== correct value<br/>
MPIR calculation&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: n=57 k=25&nbsp;results in 9929472283517786 &lt;== wrong value<br/>
<br/>
I could track it down to the function:<br/>
<p>
<font face="Consolas" size="2"><font face="Consolas" size="2">mpz_get_d(result);</font></font></p>
<br/>
which will modify the outgoing value.<br/>
Any idea why this happens?<br/>
<br/>
<br/>
Best regards,<br/>
Michael Dambmann<br/>
<br/>
<br/>
<br/></body></html>
-----------------------------------------

binomial_test.cpp

Cactus

unread,
Dec 31, 2011, 9:16:45 AM12/31/11
to mpir-...@googlegroups.com, mic...@dambmann.de
The bug is in your mpir_bincoef subroutine where you use a routine that returns a double as follows:

ret = mpz_get_d(result);

 to set a 64-bit value in ret:  

Since a double on Windows has a 53 bit mantissa, this will give wrong values for 64-bit values with longer bit lengths than this.

If you add:

#include <stdint.h>

to the file and then replace the above line with 

ret = mpz_get_ux(result);

it will, I think, work as expected.

     Brian

Reply all
Reply to author
Forward
0 new messages