0

not the same solution as this

In a function, I need to calculate the exponential of a fraction. The exponent is a whole number, and the base is not.

I tried using the ABDKMath64x64 library and using this formula: enter image description here

I converted r into a 64.64bit fixed float which is this: (1.8538977)×10^19÷2^64 ≈ 1.0049999569529399767731092651956714689731597900390625

(My r value is 0.005 or 0.5%)

And I got the majority of my code working here

    int128 nominator = 1.8538977 * 1e19;
    nominator = ABDKMath64x64.log_2(nominator);
    int128 newNominator = nominator * rebaseAmount;
    int128 result = ABDKMath64x64.exp_2(newNominator); 
    uint64 uintResult = ABDKMath64x64.toUInt(result);
    return uintResult; 

The problem is the last line. I am certain my int128 result is accurate (when I manually divide it by 2^64, I get my desired number), but when I cast it to a uint, it gives me a single whole number, which is like the current whole number, but rounded down to classic uint style.

Is there a way to cast the result into a uint whilst keeping the following decimals? I tried doing * 1e18 to the above code but the decimals don't keep due to the conversion.

If not, are there better ways at solving 1.005^x, than using this library and formula?

kmao
  • 1
  • 1

1 Answers1

0

Wow, I solved this by just adding 1e18 to the int128 result line, and then doing the function. I thought having a int128 with 36 leading decimals would overflow, but it appears now.

The working code is

        nominator = ABDKMath64x64.log_2(nominator);
        int128 newNominator = nominator * rebaseAmount;
        int128 result = ABDKMath64x64.exp_2(newNominator) * 1e18 ; 
        uint64 resultUint = ABDKMath64x64.toUInt(result);
        return resultUint;

However, I am limited as rebaseAmount overflows for an amount ~500. Are there other implementations possible here?

kmao
  • 1
  • 1