FV-SOL-2-C2 Floating Point

TLDR

Floating-point numbers can’t represent every decimal exactly. For example, 0.3 might be stored as 0.30000000000000004 in a system that uses floating-point arithmetic.

When you divide and multiply these imprecise values, the error compounds. For example, (0.3 / 1000.0) * 1.0 could produce something like 0.00030000000000000004 instead of the expected 0.0003.

This often occurs when dealing with fractional values or very large numbers, where small rounding errors can accumulate and lead to inaccurate calculations or financial discrepancies.

Game

What is the result of userReward? can you tell?

// SPDX-License-Identifier: MIT
// Open me in VSCode and really think before opening the hints!
// Add @audit tags wherever suspicious
// Go to the solidity docs to complete missing knowledge of what's happening here
// Solve by drafting a fix!

totalHoldings = 1000.0; // Total holdings
userHoldings = 0.3;     // User's fractional holdings
totalReward = 1.0;      // Total reward to be distributed

// Calculate user's reward
userReward = (userHoldings / totalHoldings) * totalReward; 

Last updated