2.2. Type Float

2.2.1. Type Definition

  • Notation without leading or trailing zero is used by numpy

Listing 2.3. float Type Definition
data = 13.37             # 13.37
data = -13.37            # -13.37
Listing 2.4. Notation without leading or trailing zero
data = 10.               # 10.0
data = .44                # 0.44
Listing 2.5. Engineering notation
million = 1e6           # 1000000.0
million = 1E6           # 1000000.0

+1e6                    # 1000000.0
-1e6                    # -1000000.0

1e-3                    # 0.001
1e-4                    # 0.0001
1e-5                    # 1e-05
1e-6                    # 1e-06

1.337 * 1e3             # 1337.0
1.337 * 1e-3            # 0.001337

2.2.2. Type Casting

  • float() converts argument to float

Listing 2.6. float() converts argument to float
float(13)               # 13.0
float(-13)              # -13.0
float(13.37)            # 13.37
float(-13.37)           # -13.37

float('+13.37')         # 13.37
float('-13.37')         # -13.37
float('13,37')          # ValueError: could not convert string to float: '13,37'
float('-13,37')         # ValueError: could not convert string to float: '-13,37'

2.2.3. Round Number

  • round() - Rounds a number

Listing 2.7. round() - Rounds a number
pi = 3.14159265359

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

print(f'Pi number is {pi}')         # Pi number is 3.14159265359
print(f'Pi number is {pi:f}')       # Pi number is 3.141593
print(f'Pi number is {pi:.4f}')     # Pi number is 3.1416
print(f'Pi number is {pi:.2f}')     # Pi number is 3.14

2.2.4. Built-in Functions

  • abs() - Absolute value

  • pow() - Number to the n-th power

  • Note, that arithmetic operator ** also raises number to the power

Listing 2.8. pow() - Number to the n-th power
pow(10, 2)          # 100
pow(2, -1)          # 0.5

pow(1.337, 3)       # 2.389979753
pow(4, 0.5)         # 2.0
pow(2, 0.5)         # 1.4142135623730951
Listing 2.9. abs() - Absolute value
abs(1)                      # 1
abs(13.37)                  # 13.37

abs(-1)                     # 1
abs(-13.37)                 # 13.37

2.2.5. Assignments

2.2.5.1. Float Example

English
  1. Use data from "Input" section (see below)

  2. Data uses imperial (US) system

  3. Convert to metric (SI) system

  4. Speed limit round to one decimal place

Polish
  1. Użyj danych z sekcji "Input" (patrz poniżej)

  2. Dane używają systemu imperialnego (US)

  3. Przelicz je na system metryczny (układ SI)

  4. Ograniczenie prędkości zaokrąglij do jednego miejsca po przecinku

Input
  • Plane altitude: 10.000 ft

  • Bottle volume: 20 Fl Oz

  • Speed limit: 75 mph

Solution
SECOND = 1
MINUTE = 60 * SECOND
HOUR = 60 * MINUTE

M = 1
KM = 1000 * M
FT = 0.3048 * M
MI = 1609.344 * M
KPH = KM / HOUR
MPH = MI / HOUR

LITER = 1
FLOZ = 0.02957344 * LITER

plane_altitude = 10_000 * FT
bottle = 20 * FLOZ
speed_limit = 75 * MI/HOUR


print(f'Plane altitude: {plane_altitude/M} m')
# Plane altitude: 3048.0 m

print(f'Bottle volume: {bottle/LITER} l')
# Bottle volume: 0.5914688 l

print(f'Speed limit: {speed_limit/KPH:.1f} km/h')
# Speed limit: 120.7 km/h
The whys and wherefores
  • Defining constants and variables

  • Naming convention

  • Mathematical operations

2.2.5.2. Float Casting

English
  1. Use code from "Input" section (see below)

  2. Convert units

  3. Instead ... substitute calculated and converted values

  4. Compare result with "Output" section (see below)

Polish
  1. Użyj kodu z sekcji "Input" (patrz poniżej)

  2. Przekonwertuj jednostki

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

  4. Porównaj wyniki z sekcją "Output" (patrz poniżej)

Input
m = 1337

print(f'Meters: {...}')
print(f'Kilometers: {...}')
print(f'Miles: {...}')
print(f'Nautical Miles: {...}')
print(f'm: {...}, km: {...}, mi: {...}, nm: {...}')
Output
Meters: 1337
Kilometers: 1.337
Miles: 0.83
Nautical Miles: 0.72
All: 1337, 1, 0.8, 0.7
The whys and wherefores
  • Defining constants and variables

  • Naming convention

  • Print formatting

  • Mathematical operations

  • Separation of business logic and view

Hints
  • 1 km = 1000 m

  • 1 mile = 1609.344 m

  • 1 nautical mile = 1852 m

2.2.5.3. Float Calculation

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. Print all results in kPa and PSI rounding to two decimal places

  6. Compare result with "Output" section (see below)

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. Wypisz wszystkie wyniki w kPa oraz PSI zaokrąglając do dwóch miejsc po przecinku

  6. Porównaj wyniki z sekcją "Output" (patrz poniżej)

Output
EMU operating pressure: 29.65 kPa, 4.30 psi
Orlan operating pressure: 40.00 kPa, 5.80 psi
The whys and wherefores
  • Defining constants and variables

  • Naming convention

  • Print formatting

  • Mathematical operations

  • Separation of business logic and view

../../_images/spacesuits.png

Figure 2.1. EMU and Orlan

Hint
  • 1 hPa = 100 Pa

  • 1 psi = 6894.757 Pa

2.2.5.4. Float Percent

English
  1. Calculate International Standard Atmosphere pressure at sea level

  2. Calculate partial pressure of Oxygen at sea level

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

  4. Print all results in kPa rounding to two decimal places

  5. Compare result with "Output" section (see below)

Polish
  1. Oblicz ciśnienie standardowej atmosfery na poziomie morza

  2. Oblicz ciśnienie parcjalne tlenu na poziomie morza

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

  4. Wypisz wszystkie wyniki w kPa zaokrąglając do dwóch miejsc po przecinku

  5. Porównaj wyniki z sekcją "Output" (patrz poniżej)

Output
International Standard Atmosphere: 1013.25 hPa
O2 partial pressure at sea level: 212.24 hPa
Oxygen starvation altitude: 7088.63 m
The whys and wherefores
  • Defining constants and variables

  • Naming convention

  • Print formatting

  • Mathematical operations

  • Separation of business logic and view

Hint
  • 1 hPa = 100 Pa

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

  • pressure gradient = -11.3 Pa / 1 m

  • Atmosphere gas composition:

    • Nitrogen 78.084%

    • Oxygen 20.946%

    • Argon 0.9340%

    • Carbon Dioxide 0.0407%

    • Others 0.001%