Question regarding precision bits used in Mpmath

28 views
Skip to first unread message

Sahil Verma

unread,
Jul 11, 2018, 5:39:07 PM7/11/18
to mpmath
Dear Developers,

I had a question regarding the precision bits (which I specify by assigning a number to mp.prec). So, I have a thought, the precision bits assigned here is just the mantissa of corresponding the floating point number and not the entire size [sign bit + exponent size + mantissa size] of the entire floating point number. Please correct me if I am incorrect. 

If I am incorrect and the case is such that mp.prec actually specifies the entire size [sign bit + exponent size + mantissa size] of a floating point number, then for numbers like 18 or 23, how do you break up into number into the components (sign, exponent and mantissa) ? 

Thank you,
Sahil

Fredrik Johansson

unread,
Jul 11, 2018, 5:42:50 PM7/11/18
to mpm...@googlegroups.com
Hello Sahil,

The precision only specifies the size of the mantissa. Exponents at any precision can be arbitrarily large.

Fredrik

Sahil Verma

unread,
Jul 11, 2018, 5:48:49 PM7/11/18
to mpm...@googlegroups.com
Dear Fredrik,

I am really thankful for your instant reply. I believe adding this part explicitly to the documentation would be helpful. I then have a following question. Lets say I have floating point number, I cast that number using numpy to 32 bit floating point and use 23 bit precision in mpmath library. Since the mantissa of 32 bit floating point number is 23, shouldn’t these two casts give me the same answer ? 
If I execute this code in python, it gives me different results. I have attached the code file for your cognizance. 

Please correct me if I am incorrect. Maybe the rounding process in mpmath and numpy differs, I am not sure, do you have an idea ?

Best Regards,
Sahil

mpmath-try.py

Aaron Meurer

unread,
Jul 11, 2018, 5:51:30 PM7/11/18
to mpm...@googlegroups.com
These things are documented in depth at
http://mpmath.org/doc/current/technical.html

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "mpmath" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mpmath+un...@googlegroups.com.
> To post to this group, send email to mpm...@googlegroups.com.
> Visit this group at https://groups.google.com/group/mpmath.
> For more options, visit https://groups.google.com/d/optout.
>
>
>
> On 11-Jul-2018, at 5:42 PM, Fredrik Johansson <fredrik....@gmail.com>
> wrote:
>
> On Wed, Jul 11, 2018 at 11:23 PM, Sahil Verma <v.sa...@gmail.com> wrote:
>>
>> Dear Developers,
>>
>> I had a question regarding the precision bits (which I specify by
>> assigning a number to mp.prec). So, I have a thought, the precision bits
>> assigned here is just the mantissa of corresponding the floating point
>> number and not the entire size [sign bit + exponent size + mantissa size] of
>> the entire floating point number. Please correct me if I am incorrect.
>>
>> If I am incorrect and the case is such that mp.prec actually specifies the
>> entire size [sign bit + exponent size + mantissa size] of a floating point
>> number, then for numbers like 18 or 23, how do you break up into number into
>> the components (sign, exponent and mantissa) ?
>>
>> Thank you,
>> Sahil
>
>
> Hello Sahil,
>
> The precision only specifies the size of the mantissa. Exponents at any
> precision can be arbitrarily large.
>
> Fredrik
>
> --
> You received this message because you are subscribed to the Google Groups
> "mpmath" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mpmath+un...@googlegroups.com.
> To post to this group, send email to mpm...@googlegroups.com.
> Visit this group at https://groups.google.com/group/mpmath.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "mpmath" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mpmath+un...@googlegroups.com.
> To post to this group, send email to mpm...@googlegroups.com.
> Visit this group at https://groups.google.com/group/mpmath.
> For more options, visit https://groups.google.com/d/optout.
>

Sahil Verma

unread,
Jul 11, 2018, 6:00:34 PM7/11/18
to mpm...@googlegroups.com
Dear Aaron,

Thank you for the link. I read it, it actually doesn’t exactly address my concern about the difference in the results for same precision between numpy and Mpmath. I would be grateful, if someone could point out the reason for the difference.

Thank you,
Sahil

Fredrik Johansson

unread,
Jul 11, 2018, 6:12:21 PM7/11/18
to mpm...@googlegroups.com
The different types are just printed differently. You can see that the values are the same by, for instance, converting both back to float. nstr(mp.mpf(b),10) also shows the same 10 digits as printed by numpy.

Fredrik
Reply all
Reply to author
Forward
0 new messages