Bird
Raised Fist0
NumPydata~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which numpy float type uses the least amount of memory?
easy
A. float16
B. float32
C. float64
D. float128

Solution

  1. Step 1: Recall memory sizes of float types

    float16 uses 2 bytes, float32 uses 4 bytes, and float64 uses 8 bytes.
  2. Step 2: Identify the smallest memory usage

    Among these, float16 uses the least memory (2 bytes).
  3. Final Answer:

    float16 -> Option A
  4. Quick Check:

    Smallest memory float = float16 [OK]
Hint: Smaller float number means less memory used [OK]
Common Mistakes:
  • Confusing float32 as smallest
  • Thinking float64 uses less memory
  • Assuming float128 is standard
2. Which of the following is the correct way to create a NumPy array with float32 type?
easy
A. np.array([1.0, 2.0], dtype='float32')
B. np.array([1.0, 2.0], type='float32')
C. np.array([1.0, 2.0], dtype=float16)
D. np.array([1.0, 2.0], float='float32')

Solution

  1. Step 1: Recall correct parameter for data type in np.array

    The correct parameter is dtype, not type or float.
  2. Step 2: Check correct dtype value for float32

    Using dtype='float32' is correct syntax to specify float32 type.
  3. Final Answer:

    np.array([1.0, 2.0], dtype='float32') -> Option A
  4. Quick Check:

    Use dtype='float32' in np.array [OK]
Hint: Use dtype='float32' to set float type in np.array [OK]
Common Mistakes:
  • Using 'type' instead of 'dtype'
  • Passing float='float32' parameter
  • Using wrong dtype like float16 when asked for float32
3. What is the output dtype of this code?
import numpy as np
arr = np.array([1.5, 2.5], dtype=np.float16)
print(arr.dtype)
medium
A. float32
B. float64
C. int16
D. float16

Solution

  1. Step 1: Check dtype argument in np.array

    The array is created with dtype=np.float16, so the array elements are stored as float16.
  2. Step 2: Print dtype attribute

    Printing arr.dtype will show float16 as the data type.
  3. Final Answer:

    float16 -> Option D
  4. Quick Check:

    dtype set to float16 prints float16 [OK]
Hint: dtype attribute shows the array's float type [OK]
Common Mistakes:
  • Assuming default float64 dtype
  • Confusing float16 with float32
  • Thinking dtype prints data values
4. Identify the error in this code snippet:
import numpy as np
arr = np.array([1.0, 2.0], dtype='float16')
arr = arr.astype('float32')
print(arr.dtype)
medium
A. dtype argument must be np.float32, not string
B. No error; code runs and prints float32
C. astype cannot convert float16 to float32
D. print statement syntax is wrong

Solution

  1. Step 1: Check dtype usage in np.array

    Using dtype='float16' is valid and creates a float16 array.
  2. Step 2: Check astype conversion and print

    astype('float32') converts array to float32 correctly. The print statement syntax is correct.
  3. Final Answer:

    No error; code runs and prints float32 -> Option B
  4. Quick Check:

    astype converts float16 to float32 fine [OK]
Hint: astype converts float types smoothly [OK]
Common Mistakes:
  • Thinking astype can't convert float16 to float32
  • Believing dtype must be np.float32 object
  • Assuming print syntax error
5. You have a large dataset of decimal numbers but limited memory. You want to store the data with the smallest float type without losing too much precision. Which float type should you choose?
hard
A. float64 for maximum precision
B. int32 since decimals are not needed
C. float32 as a balance of precision and memory
D. float16 for smallest memory regardless of precision

Solution

  1. Step 1: Understand memory vs precision tradeoff

    float16 uses least memory but has low precision, which may lose important decimal details.
  2. Step 2: Choose float type balancing memory and precision

    float32 uses moderate memory and keeps good precision, suitable for large datasets with decimals.
  3. Final Answer:

    float32 as a balance of precision and memory -> Option C
  4. Quick Check:

    Balance memory and precision with float32 [OK]
Hint: Pick float32 for good precision and moderate memory [OK]
Common Mistakes:
  • Choosing float16 losing too much precision
  • Always picking float64 ignoring memory
  • Using int32 when decimals matter
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard