Data Type - 'float' for Real Numbers

This section describes the 'float' data type, which using the double precision of floating-point number format defined in the IEEE 754 standard.

What Is the "float" Data Type? "float" is the floating number data type in Python. It has infinite number of objects covering all real numbers.

Note that "float" data type stores real numbers in double precision (using 8-byte storage) format according to the IEEE 754 standard.

"float" data type has the following main Features.

1. "float" data objects can be created in several ways:

• Using decimal point literals like 3.14, -9.8, 0.0, etc.
• Using exponential literals like 2.99792458e8, 9.1093837e-31, 0e0 etc.
• Using the cast function, float(object), like float(1), float('1'), etc.

2. "float" data type is an immutable data type. Once a "float" data object is created to store a real number, this number will never change.

Don't get confused about "float" immutability and "float" variable re-assignment capability. A "float" variable can be re-assigned with different "float" objects many times.

```# new object is created for 1.0 and assigned to x
>>> x = 1.0
>>> id(x)
4551429136

# assign the same object to y
>>> y = x
>>> id(y)
4551429136

# new object is created for 2.0 and assigned to x
>>> x = 2.0
>>> id(x)
4551429360

# the first object for 1.0 is still there in memory
>>> y
1.0
>>> id(y)
4551429136
```

3. Because "float" data type is immutable, an object created for a given real number is not allowed to change its value. This allow "float" objects to be cached in memory and reused later whenever they are needed again. Reusing objects will reduce execution time and memory consumption.

On my macOS computer, "float" objects are cached and reused in some cases, not in all cases. The code example below shows you that there are 4 "float" objects created for 5 requests of real number 1.0.

```>>> x = 1.0
>>> y = 1.0
>>> (id(x), id(y), id(1.0), id(float(1)), id(float('1')))
(4551427088, 4551429360, 4551429136, 4551426576, 4551426576)
```

4. "float" objects support the following arithmetic operations:

```Syntax   Operation         Resulting Type
------   ---------         --------------
x - y   Subtraction       float
x * y   Multiplication    float
x / y   Division          float
```

5. "float" objects support the following comparison operations.

```Syntax   Operation
------   ---------
x <  y   Less than
x <= y   Less than or equal
x >  y   Greater than
x >= y   Greater than or equal
x == y   Equal
x != y   Not equal
```

6. Some built-in functions are provided for "float" objects.

• abs(x) - Returns a "float" object for the absolute value of x.
• round(x, n) - Returns a "float" object for x rounded up to n digits after the decimal point.
• pow(x, y) - Returns an "float" object for x to the power y. This can also be written as x**y using the ** operator.

7. Some class/instance methods are provided for "float" objects.

• float.as_integer_ratio() - Instance method returns a tuple of 2 integers whose ratio is exactly equal to the original float and with a positive denominator.
• float.is_integer() - Instance method returns True if this real number is finite with integral value, and False otherwise.
• float.hex() - Instance method returns a representation of a floating-point number as a hexadecimal string in format of [sign] ['0x'] integer ['.' fraction] ['p' exponent].
• float.fromhex(s) - Class method returns a "float" object by parsing the given string representing a floating-point number in the format of [sign] ['0x'] integer ['.' fraction] ['p' exponent].