Float

In the context of programming and machine learning, a "float" refers to a floating-point number, which is a number that can have a fractional part. Floats are used to represent real numbers and are essential in various computational tasks due to their ability to handle a wide range of values with decimal points. This entry explores the concept of floats as discussed in Python and PyTorch.

Float in Python

The Quick Python Book, Fourth Edition provides a comprehensive overview of floats in Python:

Definition

In Python, a float is a number that can be represented with a decimal point or in scientific notation, such as 3.14 or -2E-8. The precision of floats is typically equivalent to double (64-bit) types in C, governed by the underlying machine.

Float Operations

Arithmetic operations involving floats in Python are similar to those in C. When two integers are involved in an operation, the result is an integer, except for division (/), which results in a float. If the // division symbol is used, the result is an integer with truncation. Operations involving a float always produce a float. Here are some examples:

5 + 2 - 3 * 2
# [Output: 1](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/103)

5 / 2         # floating-point result with normal division
# [Output: 2.5](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/103)

5 / 2.0       # also a floating-point result
# [Output: 2.5](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/103)

5 // 2        # integer result with truncation when divided using '//'
# [Output: 2](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/103)

30000000000   # This would be too large to be an int in many languages
# [Output: 30000000000](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-6/67)

30000000000 * 3
# [Output: 90000000000](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/103)

30000000000 * 3.0
# [Output: 90000000000.0](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/103)

2.0e-8        # Scientific notation gives back a float
# [Output: 2e-08](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/103)

3000000 * 3000000
# [Output: 9000000000000](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-6/67)

Conversion to Float

The float function in Python is used to convert a string or a number to a floating-point number. If the string cannot be converted, it raises a ValueError. Examples include:

float('123.456')
# [Output: 123.456](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-6/67)

float('xxyy')
# [Raises ValueError: could not convert string to float: 'xxyy'](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-6/67)

Converting an integer to a float will always add a ".0":

float(200)
# [Output: 200.0](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/103)

Conversion Between Types

Python allows explicit conversions between types. Converting from a float to an int will truncate the value and discard the decimal portion. Conversely, converting an int to a float will always add a ".0":

int(200.2)     # Convert float to int
# [Output: 200](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/103)

int(2e2)       # Convert exponent to int
# [Output: 200](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/103)

float(200)     # Convert an int to a float
# [Output: 200.0](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/103)

Additional Considerations

When using the int function, you can pass an optional second argument to specify the numeric base for interpreting the input string. However, this is not applicable to floats:

int('3333')
# [Output: 3333](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-6/67)

int('123.456')  # Can't have decimal point in integer
# [Raises ValueError: invalid literal for int() with base 10: '123.456'](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/103)

int('10000', 8) # Interprets 10000 as octal number
# [Output: 4096](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/103)

int('101', 2)
# [Output: 5](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/103)

int('ff', 16)
# [Output: 255](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-6/67)

int('123456', 6) # Can't interpret 123456 as base 6 number
# [Raises ValueError: invalid literal for int() with base 6: '123456'](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-6/64)

Float in PyTorch

Deep Learning with PyTorch, Second Edition discusses floats in the context of PyTorch:

Characteristics of Float in PyTorch

  • 32-bit Precision: A float in PyTorch is a 32-bit floating-point number, which provides a balance between range and precision. This is typically sufficient for most deep learning tasks, where the precision of 32 bits allows for efficient computation while maintaining accuracy.

  • Homogeneity in Tensors: PyTorch tensors are homogenous, meaning that all elements within a tensor must be of the same data type. When a tensor is defined as a float tensor, all its elements are 32-bit floating-point numbers. This homogeneity is crucial for efficient computation and memory usage, as it allows PyTorch to optimize operations on tensors.

  • Usage in Deep Learning: Floats are extensively used in deep learning models for representing weights, biases, activations, and gradients. The choice of using 32-bit floats is a trade-off between computational efficiency and the precision required for training complex models.

Understanding the role of floats in PyTorch is essential for effectively working with tensors and building deep learning models. The use of 32-bit floats is a standard practice in the field, providing a good balance between performance and precision.

| Book Title | Usage of float | Technical Depth | Connections to Other Concepts | Examples Used | Practical Application |
|------------|----------------|-----------------|-------------------------------|---------------|-----------------------|
| [The Quick Python Book, Fourth Edition](https://www.manning.com/books/the-quick-python-book-fourth-edition) | Discusses float as a number with a decimal point or in scientific notation, similar to C. | Covers precision, arithmetic operations, and conversion between types. | Links float operations to integer operations and type conversion. | Provides Python code examples for float operations and conversions. | Focuses on Python programming, explaining float usage in arithmetic and type conversion. [more](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/99) |
| [Deep Learning with PyTorch, Second Edition](https://www.manning.com/books/deep-learning-with-pytorch-second-edition) | Describes float as a 32-bit data type used in PyTorch tensors. | Explains 32-bit precision and its importance in deep learning. | Connects float usage to tensor operations and deep learning model training. | Discusses the role of floats in representing weights, biases, and activations. | Emphasizes the use of floats in deep learning for efficient computation and model training. [more](https://livebook.manning.com/book/deep-learning-with-pytorch-second-edition/chapter-3/20) |

FAQ (Frequently asked questions)

What is the result of dividing 5 by 2 in Python?

How do you convert a string to a float in Python?

What happens if a string cannot be converted to a float in Python?

What is the result of converting '123.456' to a float in Python?

What error is raised when a string cannot be converted to a float in Python?