# Using Fractions in Leasing contract

I want to create a smart contract that allows people to lend dai stable coin to other or a smart contract at an interest. suppose at 4 % interest I need to calculate (1+0.04/12)^n (n is the period in months). But I am having problem calculating fractions the factor never goes above 1. it is supposed to be 1.04 for n=12 and 1.081 at n=24 but all I get is one I cannot multiply the fraction as the number increases exponentially with power (1^24 is 1 but 10^24 has 24 zeros) so what should I do to invoke fractions into a contract Is there a way I can calculate externally and send the result to the contract

• `Is there a way I can calculate externally and send the result to the contract?` Of course, but that's mixing several different questions into one, so please post it separately. – goodvibration Apr 7 '20 at 11:27
• See my advanced fixed-point math library PRBMath. – Paul Razvan Berg Apr 20 at 14:55

Floating-point arithmetic is not supported in Solidity.

In other words, `x / y` yields the floor of dividing `x` by `y`.

As a bonus fact, `x ^ y` yields the bit-wise XOR of `x` and `y`.

In order to claculate `x` to the power of `y`, you need to use `x ** y`.

But again, this only works for integers.

• You can find a `power` function implementation here. – goodvibration Apr 7 '20 at 11:33

Well you can either calculate it externally or calculate it inside the contract. If you calculate externally you lose some of the benefits of decentralization (you'll lose the trustless nature as everyone has to trust you to input the right value) but you'll save in gas costs.

If you want to calculate it in the contract you will have problems with the floating point exponent. So I suggest you have a look at this article: https://medium.com/coinmonks/math-in-solidity-part-5-exponent-and-logarithm-9aef8515136e . It gets rather..complicated and requires some mathematical understanding but it explains how you can support floating exponents (among other things).

While fractions are not supported natively by Solidity nor EVM, they could be emulated at reasonable cost.

First thing you need to decide is what kind of fractions to use. Most common types are: simple, fixed-point, or floating point. Fixed- and floating- point fractions could be decimal (base 10), binary (base 2), or use come other base.

Then you will need to decide what method to use for raising fraction into integer power. The two most common methods are: exponentiation by squaring and combining fixed-base exponent and logarithm.

You may, of course, implement the everything yourself, as it is not too complicated and would be a good coding exercise, but for your particular case i would recommend using `pow` function from ABDKMath64x64 library. It implements exponentiation by squaring algorithm on signed binary 64.64-bit fixed-point fractions. Here 64.64-bit signed binary fixed point fraction is a number of the form `x/2^64`, where x is signed 128-bit integer. Such fractions allow representing numbers from about -2^63 to +2^63 with precision of 2^-64. This should be enough for your case, and will work well even for interest charging periods much shorter than 1 month, such as 1 week, 1 day, or even 1 second.