2. float

2.1. Defining float

value = 10.5        # 10.5

2.1.1. Notation without leading or trailing zero

  • Used by numpy

value = 10.         # 10.0
value = .44         # 0.44

2.1.2. Engineering notation

million = 1e6       # 1000000.0
million = 1E6       # 1000000.0
million = +1E6      # 1000000.0
million = -1E6      # -1000000.0
float(+1E6)         # 1000000.0
float(-1E3)         # -1000.0
float(1e-4)         # 0.0001
float(1E-3)         # 0.001

float(1e-5)         # 1e-05
float(1E-5)         # 1E-05

2.1.3. Maximal and minimal float values

import sys

sys.float_info.min  # 2.2250738585072014e-308
sys.float_info.max  # 1.7976931348623157e+308

2.1.4. Infinity representation

1e308               # 1e+308
1e309               # inf
float('-inf')       # -inf
float('-Infinity')  # -inf
float('inf')        # inf
float('Infinity')   # inf

2.2. Converting to float

  • Also known as "type casting"

  • float() converts argument to float

float(10.5)         # 10.5
float(10)           # 10.0
float(1.23)         # 1.23
float(-1.23)        # -1.23
float('+1.23')      # 1.23
float('-1.23')      # -1.23

float('1,23')       # ValueError: could not convert string to float: '1,23'
float('-1,23')      # ValueError: could not convert string to float: '-1,23'

2.3. Numeric Functions

2.3.1. Rounding numbers

pi = 3.14159265359

round(pi)       # 3
round(pi, 2)    # 3.14
round(pi, 4)    # 3.1416

2.3.2. Absolute value

abs(1.5)        # 1.5
abs(-1.5)       # 1.5

2.3.3. Number to the n-th power

pow(2, -1)      # 0.5
pow(2.888, 3)   # 24.087491072
2 ** -1         # 0.5
2.888 ** 3      # 24.087491072

2.4. Assignments

2.4.1. Handling user input and type casting

  • Complexity level: easy

  • Lines of code to write: 4 lines

  • Estimated time of completion: 5 min

  • Filename: solution/float_casting.py

English
  1. User typed distance 1337 meters

  2. Print values in other units

  3. Use code output (see below) as a template

  4. Convert data to types shown in comments at the right side

  5. Instead ... substitute calculated and converted values

Polish
  1. Użytkownik wprowadził odległość 1337 metrów

  2. Wypisz wartość w różnych jednostkach

  3. Użyj kodu wyjściowego (patrz poniżej) jako szablonu

  4. Przekonwertuj dane do typów podanych w komentarzu po prawej stronie

  5. Zamiast ... podstaw wyliczone i przekonwertowane wartości

Output
print(f'Meters: {...}')                              # int
print(f'Kilometers: {...}')                          # int
print(f'Miles: {...}')                               # float
print(f'Nautical Miles: {...}')                      # float
print(f'm: {...}, km: {...}, mi: {...}, nm: {...}')  # int, int, float, float
The whys and wherefores
  • Defining constants and variables

  • Naming convention

  • Print formatting

  • Mathematical operations

  • Separation of business logic and view

Hints
  • 1000 m = 1 km

  • 1608 m = 1 mile

  • 1852 m = 1 nautical mile

2.4.2. SI Unit Conversion

  • Complexity level: medium

  • Lines of code to write: 20 lines

  • Estimated time of completion: 20 min

  • Filename: solution/float_pressure.py

English
  1. Operational pressure of EMU spacesuit: 4.3 PSI

  2. Operational pressure of ORLAN spacesuit: 400 hPa

  3. Calculate operational pressure in kPa for EMU

  4. Calculate operational pressure in PSI for Orlan

  5. Calculate International Standard Atmosphere pressure at sea level

  6. Calculate partial pressure of Oxygen at sea level

  7. At what altitude above sea level, pressure is equal to partial pressure of Oxygen

Polish
  1. Ciśnienie operacyjne skafandra kosmicznego EMU: 4.3 PSI

  2. Ciśnienie operacyjne skafandra kosmicznego ORLAN: 400 hPa

  3. Oblicz ciśnienie operacyjne skafandra EMU w kPa

  4. Oblicz ciśnienie operacyjne skafandra Orlan w PSI

  5. Oblicz ciśnienie standardowej atmosfery na poziomie morza

  6. Oblicz ciśnienie parcjalne tlenu na poziomie morza

  7. Oa jakiej wysokości nad poziomem morza panuje ciśnienie równe ciśnieniu parcjalnemu tlenu?

  8. Wszystkie wyniki podaj w kPa oraz w PSI zaokrąglając do dwóch miejsc po przecinku

The whys and wherefores
  • Defining constants and variables

  • Naming convention

  • Print formatting

  • Mathematical operations

  • Separation of business logic and view

Hint
  • 1 ata = 1013.25 hPa (ISA - International Standard Atmosphere)

  • 1 psi = 6894.757 Pa

  • pressure gradient = -11.3 Pa / 1 meter

  • Atmosphere gas composition:

    • Nitrogen 78.084%

    • Oxygen 20.946%

    • Argon 0.9340%

    • Carbon Dioxide 0.0407%

    • Others 0.001%