Python Forum
Why replace treats an integer value 999 as 999.0?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why replace treats an integer value 999 as 999.0?
#11
(Feb-11-2020, 07:49 PM)new_to_python Wrote:
(Feb-11-2020, 02:54 PM)DeaD_EyE Wrote:
import struct


def double_to_bin(value):
    """
    Convert an float to double (64 bit) binary representation
    Please check here: http://www.binaryconvert.com/result_double.html

    """
    value_sum = sum(
        value << (shift * 8)
        for shift, value in
        enumerate(struct.pack('<d', value))
    )
    bin_str = f'{value_sum:064b}'
    return {
        'dec': value,
        'hex': f'{value_sum:08x}',
        'bin': bin_str,
        'sign': bin_str[0] == '1',
        'exponent': bin_str[1:12],
        'mantissa': bin_str[12:],
    }
Better you check it online if the result is right.
I checked it with 1.0 and 0.3.


Thanks. In this case, 999 and 999.0 produces the same binary representation which is:

'0100000010001111001110000000000000000000000000000000000000000000'

So python treats them as the same?

Hi, anybody else knows the answer?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Search & Replace - Newlines Added After Replace dj99 3 3,387 Jul-22-2018, 01:42 PM
Last Post: buran
  Using a variable to replace an integer? (Except it isn't working!) s1monsays 15 8,325 Jul-25-2017, 06:58 PM
Last Post: s1monsays

Forum Jump:

User Panel Messages

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