NEWTON

NEWTON

# Can someone explain the difference between unsigned_div_rem and uint256_unsigned_div_rem?

2 months ago

195

views

1

This question has a bounty of \$0.00 in testnet ETH. Answers to this question are eligible to win that bounty.

These are testnet contracts. Not real ETH.

\$0.00

Can someone explain the difference `between unsigned_div_rem` and `uint256_unsigned_div_rem`?

Why unit version process so many extra steps, but overall number of steps for computing are different for these methods?

cairomath

vargastartup

2 months ago

0

Both function compute the quotient and remainder(or modulo) for unsigned integers.

`unsigned_div_rem` takes `felt` arguments

`uint256_unsigned_div_rem` takes `Uint256` arguments.

Recall that Uint256 integers are encoded as two felts representing the 256 bits as two 128 bits numbers `low` and `high`.

``````struct Uint256 {
low: felt   \\ 128 low bits
high: felt  \\ 128 high bits
}
``````

Thus, `uint256_unsigned_div_rem` needs to do more thorough checks than the felt version to make sure the encoding is safe and sound. Namely, it calls `uint256_check` on return values and needs to verify the result (gotten via a hint). Those checks can be seen here in the source, while the felt counterpart is more straightforward.

In general, functions using the Uint256 struct will be less efficient than their felt counterpart.

Additional resources can be found here and here.

0xDE8D

2 months ago

Answer is not submitted on chain

0

In Solidity, the uint256_unsigned_div_rem function is an implementation of an unsigned integer division and remainder operation for 256-bit integers. This function takes two arguments, a and b, and returns a tuple containing the quotient and remainder of the division a / b.

The unsigned_div_rem function is similar, but it is not specific to 256-bit integers and can be used with any unsigned integer type. It also takes two arguments, a and b, and returns a tuple containing the quotient and remainder of the division a / b.

Here is an example of how these functions can be used:

Copy code pragma solidity ^0.7.0;

contract Example { function example() public pure { uint256 a = 100; uint256 b = 3; uint256 c = 7;

``````    // Use uint256_unsigned_div_rem to perform division and remainder with 256-bit integers
(uint256 q, uint256 r) = uint256_unsigned_div_rem(a, b);
assert(q == 33);
assert(r == 1);

// Use unsigned_div_rem to perform division and remainder with any unsigned integer type
(uint q2, uint r2) = unsigned_div_rem(c, b);
assert(q2 == 2);
assert(r2 == 1);
}
``````

} In this example, the uint256_unsigned_div_rem function is used to perform division and remainder with 256-bit integers, while the unsigned_div_rem function is used to perform the same operation with smaller unsigned integers.

Answer is not submitted on chain

0

In Solidity, the uint256_unsigned_div_rem function is an implementation of an unsigned integer division and remainder operation for 256-bit integers. This function takes two arguments, a and b, and returns a tuple containing the quotient and remainder of the division a / b.

The unsigned_div_rem function is similar, but it is not specific to 256-bit integers and can be used with any unsigned integer type. It also takes two arguments, a and b, and returns a tuple containing the quotient and remainder of the division a / b.

Here is an example of how these functions can be used:

Copy code pragma solidity ^0.7.0;

contract Example { function example() public pure { uint256 a = 100; uint256 b = 3; uint256 c = 7;

``````    // Use uint256_unsigned_div_rem to perform division and remainder with 256-bit integers
(uint256 q, uint256 r) = uint256_unsigned_div_rem(a, b);
assert(q == 33);
assert(r == 1);

// Use unsigned_div_rem to perform division and remainder with any unsigned integer type
(uint q2, uint r2) = unsigned_div_rem(c, b);
assert(q2 == 2);
assert(r2 == 1);
}
``````

} In this example, the uint256_unsigned_div_rem function is used to perform division and remainder with 256-bit integers, while the unsigned_div_rem function is used to perform the same operation with smaller unsigned integers.

0x2932...Bc3E69

29 days ago

Answer is not submitted on chain

0

We use Cairo’s common math library, specifically unsigned_div_rem (unsigned division with remainder) to calculate the amount of tokens to be received.

// Calculate swap amount. let (local amount_to, _) = unsigned_div_rem( amm_to_balance * amount_from, amm_from_balance + amount_from, ); uint256_unsigned_div_rem takes Uint256 arguments.

Recall that Uint256 integers are encoded as two felts representing the 256 bits as two 128 bits numbers low and high.

Thus, uint256_unsigned_div_rem needs to do more thorough checks than the felt version to make sure the encoding is safe and sound. Namely, it calls uint256_check on return values and needs to verify the result (gotten via a hint). Those checks can be seen here in the source, while the felt counterpart is more straightforward.

In general, functions using the Uint256 struct will be less efficient than their felt counterpart.

Additional resources can be found here and here.

0xeF57...46cee1

2 months ago

Answer is not submitted on chain