When to (un)wrap (un)signed integers #149
-
Hi, A few versions ago I would declare Now we are required to wrap a normal uint256 into a
It has become a little more difficult to use this implementation, and I want to make sure I ask this before I break something. Thank you in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi @pimhakkert, thank you for your question.
Actually, they weren't safe. The goal with a fixed-point math library is to emulate decimals with simple integers - this means that non-emulated numbers should NOT be used together with emulated numbers, e.g. you should not take the logarithm of a non-emulated (non-fixed-point) uint256. Though there are exceptions, this is an important rule of thumb that can prevent nasty bugs that are otherwise hard to detect.
This is a very broad question that I cannot answer succinctly, I'm afraid. What I can do is recommend you to Solidity's blog post on user defined value types.
User defined value types like
I cannot answer this because it depends on what you want to do with the
I view this as a feature, not a bug. The purpose is to force you, the user, to implement type safety and draw a line in the sand between your fixed-point and non-fixed-point types. It's a really good thing from a security point of view to be aware that some integers are actually fixed-point types, and have 18 decimals, while others are not (e.g. unix timestamps). |
Beta Was this translation helpful? Give feedback.
Hi @pimhakkert, thank you for your question.
Actually, they weren't safe.
The goal with a fixed-point math library is to emulate decimals with simple integers - this means that non-emulated numbers should NOT be used together with emulated numbers, e.g. you should not take the logarithm of a non-emulated (non-fixed-point) uint256. Though there are exceptions, this is an important rule of thumb that can prevent nasty bugs that are otherwise hard to detect.
This is a very broad question that I cannot answer succinctly, I'm afraid. What I can do is recommend you to Solidity's bl…