Python Forum
getting mantissa size of float
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting mantissa size of float
#1
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).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
(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?
Reply
#4
There is a function in the module math:
https://docs.python.org/3/library/math.html#math.frexp

It returns different values, but after the calculation you get the same result.
I'm not complete sure which one is the right representation.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
(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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
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.
Reply
#7
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
(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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python calculate float plus float is incorrect? sirocawa 1 26 55 minutes ago
Last Post: Gribouillis
  size of set vs size of dict zweb 0 2,134 Oct-11-2019, 01:32 AM
Last Post: zweb
  Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key firebird 2 3,373 Jul-25-2019, 11:32 PM
Last Post: scidam
  CSV file created is huge in size. How to reduce the size? pramoddsrb 0 10,469 Apr-26-2018, 12:38 AM
Last Post: pramoddsrb

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020