This EIP proposes a natural way for complex numbers to be stored in and retrieved from the bytes32 data-type. It splits the storage space exactly in half and, most importantly, assigns real numbers to the least significant 16 bytes and imaginary numbers to the most significant 16 bytes.
To create a complex number one would use
function cnNew(int128 _Real, int128 _Imag) public pure returns (bytes32){
bytes32 Imag32 = bytes16(uint128(_Imag));
bytes32 Real32 = bytes16(uint128(_Real));
return (Real32>> 128) | Imag32;
}
and to convert back
function RealIm(bytes32 _cn) public pure returns (int128 Real, int128 Imag){
bytes16[2] memory tmp = [bytes16(0), 0];
assembly {
mstore(tmp, _cn)
mstore(add(tmp, 16), _cn)
}
Imag=int128(uint128(tmp[0]));
Real=int128(uint128(tmp[1]));
}
See Add EIP-5850: Complex Numbers stored in Bytes32 types by genkifs · Pull Request #5850 · ethereum/EIPs · GitHub for discussion
The EIP doesn’t discuss the manipulation of complex numbers when they are in Byte32 form because this is undoubtedly a much larger topic.