Ethereum Vyper Training: Understanding //
Syntax
As a developmenter who uses the vyper’s smart contract, you may face the following error by performing sharing operations:
`
Invalid Action: Cannot Make A Decimalation in Uint256
`
This issue arises because vyper does not support decimal arithmetic. By dividing two uint256 numbers at vyper 0.4.0b or later, it throws an exception to" Disability ".
What is the difference between//and
(convert)?
There are two syntaxes in the venus to make conversions:
- // convert ()
:
- This method returns the converted value.
- The requirement to be transformed) and returns a newuint256.
- If used in conjunction with the Department Operator (/) or modulo operator (
%), it can cause problems associated with the inaccuracy of floating points.
- (Convert (Price, Uint256))*…
:
- This method requires two argument: Value (as a series) and unsign a whole number.
- It tries to transform a series ofuint256, then multiplied by the number specified.
- vyper 0.4.0b or later this syntax is recommended for decimal.
how to solve the problem
To prevent the error when performing a sharing operation in vyper:
- Turn Your Values Using(Convert (Price, Uint256))*…
When dividing twouint256 numbers, use the above syntax:
`V
Div (result, 10); // assuming “the result” is variable by holding one of the numbers
`
- Avoid
// Conversion ()
use decimal
As mentioned above, vyper does not support decimal arithmetic using the method //
. Instead, use
(convert (price, uint256)) * ….
Example of Use: Decimal
To show this approach, let's create a simple example:
V
Mathematics of Contract {
Function Divide (A: U32, B: U8) Publicly Pure Return (U32) {
Return (A / Converting (B, U256)) % 10; // using a converted value for a decimal part
}
}
`
In this fragment of the code:
– Convert (1, 2) is a conversion feature that converts a series of
uint256. The "convert" method can process both "and" U8 ", making it suitable for our exam.
- Divide "(Convert (B, U256))" provides decimal arithmetic by converting any potential fractional fractional part "B".
- then we make a division using the converted value:A / (Convert (B, U256)) % 10.
Conclusion
In this article, we have explored why vyper is throwing an exception to a decimal division. Understanding how to properly modify values and using the recommended syntax(Convert (Price, Uint256)) * … based in smart contracts.