Challenge - 5 Problems
Floating Point Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of floating point addition with float and double
What is the output of this C# code snippet?
C Sharp (C#)
float a = 0.1f; double b = 0.1; Console.WriteLine(a == b);
Attempts:
2 left
💡 Hint
Consider how float and double represent decimal numbers differently.
✗ Incorrect
The float and double types have different precision and internal representation. Comparing a float to a double directly with == returns false because 0.1f and 0.1 (double) are not exactly the same value.
❓ Predict Output
intermediate2:00remaining
Decimal precision in financial calculation
What is the output of this C# code?
C Sharp (C#)
decimal price = 19.99m; decimal quantity = 3m; decimal total = price * quantity; Console.WriteLine(total);
Attempts:
2 left
💡 Hint
Decimal type is designed for exact decimal representation.
✗ Incorrect
Decimal type stores exact decimal values, so multiplying 19.99 by 3 results in exactly 59.97 without floating point rounding errors.
❓ Predict Output
advanced2:00remaining
Result of mixed type arithmetic with float, double, and decimal
What is the output of this C# code?
C Sharp (C#)
float f = 1.5f; double d = 2.5; decimal m = 3.5m; var result = f + (float)d; Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Consider whether these values are exactly representable in float precision.
✗ Incorrect
Both 1.5 and 2.5 are exactly representable in single-precision float (and double). Casting double 2.5 to float gives the exact value 2.5f, and 1.5f + 2.5f = 4.0f exactly, which Console.WriteLine prints as "4".
❓ Predict Output
advanced2:00remaining
Effect of floating point precision on equality check
What does this C# code print?
C Sharp (C#)
double x = 0.1 + 0.2; bool equal = x == 0.3; Console.WriteLine(equal);
Attempts:
2 left
💡 Hint
Floating point addition can introduce small errors.
✗ Incorrect
Due to floating point precision, 0.1 + 0.2 is not exactly 0.3 in double precision, so the equality check returns false.
🧠 Conceptual
expert2:00remaining
Choosing the correct floating point type for currency calculations
Which floating point type should you use in C# for precise currency calculations to avoid rounding errors?
Attempts:
2 left
💡 Hint
Think about which type stores decimal numbers exactly without binary rounding errors.
✗ Incorrect
The decimal type is designed for financial and monetary calculations because it stores decimal numbers exactly, avoiding rounding errors common in float and double.