Avatar ·

How to represent float as integer without loss of information?

📁 python, помощь, ии

There is a task of converting float values to int type values (one float value to one int value) without loss of information, i.e., so that the reverse conversion can be performed later. How can this be implemented?

Important

It is necessary that during such conversion the order and sum are preserved,
i.e., for any a, b: float the following must hold:

to_float(to_int(a) + to_int(b)) == a + bto_int(a) < to_int(b) when a < b, etc.

I found on English-language SO where the structure of float values is represented bitwise, but I don't understand how to apply it to my task.

Note: NaN and inf in my task are not

Avatar ·

The link describes the cause of this error, as well as what needs to be done if you need to serialize a type unknown to the encoder.

Here I will give an example of a case where a typical class with a set of attributes that is subject to serialization is created.

import jsonclass Person:    def __init__(self, name, age, **kwargs):        self.name = name        self.age = age        self.attribute = kwargs or Noneclass PersonEncoder(json.JSONEncoder):    def default(self,

Log in to leave an answer

Blogs