12. Float type#

Author: Tue Nguyen

12.1. Outline#

  • Why float type?

  • Why the name float?

  • How to get a float?

  • Conversion to float

  • When uses float?

  • Operations on float

  • Math utilities

12.2. Why float type?#

  • Motivation: used to represent reald numbers

  • Real-life data: salary, temperature, distance, sales, profit, price

  • Python implementation:

    • Type: float

    • Possible values: any real number

  • Note that float is just an approximation of the real line

    • The set of real numbers is a continuum with no gap

    • There’s no way machines can represent such a precision

    • Thus, be aware of round the inherent rounding error when dealing with floats

12.3. Why the name float?#

  • Python uses float to represent real numbers such as 1.5, 3.14, or 2.7

  • The name float came from the fact that real numbers are represented in the floating point format in computers

    • Ex: 3.14 can be represented as 3.14, 0.314 * 10^1, or 31.4 * 10^(-1)

    • The decimal point can move around, thus the name floating point

  • However, you can safely ignore this technical detail and think of a float just as a real number

12.4. How to get an float?#

a) Ex 1: from float literals

# Normal float
x = 10.5
type(x)
float
# 10.0 is float, not integer
x = 10.0
type(x)
float
# 10. is short for 10.0
x = 10.
type(x)
float
# Scientific notation, use e as 10^
# Ex: 2 million
2e6
2000000.0

b) Ex 2: from an expression that produces a float

Here are some examples of expressions producing ints

  • Math operations between floats

  • A call to a function that returns an float

  • Typecasting to float

b1) Math operations between ints

# Add 2 floats -> float
10.5 + 2.5
13.0
# Float + int -> float 
# Rule: result is promoted to the more general type
# Here, float is more general than int
10.0 + 2
12.0
# Real division always produces float
# Even the result is an integer mathematically
4 / 2
2.0

b2) A call to a function producing an float

round(10 / 3, 2)
3.33

b3) Typecasting to float

  • Use float() to convert a value to float

  • Conversion to float is NOT always

# From a bool to float
# True -> 1.0
# False -> 0.p
print(float(True))
print(float(False))
1.0
0.0
# From int to float
# The value is the same, but the type is now float
float(2)
2.0
# From str to float
# Only strings that look like floats can be converted
print(float("1.5"))
print(float("-1.5"))
1.5
-1.5
# White space at two ends are ignored
print(float("  -1.5      "))
print(float("  -1.5     "))
-1.5
-1.5
# Leading and ending zero is also ignored
float("-01.50")
-1.5
# However, "50,000.2" can not be converted to float
# Try them to see the errors

12.5. When uses float?#

Any time we need a real number

12.6. Operations on float#

  • Math operations: +, -, *, /, //, %, **

  • Comparison operations: ==, !=, >=, <=, >, <, is, is not

12.7. Math utilities#

  • The Python standard libraries come with a module named math containing utilities for dealing with numbers

  • This section applies to numbers, thus to both int and float

  • You will learn more about modules later

  • For now, just follow some simple examples

# To use math module, we need to import it
# Then use the syntax math.<name> to 
# retrieve the constant or call the function we want to use
import math

Useful constants

# Pi
math.pi
3.141592653589793
# Euler number
math.e
2.718281828459045

Ceiling: round UP to the nearest integer

# With positive input
math.ceil(3.333)
4
# With negative input
math.ceil(-3.333)
-3

Floor: round DOWN to the nearest integer

# With positive input
math.floor(3.333)
3
# With negative input
math.floor(-3.333)
-4

Roots

# Square root
math.sqrt(25)
5.0
# Another way without using math
25**(1/2)
5.0
# Fifth root
25**(1/5)
1.9036539387158786

Trig functions

# Sine
math.sin(math.pi / 2)
1.0
# Cosine
# Now you see the rounding error
math.cos(math.pi / 2)
6.123233995736766e-17

For the full list of what you can do with math, visit its official documentation page at https://docs.python.org/3/library/math.html

12.8. Summary#

Why float type?

  • Used to represent real numbers

Why the name float?

  • Real numbers are represented in the floating-point format in computers

Conversion to float

  • Use float()

  • Rules

    • bool to float: True -> 1.0 and False -> 0.0

    • int to float: 2 -> 2.0

    • str to float: the string must look like a real number

      • Whitespaces at two ends will be ignored

      • Leading and ending zeros will be ignored

    • Conversion to float is NOT ALWAYS possible

How to get an float?

  • From float literals: x = 10.5 or x = 2e3

  • From an expression that produces a float such as

    • Math operations between floats: 10.5 + 2.5

    • Call a function that returns a float: round(x, 2)

    • Typecasting: float(x)

When uses float?

  • Any time we need a real number

Operations on float

  • Math operations

  • Comparison operations

Math utils

  • Import math module

12.9. Practice#

12.9.1. Exercise 1#

Do the following

  • Create float variable x with value 100

  • Show its value and type

12.9.2. Exercise 2#

Do the following

  • Create a variable x from the division of 10 by 5

  • Show the value and type of x

12.9.3. Exercise 3#

How to quickly create a value of 7.5 billion?

12.9.4. Exercise 4#

Use math module to compute

  • The area of a circle with a radius 5

  • The hypotenuse of a right triangle with 3 and 4 being the lengths of the two other sides