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
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.