BANKEX Foundation: floating point library for Solidity

in solidity •  7 years ago 

BANKEX Foundation presents an open-source platform for collaborative work, created to promote technologies in banking and fintech sectors. It means that BANKEX Foundation is building an ecosystem that will help ensure transparency, stability and support, needed to implement blockchain technologies for the purpose of commercial integration. In this article we are explaing floating point library for Solidity, a non-profit product created by BANKEX for community.

solidity library.jpeg

— Implementation of floating point number will expand capabilities of Ethereum in terms of more general and convenient calculations (and get rid of artificial decimal multipliers).
— Don’t forget about the convenience — we are used work with numbers containing decimal point.
— And finally — for precision of calculations in financial world special “Decimal” data type is traditionally used, and experience implementing floating point numbers can be transferred to this type also.

It’s known that at the current stage of development, Solidity can only operate on integers (up to 256 bit, signed and unsigned), with fixed point numbers pushed for further development (in a form of ufixed/fixed type), and without any solid plans for floating point implementation. To fix this issue, make programming more convenient and open opportunities to use of wider classes of functions we’ve started a development of binary256 IEEE standard floating point numbers implementation using available functions and types such as uint256, bytes32, additions, multiplications and bit shifts.

We encourage an Ethereum community to contribute to development of this library and hope to see it included either to EVM or as precompiled contract in further versions of Ethereum.

Current branch is located at: https://github.com/BankEx/solidity-float-point-calculation

For the initial public release, we have focused on basic functions necessary for further implementations of standard math library.

This version includes:

  • encoding and decoding of packed 256 bit representation (in a form of bytes32 base type) to intermediate representation of three unsigned 256 bit integers for sign, mantissa and exponent. Intermediate representation is more convenient for basic operations such as addition.
  • Addition, subtraction, multiplication and naive division
  • Fast inverse square root and fast division
  • Log2 function

One of the most difficult design decisions was implementation of division operation. As we all know, division operation is always more expensive and slower than addition or multiplication, and on Ethereum network that would incur additional gas price. We have implemented two division algorithms — one is naive, that obtains one bit of mantissa at the time, requiring more than 200 integer divisions and bit shifts in total. Another algorithm was based on fast inverse square root, with great details about inverse square root can be found in a work of Matthew Robertson.

This algorithm was expanded to 256 representation and implemented. Both fast inverse square root and fast division provide relative precision better than 0.01 that can be tuned further at the price of performance.
From the long list of standard math functions float logarithm of a base two was implemented to demonstrate use of basic operations for composition of more complicated functions.

This library is not ready for production yet, although binary encoding of numbers should not change. Current problems and further work can be found in github repository. Feel free to submit pull requests with new algorithms or other implementations.

Motivation:
1/ Why implement it in solidity: reference implementation in Solidity can be easily transformed to implementations in other languages for precompiled contract creation

2/ Why binary256: 32 bytes is a natural storage slot length and using lesser sizes would not always be beneficial in terms of storage and all operations except float division don’t have a large dependency in gas price on argument structure

3/ Why implement float point type at the first place: we believe that at some point float point operations will be necessary to implement in solidity. For example a “Decimal” data type is necessary for convenience of financial operations and experience gained during creation of float type can be transfered

BANKEX is available at:
Website: https://bankex.com/
Telegram: https://t.me/bankex

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

First prize at Ethereum Hackathon and now contributing to the open source for Ethereum just goes on to show how much potential the team behind Bankex has..Kudos guys!