# 3.2. Type float

## 3.2.1. Type Definition

• Notation without leading or trailing zero is used by numpy

Listing 13. float Type Definition
value = 13.37           # 13.37
value = -13.37          # -13.37

Listing 14. Notation without leading or trailing zero
value = 10.             # 10.0
value = .44             # 0.44

Listing 15. 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


## 3.2.2. Type Casting

• float() converts argument to float

Listing 16. float() converts argument to float
float(10)                   # 10.0
float(-10)                  # -10.0
float(10.5)                 # 10.5
float(-10.5)                # -10.5
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'


## 3.2.3. Rounding Numbers

• round() - Rounds a number

Listing 17. round() - Rounds a number
round(3.1415926)        # 3
round(3.1415926, 2)     # 3.14
round(3.1415926, 4)     # 3.1416

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

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

print(f'{pi:.2f}')      # 3.14
print(f'{pi:.4f}')      # 3.1416


## 3.2.4. Numeric Functions

• abs() - Absolute value

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

Listing 19. abs() - Absolute value
abs(1)                      # 1
abs(-1)                     # 1
abs(13.37)                  # 13.37
abs(-13.37)                 # 13.37


## 3.2.5. Assignments

### 3.2.5.1. Float Example

English
1. Input data (see below)

2. Use Imperial (US) measurements system

3. Convert to metric (SI) system

4. Speed limit round to one decimal place

Polish
1. Dane wejściowe (patrz sekcja input)

2. Używają system Imperialny (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

### 3.2.5.2. Float Casting

English
1. Declare variable for storing value of 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. Zdefiniuj zmienną dla przechowywania wartości 1337 metrów

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

3. Użyj kodu wyjściowego (patrz sekcja input) jako szablonu

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

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

Non-functional requirements
• Do not use input()

Input
print(f'Meters: {...}')                              # int
print(f'Kilometers: {...}')                          # float
print(f'Miles: {...}')                               # float (two decimal places)
print(f'Nautical Miles: {...}')                      # float (two decimal places)
print(f'm: {...}, km: {...}, mi: {...}, nm: {...}')  # int, int, float (one decimal place), float (one decimal place)

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

### 3.2.5.3. Float Calculation

• Complexity level: medium

• Lines of code to write: 20 lines

• Estimated time of completion: 20 min

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

8. All results print in kPa and PSI rounding to two decimal places

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. Na 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

Figure 16. EMU and Orlan

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%