Table of Contents » Chapter 2 : Data (Input) : Data Types : Floating Points
Floating Points
Contents
Overview
A floating point values are used to represent real numbers that can accommodate a wide range of values. Floating-point numbers are essentially the computer's way of representing fractions and decimals, especially when the range of values is too large or the precision requirements are too fine for integer representation.
In programming with floating-point numbers, precision and scale are two important concepts that define the format and accuracy of these numbers.
- Precision: Refers to the total number of significant digits in a floating-point number, irrespective of where the decimal point is located. It includes both the digits before and after the decimal point, but does not include any leading or trailing zeroes if they don't contribute to the accuracy of the number. For example, in the number 123.456, the precision is 6 (three digits before and three digits after the decimal point).
- Scale: Refers specifically to the number of digits after the decimal point. It defines the fraction part of the number, which contributes to its accuracy in representing very small or very precise values. For instance, in the number 123.456, the scale is 3, indicating three digits after the decimal point.
Examples of floating points in Python:
Question
What are the precision and scale values for the following floating point numbers?
- 10.5
- 3.14
- 23456.78901
- 4554545545.89988998989898
In Python, we can perform mathematical calculations using Floating Points using operators which are symbols to represent valid arithmatic operations for float values. Here are the first of these that we will explore:
| Operation | Operator | Description & Examples |
|---|---|---|
| Addition |
Combining two or more numeric values into a single total sum.
|
|
| Subtraction |
Taking away one numeric value (or quantity) from another to find the difference between them.
|
|
| Multiplication |
Adding a number a certain number of times to produce a product.
|
|
| Division |
Splitting a number into a specified number (the divisior) of equal parts. In computing, division by zero will cause an error because division by zero is mathematicaly undefined.
|
|
| Floor (Integer) Division |
The same as division (above), however the result will be an truncated down to the nearest whole number that is less than or equal to the actual result. |
|
| Modulus |
Same as division (above), however it determins if there is a remainder in the result of a division operation.
|
|
| Exponentiation |
This operation raises the first number (the base) to the power of the second number (the exponent). The exponent signifies how many times the base is multiplied by itself.
|
Any of the mathematical operators can be used in more complex (combined) mathematical calculations. Here are some examples:
print(10.5 + 20.5 - 15.5 * 5.5)
print(10.5 + (100.5 / 2.5)) # See the note about the use of parenthses below
print(10.5 ** (2.5 * 30.5))) # See the note about the use of parenthses below
In Python, the order of operations for mathematical operators follows the conventional mathematical hierarchy, which is consistent with the rules of arithmetic that are typically taught in mathematics (often remembered by the acronym PEMDAS/BODMAS). For the operators you've listed (+, -, *, /, //, %, and **), the order of precedence is as follows:
- Exponents (**)
- Multiplication (*) and Division (/, //, and %)
- Addition (+) and Subtraction (-)
In Python, like math, we can use parenthses ( ) to isolate and group factors of our equations to override andn control the order of operations. See the examples in the Combining Operations section above.