Challenge - 5 Problems
Float Type Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember that float16 has limited precision and rounds values.
✗ Incorrect
The float16 type rounds the values to fewer decimal places, so the sum is approximately 4.11, not the more precise float64 sum.
❓ data_output
intermediate1: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)
Attempts:
2 left
💡 Hint
Check the number of bytes each float type uses in NumPy.
✗ Incorrect
float16 uses 2 bytes, float32 uses 4 bytes, and float64 uses 8 bytes.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Think about precision differences between float32 and float64.
✗ Incorrect
float32 has fewer bits for precision, so summing many small values accumulates rounding errors, unlike float64.
🧠 Conceptual
advanced1:30remaining
Choosing float types for large datasets
Which is the best reason to choose float16 over float64 for a large dataset?
Attempts:
2 left
💡 Hint
Think about memory and precision trade-offs.
✗ Incorrect
float16 uses less memory but has lower precision, useful when memory is limited and precision is less critical.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Use absolute difference and check if any value exceeds the threshold.
✗ Incorrect
Option A correctly computes absolute differences and checks if any difference is greater than 1e-6, indicating precision loss.