Python Float Numeric Data Type
Published: 2023-05-30 10:11:12



<section>
<a id="introduction" class="custom_anchor"></a>
<h2>A. Introduction</h2></a>
</section>

Float is one of the data types under the numeric category. See the <a href="https://logiclair.org/1533/python-integer-numeric-data-type">Integer type</a> in this tutorial. Python has integer, float, complex, bool, list, tuple, set, range, dictionary, string, and bytes data types. In this tutorial I will only cover the <mark>Float</mark> type.


<section>
<a id="numeric" class="custom_anchor"></a>
<h2>B. Numeric data type</h2></a>
</section>

There are three built-in numeric data types in Python.


    Integer
    Float
    Complex


There are also two non-built-in numeric data types.


    Fraction
    Decimal


Built-in means you don't need to import such data or functionality.


<section>
<a id="float-1" class="custom_anchor"></a>
<h2>C. Float</h2></a>
</section>

Float or floating-point number may represent a number that is equal to one or more, or a number that is less than one with decimal point. It can be a positive number such as 1.25 or 0.75. It can also be a negative number such as -100.5 or -0.0015. It can also be just 0.0. It can also be a whole number with decimal point such as 40.0.

Python's float has a maximum value of <mark>1.7976931348623157e+308</mark>. Its minimum positive value is <mark>2.2250738585072014e-308</mark>.

You can get these values by running the Python interpreter from your console or command line. Assuming that you already installed Python.

    C:\>python
    Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug  1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    >>> max_value = sys.float_info.max
    >>> max_value
    1.7976931348623157e+308
    >>> min_value = sys.float_info.min
    >>> min_value
    2.2250738585072014e-308


Output the full digits for maximum value.


    >>> int(max_value)
    179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368



<section>
<a id="operators" class="custom_anchor"></a>
<h2>D. Operators</h2></a>
</section>

We usually use operators to manipulate data. I will only cover some common operators that are usually applied for float. The full operator tutorial will cover this.

Arithmetic operators.

<table>
  <tr>
    <th>Operator</th><th>Name</th>
  </tr>
  <tr>
    <td>+</td><td>addition</td>
  </tr>
  <tr>
    <td>-</td><td>subtraction</td>
  </tr>
  <tr>
    <td>/</td><td>float division</td>
  </tr>
  <tr>
    <td>%</td><td>remainder, a % b</td>
  </tr>
  <tr>
    <td>**</td><td>power, a ** b or a ^ b</td>
  </tr>
</table>

Comparison operators.

<table>
  <tr>
    <th>Operator</th><th>Name</th>
  </tr>
  <tr>
    <td> > </td><td>greater than</td>
  </tr>
  <tr>
    <td> >= </td><td>greater than or equal</td>
  </tr>
  <tr>
    <td> < </td><td>less than</td>
  </tr>
  <tr>
    <td> <= </td><td>less than or equal</td>
  </tr>
  <tr>
    <td> == </td><td>equal, a == b</td>
  </tr>
  <tr>
    <td> != </td><td>not equal, a != b</td>
  </tr>
</table>


<section>
<a id="arithmetic" class="custom_anchor"></a>
<h3>1. Arithmetic Operators</h3></a>
</section>

Addition and subtraction

Open your python interpreter from the terminal by typing python.


    c:\>python
    Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug  1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 10.5 + 1.5
    12.0
    >>> 20.0 - 5.5
    14.5


Multiplication

Use an asterisk symbol for multiplication. The "type()" method gets the type or class of a variable or object.


    >>> a = 100.5
    >>> b = 2.5
    >>> c = a * b
    >>> c
    251.25
    >>> type(c)
    <class 'float'>
    >>> type(a)
    <class 'float'>
    >>> type(b)
    <class 'float'>


Division

Use the "/" symbol to get the quotient.


    >>> a = 300.9
    >>> b = 200.5
    >>> c = a / b
    >>> c
    1.5007481296758103


Remainder

Python has an operator that can calculate the remainder. It is called the modulo operator with symbol `%`.

    >>> a = 2.5
    >>> b = 6.2
    >>> a % b
    2.5


Power

The built-in function pow() will return a number given base and exponent. It can also be expressed using double asterisk.


    >>> a = 6.4
    >>> b = 1.25
    >>> pow(a, b)
    10.17946532821825
    >>> a ** b
    10.17946532821825



<section>
<a id="comparison" class="custom_anchor"></a>
<h3>2. Comparison operators</h3></a>
</section>

Greater than operator ">".

    >>> a = 1.9
    >>> b = 2.1
    >>> a > b
    False


Greater than or equal to with symbol ">=".

    >>> a = 150.0
    >>> b = 120.8
    >>> a >= b
    True


Less than with symbol "<".

    >>> a = 10.8
    >>> b = 20.0
    >>> a < b
    True

Less than or equal to with symbol "<=".

    >>> a = 250.5
    >>> b = 250.5
    >>> if a <= b:
    ...     print("a is less than or equal to b")
    ... else:
    ...     print("a is not less than or equal to b")
    ...
    a is less than or equal to b


Equal with symbol "==".

    >>> a = 0.36
    >>> b = 0.36
    >>> a == b
    True
    >>>
    >>> # another example
    >>> a = 50.45
    >>> b = 50.40
    >>> a == b
    False


Not equal with symbol "!=".

    >>> a = 1000.36
    >>> b = 1000.361
    >>> a != b
    True


<section>
<a id="class" class="custom_anchor"></a>
<h2>E. Class float() Constructor</h2></a>
</section>

Python has a built-in function called "float()" or a class constructor that can set another type into a float type.

Convert an integer to a float.

    >>> a = 100
    >>> a
    100
    >>> type(a)
    <class 'int'>
    >>> b = float(a)
    >>> b
    100.0
    >>> type(b)
    <class 'float'>

Convert a numeric string to a float.

    >>> a = "75.5"
    >>> a
    '75.5'
    >>> type(a)
    <class 'str'>
    >>> b = float(a)
    >>> b
    75.5
    >>> type(b)
    <class 'float'>


<section>
<a id="float-2" class="custom_anchor"></a>
<h2>F. Float Example</h2></a>
</section>

Write a python script called "fruit.py" that takes the price of apple per kilogram, the number kilograms to buy and output the total cost.

*fruit.py*

    # Ask the cost per kilograms.
    cost_per_kg = input("How much is the cost of apple per kilogram? ")
    cost_per_kg = float(cost_per_kg)  # convert string to float
    
    # Ask how many kilograms to buy.
    num_kg = input("How many kilograms to buy? ")
    num_kg = float(num_kg)
    
    # Calculate the total cost.
    total_cost = cost_per_kg * num_kg
    print("total cost: " + str(total_cost))

*execute the script*

    c:\>python fruit.py


Put your fruit.py in C drive.

If the file is in C drive under python_tutorial folder for example, "cd" or change directory to that folder. The command will look like this:

    c:\python_tutorial>python fruit.py

*output*

    How much is the cost of apple per kilogram? 1.44
    How many kilograms to buy? 1.5
    total cost: 2.16


<section>
<a id="float-3" class="custom_anchor"></a>
<h2>G. Float method as_integer_ratio</h2></a>
</section>

The class float has a method that returns an integer numerator and denominator. It is defined as "float.as_interger_ratio()".

    >>> a = 0.25
    >>> b = a.as_integer_ratio()
    >>> b
    (1, 4)
    >>> numerator = b[0]
    >>> denominator = b[1]
    >>> numerator
    1
    >>> denominator
    4
    >>> type(numerator)
    <class 'int'>
    >>> type(denominator)
    <class 'int'>
    >>> q = numerator / denominator
    >>> q
    0.25
    >>> type(q)
    <class 'float'>


<section>
<a id="rounding" class="custom_anchor"></a>
<h2>H. Rounding a float</h2></a>
</section>

Python provides a built-in function "round()" to round a float up to a given decimal places. This is useful if you don't need a very accurate number for simplification.

Round off a number up to two decimal places.

    >>> a = 7.35514552
    >>> type(a)
    <class 'float'>
    >>> b = round(a, 2)
    >>> b
    7.36