Hot answers tagged

24 votes
Accepted

Can't do any integer division

Problem is 4 & 5 fit in less that 256 bits (each). You end up with tiny uints in the constant expression, and then those aren't easily converted to the uint256, so ... cast the type explicitly. ...
user avatar
  • 51.9k
6 votes
Accepted

What does "mulmod" do in Solidity?

As per the Solidity cheatsheet, mulmod is a function available at the global scope. mulmod(uint x, uint y, uint k) returns (uint): compute (x * y) % k where the multiplication is performed with ...
user avatar
4 votes

Can't do any integer division

as an addition to @rob's answer you can use : function calcul(uint a, uint b, uint precision) view returns ( uint) { return a*(10**precision)/b; } If we divide using the function above 7/3 ...
user avatar
  • 17.6k
4 votes
Accepted

Why DS-Math add WAD/2 or RAY/2 in WAD,RAY multiply function

x * y < WAD / 2 ==> x * y + WAD / 2 < WAD ==> (x * y + WAD / 2) / WAD == 0
user avatar
4 votes
Accepted

How to take the average of two uint256s without overflowing?

After posting the question, I realised I gave the answer myself. I simply have to check if both x and y are odd numbers and, if yes, add 1 to the result: function avg(uint256 x, uint256 y) external ...
user avatar
4 votes
Accepted

Does the unchecked { ... } block apply only to the current function?

Looking at the documentation for Solidity 0.8.0 it doesn't seem so like the unchecked block in bar disables the safety checks in foo.. The setting only affects the statements that are syntactically ...
user avatar
3 votes
Accepted

How to convert uint8 to uint256 bitmap in solidity?

You would use the shift operator. In your example: uint8 value = 9; uint256 mask = 1 << value; See https://docs.soliditylang.org/en/v0.8.10/types.html#integers
user avatar
  • 3,869
3 votes
Accepted

What's the most efficient way to check if a number is a power of ten in Solidity?

I can think of four ways, ordered below from least efficient to most efficient. I didn't include any gas costs because they vary based on the value of x, whether you use the compiler optimizer, what's ...
user avatar
3 votes
Accepted

Can division underflow or overflow in Solidity?

Yes, there is at least one situation where overflow can occur. I don't know about underflows though. When you divide the minimum of a signed type by -1, you get the mirror image of that number in the ...
user avatar
2 votes
Accepted

How can I use decimal in solidity

Maintain every non-integer entity in your code as a pair of numerator and denominator. If the same denominator is used everywhere (for example, 100 in your case), then you can maintain only the ...
user avatar
2 votes
Accepted

How can you cheaply choose from a dynamic array randomly, with an oracle generated seed number, in Solidity?

Good Vibes is right. Modulo and you're done. The discussion gives me the impression that you don't fully understand how the remainder function solves it. There is no divide by zero, no need to ...
user avatar
  • 51.9k
2 votes

Using SafeMath with older Solidity compiler

SafeMath is used to protect your contract against math errors, such as overflow (adding uint above the max uint, for instance). SafeMath will revert the transaction if bad math happens in a ...
user avatar
  • 3,257
2 votes

Using SafeMath with older Solidity compiler

Solidity had a historical issue with arithmetic overflows and underflows, that's why having SafeMath enabled was a necessity back in the day. SafeMath has been made obsolete starting from Solidity v0....
user avatar
2 votes
Accepted

How to add three numbers using safemath?

Writing 5.add(7) is a syntax sugar for add(5, 7). So first expression 5.add(7.add(8)) is add(5, add(7, 8)) and the second one 5.add(7).add(8) is add(add(5, 7), 8). In the particular case of SafeMath's ...
user avatar
  • 29.6k
2 votes
Accepted

Is the modulo operation cheaper in unchecked arithmetic?

Yes, modulo is cheaper in unchecked arithmetic! Take the following code: pragma solidity ^0.8.0; contract Modulos { function foo(uint256 x, uint256 y) external view returns (uint256 result, ...
user avatar
2 votes
Accepted

How to handle division by zero in solidity?

Division by zero is an exception that will result in a reverted transaction. function foo(uint num, uint den) public pure returns(uint result) { if(den == 0) return 0; // there is no "correct&...
user avatar
  • 51.9k
2 votes
Accepted

Is solidity compiler rounding floating point numbers during the complilation?

The compiler does accept decimals in an expression if the resulting value is valid. For example uint256 x = 0.23 * 100 is valid. The expression uint256 y = 0.123 * 100 fails to compile with TypeError:...
user avatar
  • 29.6k
2 votes

How to write a linear formula for percentage in Solidity?

To represent decimal numbers, you can use integers and assumes that the first couple of digits are used as decimal places. It is common to use 18 decimal places. When working with fixed-point numbers, ...
user avatar
  • 502
1 vote
Accepted

Exponent sign not working, only double star instead, why?

Looking at the latest Solidity docs there are both operation for integers available: ** - exponentiation ^ - bitwise exclusive or So in your case only ** makes sense. I am not aware of any version ...
user avatar
  • 3,869
1 vote
Accepted

What happens if you subtract two uint256 values whose answer is supposed to be negative?

solidity (prior to version 0.8.x) doesn't check for over/underflows. That means that if you're if you're trying to set it to less than 0 (or more than (2^256 -1) it's going to wrap around (0-1 = 2^256-...
user avatar
  • 1,328
1 vote
Accepted

Do the Solidity v0.8 overflow checks apply to assembly operations too?

No, they apply only to non-assembly code. Check for yourself on Remix: // SPDX-License-Identifier: MIT pragma solidity >=0.8.0; contract Mul { function mul_normal(uint8 x, uint8 y) external ...
user avatar
1 vote
Accepted

Rounding fixed point integers

They are integers so you can round them like they were integers. To round integer A to B proceed as following trunc(A / B) * B. // SPDX-License-Identifier: MIT pragma solidity >=0.4.0 <0.9.0; ...
user avatar
  • 29.6k
1 vote
Accepted

Distribution coefficient

Set Y to be (1/X). X being the sum of all units staked (100 in your first example, 120 in second example). If you divide each user's stake by the total amount staked, you get the user's percentage of ...
user avatar
1 vote

Multiply number with Pi in solidity

Use PRBMath! // SPDX-License-Identifier: Unlicense pragma solidity >=0.8.4; import "prb-math/contracts/PRBMathSD59x18.sol"; contract SignedConsumer { using PRBMathSD59x18 for int256; ...
user avatar
1 vote
Accepted

Calculate Percentage

This kind of rounding issues are quite typical in crypto projects. You have two options: 1) Try to decide who should get more (or less) 2) Do something else with the leftovers (also called dust). ...
user avatar
1 vote
Accepted

Why do you have to wrap a uint() around a number when dividing it and there is a reminder?

That's a very good question. It touches upon a detail that is probably not very well known, even though it's documented (Rational and Integer Literals): Number literal expressions retain arbitrary ...
user avatar
  • 1,526
1 vote
Accepted

How can I calculate mining rewards from the hashrate?

You could roughly estimate the 24 hr reward as follows: dailyReward = (your_hashrate / api.nethash) * api.block_reward24 * (6*60*24) You need to multiply by 6*60*24 to convert the block reward to the ...
user avatar
1 vote

Using SafeMath with older Solidity compiler

The top comment of the updated safemath library now says: CAUTION This version of SafeMath should only be used with Solidity 0.8 or later, because it relies on the compiler's built in overflow checks....
user avatar
  • 111
1 vote
Accepted

Converting to/from per-second and annualized interest rates with BigNumber.js

Maker Protocol does not use or store an annual rate and there is no representation for it internally. All rates are in per-second which uses the RAY number format which are regular uint256 values and ...
user avatar
  • 26
1 vote

Can't do any integer division

Literal expressions have arbitrary precision until converted (eg. casted) to a non-literal type. In this example, the literal expression 5/4 is an internal rational constant type with unlimited ...
user avatar
  • 195

Only top scored, non community-wiki answers of a minimum length are eligible