Is there a way to see the internal representation of float?

2024/10/6 12:24:52

In python tutorial for floating point,

In base 2, 1/10 is the infinitely repeating fraction

0.0001100110011001100110011001100110011001100110011...

How do I get python interpreter to print this intenal representation? repr(0.1) does not help.

Answer

You can use the struct module to get the internal representation of float. Once you get the representation, you just need to convert the character to the ordinal value and format it to binary.

>>> import struct
>>> def float_rep(num):return ''.join("{:08b}".format(ord(elem)) for elem in struct.pack('!f', num))>>> float_rep(0.1)
'00111101110011001100110011001101'

Edit

Though the above would provide you the actual representation internally, but I suppose, you are more interested in the binary equivalent of the decimal. The following would suffice your query

>>> def float_to_bin(num):sign = '-' if  num < 0 else ''num = abs(num)whole = int(num)num  -= wholeyield sign + bin(whole) + '.'while num:num*= 2whole = int(num)yield str(whole)num -= whole>>> ''.join(islice(float_to_bin(1.1),None,20))
'1.0001100110011001100'
>>> ''.join(islice(float_to_bin(-1.1),None,20))
'-1.0001100110011001100'
>>> ''.join(islice(float_to_bin(.1),None,20))
'0.0001100110011001100'
>>> ''.join(islice(float_to_bin(0),None,20))
'0.'
>>> ''.join(islice(float_to_bin(1),None,20))
'1.'
>>> ''.join(islice(float_to_bin(0.25),None,20))
'0.01'
>>> ''.join(islice(float_to_bin(0.5),None,20))
'0.1'
>>> ''.join(islice(float_to_bin(-0.5),None,20))
'-0.1'
https://en.xdnf.cn/q/118949.html

Related Q&A

Create function from try-except

Based on the example provided in this answer, how can I create a function from:from collections import Counter s = [0, 0, 2, 1, 1, 0, 0, 0] try:print(next(t[0] for t in Counter(s).most_common(2) if t[…

Bad request error flask with POST request

I have the following route that is getting values from a submitted form@app.route(/authenticate, methods=["POST"]) def authenticate():username = request.form[username]print(username, file = s…

How to use matplotlib/numpy to plot heatmap given three irregular lists of values in Python

Im wondering if there is a way to use matplotlib and numpy to plot the heatmap of three lists. My grid is not regular, it is oddly shaped so this does not work for me: Plotting a Heat Map X,Y,Intensity…

How to set custom color for symbols like*,#,etc in python tkinter

How to set specific color for certain symbols like ,#,etc example if I type "" its color should be blue and other stay remain same. typedecker sir i am binding you function like this but thi…

How can I get only heading names.from the text file

I have a Text file as below:Education: askdjbnakjfbuisbrkjsbvxcnbvfiuregifuksbkvjb.iasgiufdsegiyvskjdfbsldfgdTechnical skills : java,j2ee etc.,work done: oaugafiuadgkfjwgeuyrfvskjdfviysdvfhsdf,aviysdvw…

Concatenate list elements that fall between list elements of certain value

Imagine I have the following list:>>> mylist[(a, uDT),(Satisfactory, uJJ),(tracing, uVBG),(with, uIN),(a, uDT),(fairly, uRB),(persistent, uJJ),(with, uIN)]How do I concatenate list items that …

How to create list from 100 elements to list of 10 [duplicate]

This question already has answers here:How to iterate over a list in chunks(40 answers)Closed 4 years ago.I have small brain fade today and I believe it will be faster to get hint here than wondering f…

how to make the width of image longer using animation in tkinter GUI?

I have this image and I want to make the width of this image longer till the end of the window of tkinter using animation but I havent got any proper way to achieving this task. any suggestions ?

Syntax error with if statement in python 2.7

Im having trouble with a "Syntax Error: invalid syntax" message in python code that keeps moving the goals posts on me. Heres the sample code:another_answer = int(raw_input("> "…

Python- Quicksort program in-place

I have a quicksort program here, but there seems to be a problem with the result. I think there must have been some issue in the areas highlighted below when referencing some values. Any suggestions?#…