#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <malloc.h>
#include <string.h>

void summ(unsigned a[16], unsigned b[16], unsigned c[16]) //a = b + c
{
    int i;
    int flag = 0;
    unsigned long long res;

    for(i = 0; i < 16; i++)
    {
        res = b[i];
        res += c[i] + flag;
        a[i] = res;
        flag = res >> 32;
    }
}

void neg(unsigned x[16]) // -x
{
    int i;

    for(i = 0; i < 16; i++)
    {
        x[i] = ~x[i];
    }

    for(i = 0; i < 16; i++)
    {
        x[i]++;

        if(x[i] != 0)
        {
            break;
        }
    }
}

void mult(unsigned a[16], unsigned b[16], unsigned c) // a = b * c
{
    int i;
    int flag = 0;
    unsigned long long res;

    for(i = 0; i < 16; i++)
    {
        res = b[i];
        res = res*c + flag;
        a[i] = res;
        flag = res >> 32;
    }
}

void divide(unsigned a[16], unsigned b[16], unsigned c) // a = b / c
{
    int i;
    unsigned long long flag = 0;
    unsigned long long d;
    unsigned long long res;

    for(i = 15; i >= 0; i--)
    {
        res = b[i];
        d = flag;
        res += d << 32;
        a[i] = res/c;
        flag = res % c;
    }
}

void longtodecimal(int a[10], int b[100], unsigned x[16]) // a[10]...a[1]a[0].b[1]b[2]...b[100]...
{
    unsigned x1[16];
    unsigned x2[16];
    unsigned x3[16];
    unsigned x4[16];
    unsigned x5[16];
    unsigned y[16];
    unsigned y1[16];
    unsigned y2[16];
    unsigned y3[16];
    unsigned y4[16];
    unsigned y5[16];
    int i;
    int j;

    memcpy(y, x, 16*sizeof(unsigned));

    for (i = 1; i < 10; i++) //wrong
    {
         memcpy(x1, x, 16*sizeof(unsigned));

        for(j = 0; j < 12; j++)
        {
            x1[j] = 0;
        }

        divide(x2, x1, 10);
        memcpy(x, x2, 16*sizeof(unsigned));

        for(j = 12; j < 16; j++)
        {
            x2[j] = 0;
        }

        mult(x3, x2, 10);
        a[i] = x3[12];
    }

    for(i = 0; i < 100; i++)
    {
        memcpy(y1, y, 16*sizeof(unsigned));

        for(j = 12; j < 16; j++)
        {
            y1[j] = 0;
        }

        mult(y2, y1, 10);
        memcpy(y, y2, 16*sizeof(unsigned));

        for(j = 0; j < 12; j++)
        {
            y2[j] = 0;
        }

        divide(y3, y2, 10);

        for(j = 0; j < 12; j++)
        {
            y3[j] = 0;
        }

        mult(y4, y3, 10);
        neg(y4);
        summ(y5, y2, y4);
        b[i] = y5[12];
    }
}

int main(void)
{
    unsigned x[16];
    unsigned y[16];
    unsigned z[16];
    int a[10];
    int b[100];
    int i;
    int p = 9;

    for(i = 0; i < 16; i++)
    {
        x[i] = 0;
    }

    x[12] = 1;

    mult(y, x, 4);

    divide(z, y, 3);

    longtodecimal(a, b, z);


    while(a[p] == 0)
    {
        p--;
    }

    for(i = p; i >= 0; i--)
    {
        printf("%d", a[i]);
    }

    printf(".");

    for(i = 0; i < 100; i++)
    {
        printf("%d", b[i]);
    }

    return 0;
}