2
//rounds to zero if x*y < WAD / 2
function wmul(uint x, uint y) internal pure returns (uint z) {
    z = add(mul(x, y), WAD / 2) / WAD;
}
//rounds to zero if x*y < WAD / 2
function rmul(uint x, uint y) internal pure returns (uint z) {
    z = add(mul(x, y), RAY / 2) / RAY;
}

I think it'll add artifact small number ex. 2WAD x 1WAD = 2WAD + 0.5 Is it for safety reasons?

and what is

//rounds to zero if x*y < WAD / 2

I can’t figure it out.

DS-Math

2 Answers2

4

x * y < WAD / 2 ==>

x * y + WAD / 2 < WAD ==>

(x * y + WAD / 2) / WAD == 0

goodvibration
  • 26,003
  • 5
  • 46
  • 86
1

So the rounds to zero comment is true regardless of addition of WAD / 2 since solidity always rounds down to zero. The case that + WAD / 2 does is rounds the value up when the tenths place of the fixed point decimal is greater than 5.

Looking at some examples expressed in decimals (in Solidity this is in WAD precision):

if x * y = 1.2 then x * y + 0.5 = 1.7. Both of these round down to 1 in Solidity

if x * y = 1.7 then x * y + 0.5 = 2.2. The first equation will round down to 1 and the second equation will round down to 2.

This is the effect of adding WAD / 2.

Jeff Wu
  • 111
  • 2