Digging into Solidity I came across some strange rules.
When looking at a programming language designed by a single person, you find that there is a certain philosophy throughout in consistency. However, Solidity seems like it was programmed by a group that never spoke to one another.
You are allowed to convert a larger type into a smaller one of the same signedness if you use type casts. Okay, you must use type casts. The philosophy is to protect you from yourself unless you know how to typecast. Fine.
// _fondosNuevo is an uint256
uint96 fondosNuevo = uint96(_fondosNuevo);
The above is an unsafe operation. I'm fine with unsafe operations being possible because it allows you to do advanced things in some situations although running on EVM that is rather dubious.
Now the following code is supersafe. There is no possible c can have that d could not accomodate. 0-255 for c, and d runs from around negative 2 billion up to around 2 billion. I didn't expect to see a need for typecasts here.
uint8 c = 4;
int32 d = c; // error
Now I put typecasts in this example. The variable d is a bigger value and can hold values not possible for c and if you use typecasts you can assign d to c. You should probably check d is not bigger than 255 and is non-negative before you do in real code.
uint16 d = 5;
uint8 c = uint8(d); // okay the EVM trusts you here...
int16 e = int16(c); // but not here.
But the e convesion from c which is from 0-255 is not allowed to go into a value which runs from -32000 to 320000 (approximately).
After subtracting values in a smart contract, instead of the values possibly being negative, they could become impossibly large. And comparison to zero will still return positive and that everything is fine, when in reality it is negative. The numbers let into the smart contract, I want them to be unsigned because they don't make sense otherwise. It is likely these contracts could suffer from underflow and thus read a value like google divided by avagadros number, and then you could empty the contract in your name should a flaw like that exist.#programming #ethereum #binancesmartchain