0
0
NumPydata~20 mins

Float types (float16, float32, float64) in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Float Type Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of NumPy float type conversion
What is the output of this code snippet that converts a float64 array to float16 and sums it?
NumPy
import numpy as np
arr = np.array([1.123456789, 2.987654321], dtype=np.float64)
arr16 = arr.astype(np.float16)
sum_val = arr16.sum()
print(sum_val)
A4.11112
B4.11111
C4.111114
D4.11
Attempts:
2 left
💡 Hint
Remember that float16 has limited precision and rounds values.
data_output
intermediate
1:30remaining
Size in bytes of different float types
What are the sizes in bytes of numpy float16, float32, and float64 types?
NumPy
import numpy as np
sizes = (np.dtype(np.float16).itemsize, np.dtype(np.float32).itemsize, np.dtype(np.float64).itemsize)
print(sizes)
A(2, 4, 8)
B(1, 4, 8)
C(2, 8, 16)
D(4, 8, 16)
Attempts:
2 left
💡 Hint
Check the number of bytes each float type uses in NumPy.
🔧 Debug
advanced
2:30remaining
Why does this float32 array sum differ from float64?
Given this code, why does the sum of the float32 array differ from the float64 array sum?
NumPy
import numpy as np
arr64 = np.array([0.1]*1000000, dtype=np.float64)
arr32 = arr64.astype(np.float32)
sum64 = arr64.sum()
sum32 = arr32.sum()
print(sum64, sum32)
Afloat32 arrays convert values to integers before summing
Bfloat64 arrays are slower so sum is inaccurate
Cfloat32 has less precision causing rounding errors in sum
Dfloat64 arrays use less memory causing overflow
Attempts:
2 left
💡 Hint
Think about precision differences between float32 and float64.
🧠 Conceptual
advanced
1:30remaining
Choosing float types for large datasets
Which is the best reason to choose float16 over float64 for a large dataset?
ATo ensure compatibility with all Python libraries
BTo reduce memory usage while accepting lower precision
CTo speed up calculations by using 64-bit floats
DTo increase precision for scientific calculations
Attempts:
2 left
💡 Hint
Think about memory and precision trade-offs.
🚀 Application
expert
3:00remaining
Detecting precision loss when converting float64 to float32
Which code snippet correctly detects if converting a float64 numpy array to float32 causes any value to change beyond 1e-6 difference?
A
import numpy as np
arr64 = np.array([1.000001, 2.000001], dtype=np.float64)
arr32 = arr64.astype(np.float32)
diff = np.abs(arr64 - arr32)
print(np.any(diff > 1e-6))
B
import numpy as np
arr64 = np.array([1.000001, 2.000001], dtype=np.float64)
arr32 = arr64.astype(np.float32)
diff = np.abs(arr64 - arr32)
print(np.all(diff < 1e-6))
C
import numpy as np
arr64 = np.array([1.000001, 2.000001], dtype=np.float64)
arr32 = arr64.astype(np.float32)
diff = arr64 + arr32
print(np.any(diff > 1e-6))
D
import numpy as np
arr64 = np.array([1.000001, 2.000001], dtype=np.float64)
arr32 = arr64.astype(np.float32)
diff = arr64 - arr32
print(np.all(diff < 1e-6))
Attempts:
2 left
💡 Hint
Use absolute difference and check if any value exceeds the threshold.