# 2.3. Numeric Float¶

## 2.3.1. Definition¶

>>> data = 1.337
>>> data = +1.337
>>> data = -1.337


## 2.3.2. Notation¶

• Without leading or trailing zero

• Used by numpy

>>> data = 69.
>>> print(data)
69.0
>>>
>>> data = .44
>>> print(data)
0.44


Scientific and engineering notation

>>> million = 1e6
>>> print(million)
1000000.0
>>>
>>> million = 1E6
>>> print(million)
1000000.0

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

>>> -1e-3
-0.001
>>> -1e-4
-0.0001

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


## 2.3.3. Type Casting¶

Builtin function float() converts argument to float

>>> float(1)
1.0
>>> float(+1)
1.0
>>> float(-1)
-1.0

>>> float(1.337)
1.337
>>> float(+1.337)
1.337
>>> float(-1.337)
-1.337

>>> float('1.337')
1.337
>>> float('+1.337')
1.337
>>> float('-1.337')
-1.337

>>> float('1,337')
Traceback (most recent call last):
ValueError: could not convert string to float: '1,337'

>>> float('+1,337')
Traceback (most recent call last):
ValueError: could not convert string to float: '+1,337'

>>> float('-1,337')
Traceback (most recent call last):
ValueError: could not convert string to float: '-1,337'


## 2.3.4. Round Number¶

Rounding a number

>>> pi = 3.14159265359
>>>
>>> round(pi, 4)
3.1416
>>> round(pi, 2)
3.14
>>> round(pi)
3


Rounding a number in string formatting

>>> pi = 3.14159265359
>>>
>>> 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
>>> print(f'Pi number is {pi:.0f}')
Pi number is 3

>>> round(10.5)
10
>>> round(10.51)
11


## 2.3.5. Built-in Functions¶

Builtin function pow() raises number to the n-th power. Note, that arithmetic operator ** also raises number to the 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


Builtin function pow() can be used to calculate roots of a number:

>>> pow(4, 1/2)
2.0
>>> pow(2, 1/2)
1.4142135623730951
>>> pow(27, 1/3)
3.0

>>> pow(4, -1/2)
0.5
>>> pow(2, -1/2)
0.7071067811865476
>>> pow(27, -1/3)
0.33333333333333337

>>> pow(-2, -1)
-0.5
>>> pow(-4, -1)
-0.25


Builtin function pow() can return complex numbers. Note that Python has special complex() numeric type.

>>> pow(-2, -1/2)
(4.329780281177467e-17-0.7071067811865476j)
>>> pow(-2, 1/2)
(8.659560562354934e-17+1.4142135623730951j)
>>> pow(-4, -1/2)
(3.061616997868383e-17-0.5j)
>>> pow(-4, 1/2)
(1.2246467991473532e-16+2j)


Absolute value

>>> abs(1)
1
>>> abs(1.337)
1.337

>>> abs(-1)
1
>>> abs(-1.337)
1.337


## 2.3.6. Assignments¶

"""
* Assignment: Type Float Tax
* Required: yes
* Complexity: easy
* Lines of code: 5 lines
* Time: 3 min

English:
1. Cost of the service is 100.00 PLN net
2. Service has value added tax (VAT) rate of 23%
3. Calculate tax and gross values
4. To calculate tax, multiply net times VAT
5. To calculate gross multiply net times VAT plus 1
6. Mind the operator precedence
7. Run doctests - all must succeed

Polish:
1. Cena usługi wynosi 100.00 PLN netto
2. Usługa objęta jest 23% stawką VAT
3. Oblicz wartości podatku oraz cenę brutto
4. Aby obliczyć podatek, pomnóż cenę netto razy stawkę VAT
5. Aby obliczyć cenę brutto pomnóż cenę netto razy stawka VAT plus 1
6. Zwróć uwagę na kolejność wykonywania działań
7. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0

>>> assert net is not Ellipsis, \
'Assign result to variable: net'
>>> assert tax is not Ellipsis, \
'Assign result to variable: tax'
>>> assert gross is not Ellipsis, \
'Assign result to variable: gross'
>>> assert type(net) is float, \
'Variable net has invalid type, should be float'
>>> assert type(tax) is float, \
'Variable tax has invalid type, should be float'
>>> assert type(gross) is float, \
'Variable gross has invalid type, should be float'

>>> net / PLN
100.0
>>> tax / PLN
23.0
>>> gross / PLN
123.0
"""

PLN = 1
VAT = 23 / 100

# float: 100.0 PLN, without tax
net = ...

# float: 23% of net
tax = ...

# float: net plus tax
gross = ...


"""
* Assignment: Type Float Altitude
* Required: yes
* Complexity: easy
* Lines of code: 3 lines
* Time: 3 min

English:
1. Plane altitude is 10.000 ft
2. Data uses imperial (US) system
3. Convert to metric (SI) system
4. Result round to one decimal place
5. Run doctests - all must succeed

Polish:
1. Wysokość lotu samolotem wynosi 10 000 ft
2. Dane używają systemu imperialnego (US)
3. Przelicz je na system metryczny (układ SI)
4. Wynik zaokrąglij do jednego miejsca po przecinku
5. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0

>>> assert altitude is not Ellipsis, \
'Assign result to variable: altitude'
>>> assert altitude_m is not Ellipsis, \
'Assign result to variable: altitude_m'
>>> assert altitude_ft is not Ellipsis, \
'Assign result to variable: altitude_ft'
>>> assert type(altitude) is float, \
'Variable altitude has invalid type, should be float'
>>> assert type(altitude_m) is float, \
'Variable altitude_m has invalid type, should be float'
>>> assert type(altitude_ft) is float, \
'Variable altitude_ft has invalid type, should be float'

>>> altitude
3048.0
>>> altitude_m
3048.0
>>> altitude_ft
10000.0
"""

m = 1
ft = 0.3048 * m

# float: 10.000 ft
altitude = ...

# float: altitude in meters
altitude_m = ...

# float: altitude in feet
altitude_ft = ...


"""
* Assignment: Type Float Volume
* Required: yes
* Complexity: easy
* Lines of code: 4 lines
* Time: 3 min

English:
1. Bottle volume is 20 Fl Oz
2. Data uses imperial (US) system
3. Convert to metric (SI) system
4. Run doctests - all must succeed

Polish:
1. Objętość butelki wynosi 20 Fl Oz
2. Dane używają systemu imperialnego (US)
3. Przelicz je na system metryczny (układ SI)
4. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0

>>> assert volume is not Ellipsis, \
'Assign result to variable: volume'
>>> assert volume_floz is not Ellipsis, \
'Assign result to variable: volume_floz'
>>> assert volume_l is not Ellipsis, \
'Assign result to variable: volume_l'
>>> assert type(volume) is float, \
'Variable volume has invalid type, should be float'
>>> assert type(volume_floz) is float, \
'Variable volume_floz has invalid type, should be float'
>>> assert type(volume_l) is float, \
'Variable volume_l has invalid type, should be float'

>>> volume_floz
20.0
>>> volume_l
0.5914688
"""

liter = 1
floz = 0.02957344 * liter

# float: 20 Fl Oz
volume = ...

# float: volume in fluid ounces
volume_floz = ...

# float: volume in liters
volume_l = ...


"""
* Assignment: Type Float Distance
* Required: yes
* Complexity: easy
* Lines of code: 4 lines
* Time: 5 min

English:
1. Convert units
2. Instead ... substitute calculated and converted values
3. Note the number of decimal places
4. Run doctests - all must succeed

Polish:
1. Przekonwertuj jednostki
2. Zamiast ... podstaw wyliczone i przekonwertowane wartości
3. Zwróć uwagę na ilość miejsc po przecinku
4. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0

>>> assert meters is not Ellipsis, \
'Assign result to variable: meters'
>>> assert kilometers is not Ellipsis, \
'Assign result to variable: kilometers'
>>> assert miles is not Ellipsis, \
'Assign result to variable: miles'
>>> assert nautical_miles is not Ellipsis, \
'Assign result to variable: nautical_miles'
>>> assert all_units is not Ellipsis, \
'Assign result to variable: all_units'
>>> assert type(meters) is str, \
'Variable volume has invalid type, should be str'
>>> assert type(kilometers) is str, \
'Variable volume has invalid type, should be str'
>>> assert type(miles) is str, \
'Variable volume has invalid type, should be str'
>>> assert type(nautical_miles) is str, \
'Variable volume has invalid type, should be str'
>>> assert type(all_units) is str, \
'Variable volume has invalid type, should be str'

>>> meters
'Meters: 1337'
>>> kilometers
'Kilometers: 1'
>>> miles
'Miles: 0.83'
>>> nautical_miles
'Nautical Miles: 0.722'
>>> all_units
'km: 1, mi: 0.8, NM: 0.72'
"""

m = 1
km = 1000 * m
mi = 1609.344 * m
NM = 1852 * m

distance = 1337 * m
distance_m = distance / m
distance_km = distance / km
distance_mi = distance / mi
distance_NM = distance / NM

# str: distance in meters 0 decimal places
meters = f'Meters: {...}'

# str: distance in kilometers with 0 decimal places
kilometers = f'Kilometers: {...}'

# str: distance in miles with 2 decimal places
miles = f'Miles: {...}'

# str: distance in nautical miles with 3 decimal places
nautical_miles = f'Nautical Miles: {...}'

# str: distance in km, mi, NM with 0, 1, 2 decimal places
all_units = (f'km: {...}, '
f'mi: {...}, '
f'NM: {...}')


"""
* Assignment: Type Float Velocity
* Required: yes
* Complexity: easy
* Lines of code: 9 lines
* Time: 3 min

English:
1. Speed limit is 75 MPH
2. Data uses imperial (US) system
3. Convert to metric (SI) system
4. Speed limit print in KPH (km/h)
5. Round speed limit to one decimal place
6. Run doctests - all must succeed

Polish:
1. Ograniczenie prędkości wynosi 75 MPH
2. Dane używają systemu imperialnego (US)
3. Przelicz je na system metryczny (układ SI)
4. Ograniczenie prędkości wypisz w KPH (km/h)
5. Zaokrąglij ograniczenie prędkości do jednego miejsca po przecinku
6. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0

>>> assert kph is not Ellipsis, \
'Assign result to variable: kph'
>>> assert mph is not Ellipsis, \
'Assign result to variable: mph'
>>> assert speed_limit_mph is not Ellipsis, \
'Assign result to variable: speed_limit_mph'
>>> assert speed_limit_kph is not Ellipsis, \
'Assign result to variable: speed_limit_kph'
>>> assert type(kph) is float, \
'Variable kph has invalid type, should be float'
>>> assert type(mph) is float, \
'Variable mph has invalid type, should be float'
>>> assert type(speed_limit_mph) is float, \
'Variable speed_limit_mph has invalid type, should be float'
>>> assert type(speed_limit_kph) is float, \
'Variable speed_limit_kph has invalid type, should be float'

>>> round(kph, 3)
0.278
>>> round(mph, 3)
0.447
>>> speed_limit_mph
75.0
>>> speed_limit_kph
120.7
"""

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

m = 1
km = 1000 * m
mi = 1609.344 * m

# float: miles per hour
mph = ...

# float: kilometers per hour
kph = ...

# float: 75 miles per hour
speed_limit = ...

# float: speed limit in miles per hour, round to one decimal place
speed_limit_mph = ...

# float: speed limit in kilometers per hour, round to one decimal place
speed_limit_kph = ...


"""
* Assignment: Type Float Pressure
* Required: no
* Complexity: medium
* Lines of code: 2 lines
* Time: 3 min

English:
1. Operational pressure of EMU spacesuit: 4.3 PSI
2. Operational pressure of ORLAN spacesuit: 40 kPa
3. Calculate operational pressure in hPa for EMU
4. Calculate operational pressure in hPa for Orlan
5. Results round to one decimal place
6. Run doctests - all must succeed

Polish:
1. Ciśnienie operacyjne skafandra kosmicznego EMU (NASA): 4.3 PSI
2. Ciśnienie operacyjne skafandra kosmicznego ORLAN (Roscosmos): 40 kPa
3. Oblicz ciśnienie operacyjne skafandra EMU w hPa
4. Oblicz ciśnienie operacyjne skafandra Orlan w hPa
5. Wynik zaokrąglij do jednego miejsca po przecinku
6. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0

>>> assert emu is not Ellipsis, \
'Assign result to variable: emu'
>>> assert orlan is not Ellipsis, \
'Assign result to variable: orlan'
>>> assert type(emu) is float, \
'Variable emu has invalid type, should be float'
>>> assert type(orlan) is float, \
'Variable orlan has invalid type, should be float'

>>> orlan
400.0
>>> emu
296.5
"""

Pa = 1
hPa = 100 * Pa
kPa = 1000 * Pa
psi = 6894.757 * Pa

# float: 4.3 pounds per square inch in hectopascals, round to one decimal place
emu = ...

# float: 40 kilopascals in hectopascals, round to one decimal place
orlan = ...


"""
* Assignment: Type Float Percent
* Required: no
* Complexity: medium
* Lines of code: 2 lines
* Time: 3 min

English:
1. International Standard Atmosphere (ISA) at sea level is
1 ata = 1013.25 hPa
2. Calculate pO2 - partial pressure of Oxygen at sea level in hPa
3. Round result to one decimal place
4. To calculate partial pressure use ratio
(100% is 1013.25 hPa, 20.946% is how many hPa?)
5. Run doctests - all must succeed

Polish:
1. Międzynarodowa standardowa atmosfera (ISA) na poziomie morza wynosi
1 ata = 1013.25 hPa
2. Oblicz pO2 - ciśnienie parcjalne tlenu na poziomie morza w hPa
3. Wynik zaokrąglij do jednego miejsca po przecinku
4. Aby policzyć ciśnienie parcjalne skorzystaj z proporcji
(100% to 1013.25 hPa, 20.946% to ile hPa?)
5. Uruchom doctesty - wszystkie muszą się powieść

Hints:
* 1 hPa = 100 Pa
* 1 kPa = 1000 Pa
* 1 ata = 1013.25 hPa (ISA - International Standard Atmosphere)
* Atmosphere gas composition:

* Nitrogen 78.084%
* Oxygen 20.946%
* Argon 0.9340%
* Carbon Dioxide 0.0407%
* Others 0.001%

Tests:
>>> import sys; sys.tracebacklimit = 0

>>> assert pO2 is not Ellipsis, \
'Assign result to variable: pO2'
>>> assert type(pO2) is float, \
'Variable pO2 has invalid type, should be float'

>>> pO2
212.2
"""

PERCENT = 100
N2 = 78.084 / PERCENT
O2 = 20.946 / PERCENT
Ar = 0.9340 / PERCENT
CO2 = 0.0407 / PERCENT
Others = 0.001 / PERCENT

Pa = 1
hPa = 100 * Pa
kPa = 1000 * Pa

# float: pressure at sea level: 1013.25 hectopascals
ata = ...

# float: 20.946% of pressure at sea level in hectopascals
pO2 = ...


"""
* Assignment: Type Float Gradient
* Required: no
* Complexity: hard
* Lines of code: 7 lines
* Time: 8 min

English:
1. At what altitude above sea level, pressure is equal
to partial pressure of Oxygen
2. Print result in meters rounding to two decimal places
3. To calculate partial pressure use ratio
(100% is 1013.25 hPa, 20.946% is how many hPa?)
4. Calculated altitude is pressure at sea level minus
oxygen partial pressure divided by gradient
5. Mind the operator precedence
6. Run doctests - all must succeed

Polish:
1. Na jakiej wysokości nad poziomem morza panuje ciśnienie
równe ciśnieniu parcjalnemu tlenu?
2. Wypisz rezultat w metrach zaokrąglając do dwóch miejsc po przecinku
3. Aby policzyć ciśnienie parcjalne skorzystaj z proporcji
(100% to 1013.25 hPa, 20.946% to ile hPa?)
4. Wyliczona wysokość to ciśnienie atmosferyczne na poziomie morza minus
ciśnienie parcjalne tlenu podzielone przez gradient
5. Zwróć uwagę na kolejność wykonywania działań
6. Uruchom doctesty - wszystkie muszą się powieść

Hints:
* pressure gradient (decrease) = 11.3 Pa / 1 m
* 1 hPa = 100 Pa
* 1 kPa = 1000 Pa
* 1 ata = 1013.25 hPa (ISA - International Standard Atmosphere)
* Atmosphere gas composition:

* Nitrogen 78.084%
* Oxygen 20.946%
* Argon 0.9340%
* Carbon Dioxide 0.0407%
* Others 0.001%

Tests:
>>> import sys; sys.tracebacklimit = 0

>>> assert pO2 is not Ellipsis, \
'Assign result to variable: pO2'
>>> assert gradient is not Ellipsis, \
'Assign result to variable: gradient'
>>> assert altitude is not Ellipsis, \
'Assign result to variable: altitude'
>>> assert type(pO2) is float, \
'Variable pO2 has invalid type, should be float'
>>> assert type(gradient) is float, \
'Variable gradient has invalid type, should be float'
>>> assert type(altitude) is float, \
'Variable altitude has invalid type, should be float'

>>> pO2
21223.5345
11.3
>>> round(altitude/m, 2)
7088.63
"""

PERCENT = 100
N2 = 78.084 / PERCENT
O2 = 20.946 / PERCENT
Ar = 0.9340 / PERCENT
CO2 = 0.0407 / PERCENT
Others = 0.001 / PERCENT

m = 1
Pa = 1
hPa = 100 * Pa
ata = 1013.25 * hPa
pO2 = O2 * ata

# float: 11.3 Pascals per meter