i know the computational way to determine the number of bits of mantissa of float (have done it in C). i am wondering if there is some defined constant(s) or other way to get this more directly (across various platforms). i need to limit this to modules that come built in to Python. i want the effective mantissa (53 for x86 double) rather than the reserved space (52 for x86 double).
Not so easy. From stackoverflow:
https://stackoverflow.com/questions/4533...ut-strings
I think this is the best solution (float.mantissa() and float.exponent() would be nicer):
from decimal import Decimal
def fexp(number):
(sign, digits, exponent) = Decimal(number).as_tuple()
return len(digits) + exponent - 1
def fman(number):
return Decimal(number).scaleb(-fexp(number)).normalize()
Or for easier use a class:
from decimal import Decimal
class MyFloat(float):
@property
def fexp(self):
(sign, digits, exponent) = Decimal(self).as_tuple()
return len(digits) + exponent - 1
@property
def fman(self):
return Decimal(self).scaleb(-self.fexp).normalize()
v = MyFloat(133.7)
print(v.fexp)
print(v.fman)
Output:
2
1.336999999999999886313162278
(May-01-2022, 10:55 PM)Skaperen Wrote: [ -> ]i want the effective mantissa (53 for x86 double) rather than the reserved space (52 for x86 double).
What does this mean? What is the «effective mantissa». Do you have a reference for this?
(May-02-2022, 07:46 AM)Gribouillis Wrote: [ -> ]What does this mean? What is the «effective mantissa». Do you have a reference for this?
IEEE 754: A double precision float (64 bit) has 52 bits for mantissa. The 53th bit is implicit and always True. It's a trick from old men with long beards.
I dont agree with that. There is 1 sign bit, 11 bits of exponent and 52 bits of mantissa. That makes 64 bits. Or perhaps you are talking about the implicit 1. in normal numbers? I wrote
a gist about IEEE754. This doesn't make Skaperen's question more clear. I think he means something else.
Perhaps
sys.float_info.mant_dig
is the required information? It gives 53 on my system.
Yes, I was talking about the implicit 1.
Quote:Perhaps sys.float_info.mant_dig is the required information? It gives 53 on my system.
Same here. It's hard to find a different result.
(May-02-2022, 08:44 AM)Gribouillis Wrote: [ -> ]Perhaps sys.float_info.mant_dig
is the required information? It gives 53 on my system.
that's what i'm looking for. i was searching "mantissa" but that word was not used so it was not found. a slow read about the float type would have gotten the reference but, as usual, i am in a hurry and trying to find things the most direct way.