samedi 25 avril 2015

Trouble with Bailey-Borwein-Plouffe formula


I am trying to implement the BBP formula. I wrote the following quick and dirty test code, based on the paper by Paul H. Bailey (that I found here):

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gmp.h>
#include <limits.h>

long bin_exp_mod_k(int base, int exponent, int div) {
    long expmod = 1;
    long t;

    t = (long) pow(2, floor(log(exponent)/log(2.0)));

    while(42) {
        if(exponent >= t) {
            expmod = (base * expmod) % div;
            exponent -= t;
        }

        t = t/2;

        if(t >= 1) {
            expmod = ((long)pow(expmod, 2)) % div;
        } else
            break;
    }

    return expmod;
}

void bbp_part(mpf_t rop, int base, int index, int digit_index, int num_iter) {
    const int knum = num_iter;
    int k;
    long ldiv;
    mpf_t fbase;
    mpf_t fst_sum;
    mpf_t fst_sum_int;
    mpf_t snd_sum;
    mpf_t powval;
    mpf_t div;

    mpf_init(fst_sum);
    mpf_init(snd_sum);
    mpf_init(fst_sum_int);
    mpf_init(powval);
    mpf_init(div);
    mpf_init(rop);
    mpf_init_set_si(fbase, base);

    for(k = 0;k <= digit_index;k++) {
        ldiv = 8 * k + index;
        mpf_set_si(powval, bin_exp_mod_k(base, digit_index - k, ldiv));
        mpf_set_si(div, ldiv);
        mpf_div(powval, powval, div);
        mpf_add(fst_sum, fst_sum, powval);
    }

    mpf_trunc(fst_sum_int, fst_sum);
    mpf_sub(fst_sum, fst_sum, fst_sum_int);

    for(k = digit_index + 1;k < knum;k++) {
        ldiv = 8 * k + index;
        mpf_set_si(div, ldiv);
        mpf_pow_ui(powval, fbase, digit_index - k);
        mpf_div(powval, powval, div);
        mpf_add(snd_sum, snd_sum, powval);
    }

    mpf_set(rop, fst_sum);
    mpf_add(fst_sum, fst_sum, snd_sum);

    printf("S%i: %.20lf\n", index, mpf_get_d(rop));

    mpf_clear(fst_sum);
    mpf_clear(snd_sum);
    mpf_clear(fbase);
    mpf_clear(fst_sum_int);
    mpf_clear(powval);
    mpf_clear(div);
}

int main(int argc, char **argv) {
    const int base = 16;
    int d;
    int num_iter;
    mpf_t pi_digits;
    mpf_t part1;
    mpf_t part1_int;
    mpf_t part4;
    mpf_t part4_int;
    mpf_t part5;
    mpf_t part5_int;
    mpf_t part6;
    mpf_t part6_int;

    if(argc == 1) {
        return -1;
    }

    d = atoi(argv[1]);

    if(argc == 3) {
        num_iter = atoi(argv[2]);
    } else {
        num_iter = INT_MAX;
    }

    mpf_set_default_prec(128);

    mpf_init(pi_digits);

    mpf_init(part1_int);       
    mpf_init(part4_int);
    mpf_init(part5_int);
    mpf_init(part6_int);

    bbp_part(part1, base, 1, d, num_iter);
    bbp_part(part4, base, 4, d, num_iter);
    bbp_part(part5, base, 5, d, num_iter);
    bbp_part(part6, base, 6, d, num_iter);

    mpf_trunc(part1_int, part1);
    mpf_trunc(part4_int, part4);
    mpf_trunc(part5_int, part5);
    mpf_trunc(part6_int, part6);

    mpf_sub(part1, part1, part1_int);
    mpf_sub(part4, part4, part4_int);
    mpf_sub(part5, part5, part5_int);
    mpf_sub(part6, part6, part6_int);

    mpf_mul_ui(part1, part1, 4L);
    mpf_mul_ui(part4, part4, 2L);

    mpf_set(pi_digits, part1);
    mpf_sub(pi_digits, pi_digits, part4);
    mpf_sub(pi_digits, pi_digits, part5);
    mpf_sub(pi_digits, pi_digits, part6);

    mpf_clear(pi_digits);
    mpf_clear(part1);
    mpf_clear(part4);
    mpf_clear(part5);
    mpf_clear(part6);
    mpf_clear(part1_int);
    mpf_clear(part4_int);
    mpf_clear(part5_int);
    mpf_clear(part6_int);

    return 0;
}

and it seems to be correct, because according to his paper the partial results I get for S1, S4, S5 and S6 are, with a small precision difference, the same ones noted.

However, I can't figure out what I did miss with the "combining the results" part. No matter what I do, I get 0.57... instead of the 0.42... written on the paper.

Can someone see what am I missing here?


incrementing values in if loop (in C)


I am writing a self-controlled Pacman game (Pacman moves by itself by algorithms which decide it's movement, needs to chase ghosts, etc.) in C. Keep in mind that I am very new to programming in C so the method I use may not be a good one at all.

Anyways, I stumbled upon a problem.

my Pacman moves in an Array map with it's placement at

Map[yPacman][xPacman] == place of Pacman;

Now, when Pacman is in "rage mode"; after picking up a sweet, I would like him to find the ghosts he seeks. The ghost being a char 'G' in the array.

Now, I would like Pacman to see ahead of him to catch and follow the ghost when it reaches a distance near him. Let's say he's moving to the left; I would him to look 5 "X-Coords" left of him, including the X-coords "1,2,3,4,5". When any of these coordinates equal to a char 'G', it'd have to follow him.

Now, I would like to implement it, but I can't seem to find a way how to.

if (Map[yPacman][xPacman-i] == 'G')  { }

With "i" having a value from 1-5. Incrementing it during the process itself. I would not like to use

if (Map[yPacman][xPacman-1] == 'G' || Map[yPacman][xPacman-2] == 'G'|| Map[yPacman][xPacman-3] == 'G'|| Map[yPacman][xPacman-4] == 'G'|| Map[yPacman][xPacman-5] == 'G')  { }

Even though this is the exact result I would need.

I personally think it seems very easy to solve how to do this. But at the moment I have no clue. Thanks in advance


Pointer for pointer


I try to insert new node to tree without using recursive function.

Root is global defined first node of tree.

node *tree;
tree=&root;

.......

(tree->tab[first])=(node*)malloc(sizeof(node));

Where node is my own defined structure. I want to have handle to root, so I make new pointer, which I will use to move on tree. I m very suprised because new allocated nodes arent assigned to my root. How can I do that? How can edit root by editing tree.

Please help, I have spent hours for that problem. Greetings


matrix represents tree the function returns 0


I have some homwrok and it looks like this: Question 1 ( First program adjacency.c file ) Directed tree structure T has N nodes represented by the adjacency matrix A size NxN as follows:

A [ u ] [ v] == TRUE if and only if there is a directed arc from u to v in T , or in other words : u is the parent of v . In the following example a tree with N = 11 nodes :

tree

We obtain the following neighboring matrix :

matrix

this are the questions:

A. You must define with #define command and / or enum the N and permanent TRUE and FALSE. Typedef should be set with a character named adj_mat defines the neighboring matrix size N.

B. You must write a function called path, which accepts as a parameter adjacency matrix A and indexes of two nodes u and v and returns TRUE if and only if there is a directed path (by directional arrow) at the intersection u v, the tree is represented by a matrix A. Otherwise it returns FALSE.

For example: path (1,8) will return TRUE. The same path (1,3). On the other hand path (3,8) will FALSE.

C. First you must write a function (main) defines a variable type adj_mat, asks the user entries for this matrix, and indexes of the two nodes. The main function function call path, to see if there is a directed path between two nodes in the data. The function to print the test result output.

have to get some help guys

#include <stdio.h>

#define N 11
enum {FALSE, TRUE};
typedef int adj_mat[N][N];

int path2(adj_mat A, int u, int v, int temp)
{
if(u == temp && A[u][v] == FALSE)
return TRUE;

if(u == temp && A[u][v] == FALSE)
return FALSE;

if(A[u][v] == FALSE)
return path2(A, u-1, v, temp);

if(A[u][v] == TRUE)
return path2(A, N, u, temp);

return FALSE;
}

int path(adj_mat A, int u, int v)
{
return path2(A, N, v, u);
}



int main()
{

int arr[N][N]= {{0,1,1,1,0,0,0,0,0,0,0},{0,0,0,0,1,1,1,1,1,0,0},
{0,0,0,0,0,0,0,0,0,1,0},{0,0,0,0,0,0,0,0,0,0,1},{0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0}};
int u;
int v;
printf("please enter two numbers \n");
scanf("%d %d", &u, &v);
printf("The answer is %d", path(arr, u, v),".");
return 0;
}

The problem is at the terminal when i put 1,8 it returns 0. please help me guys im going inasane


error in sending char array using c socket programming


Below is a code for transferring char array from client to server but server is not able to receive array and display the content of the received array also there is a warning as follows:

assignment makes integer from pointer without a cast [enabled by default]
     temp_char_arr[i]=recvs_bytes[i];

please help me solve this problem

thanks in advance.

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<signal.h>

#define PORT 2021

int sd,ad,pid,len = 0,i;
float p;
char buf[100];
char* recvs_bytes[11],temp_char_arr[11];


void save_bytes(const char *filename,char* temp_char_arr[]);

void Die(char *mess)
{
    perror(mess); exit(1); 
}
int main()
{

    printf("\nEnter server port:2020 and server          ip:192.168.204.138\n");        
    struct sockaddr_in serverAddress;
    if((sd = socket(AF_INET,SOCK_STREAM,0)) < 0)
    {
        Die("Error creating socket");
    }
    memset(&serverAddress, 0, sizeof(serverAddress));
    serverAddress.sin_family = AF_INET;
    serverAddress.sin_port = htons(PORT);
    serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);

    if(bind(sd, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) < 0)
    {
        Die("Bind error");
    }

    if(listen(sd, 5) < 0)
    {
        Die("Listen error");
    }
    printf("\n***************************serveronline*****************************\n");
    while(1)
    {
        ad = accept(sd, (struct sockaddr*)&serverAddress, &len);
        pid=fork();
        if(pid<0) 
            Die("Fork error");

        if(pid==0)
        {
            p=1.0;
            if(send(ad,&p,sizeof(float),0)< 0)  Die("Error sending data");
            printf("\nreceivieng bytes\n");
            if(recv(ad, recvs_bytes, sizeof(recvs_bytes), 0) <0) Die("Receive error");
            for(i=0;i<=10;i++)
            {
                temp_char_arr[i]=recvs_bytes[i];
                printf("%s",&temp_char_arr[i]);
            }
            printf("\nbytes received\n");
            close(sd);
            exit(0);
        }
        if(pid>0)
        {
            close(ad);
        }
    }

    return 0;
}

client.c

#include <stdio.h>
#include <sys/statfs.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <signal.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include<signal.h>


char *trans_bytes[11],arr[11];
FILE *ptr_file;
char* temp[11],arr[11];
int sd,i;
float q;

void func();
void send_trans_bytes(char* arr[]);

void Die(char *mess) { perror(mess); exit(1); }

int main(int argc, char *argv[])
{
    if (argc != 3) {
        printf("USAGE: TCPecho <server_ip> <port>\n");
        exit(1);
    }

    struct sockaddr_in clientAddress;
    if((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        Die("Error creating socketsss");
    }

    memset((char *)&clientAddress, 0, sizeof(clientAddress));

    clientAddress.sin_family = AF_INET;
    clientAddress.sin_addr.s_addr= inet_addr(argv[1]);
    clientAddress.sin_port = htons(atoi(argv[2]));
    if ((connect(sd,(struct sockaddr *)&clientAddress, sizeof(clientAddress))) < 0)
    {
        Die("Error connectiong to server!");
    }
    printf("\n--------------------client online----------------------------\n");

    if(recv(sd, &q, sizeof(float), 0) < 0)  {    Die("Receive error");   }
    if (q==1.0)
        printf("\nsignal receivied=%f\n",q);
    func();
    return 0;
}


void func()
{
    int i;
    for(i=0;i<=10;i++)
        temp[i]="some_text";
    send_trans_bytes(temp);
}


void send_trans_bytes(char* arr[])
{
    if(send(sd,arr, sizeof(arr), 0) < 0) Die("Error sending host name");
    for(i=0;i<=10;i++)
        printf("arr[%d]=%s",i,arr[i]);
}


C: Reading multiple areas into an array and finding the largest value


I'm attempting to find the largest value in an array. I'm given a set of areas (the input), which for now is:

4.5
19.59
1.92

These areas need to be read into an array using scanf and then from there I need to find the largest value (area) in that array. It should also be known that there can only be maximum of 100 areas that can be read into the array. So later on I may need to read in 5 or 20 or 99 etc... into the array.

If anybody could give me a hand it'd be much appreciated, I'm new to programming and don't really know where to begin.


Where is the implementation of dm_task_create in cryptsetup?


Where is the implementation of the function dm_task_create in cryptsetup (and other dm_task_ related functions)? Grepping for this function in the source for cryptsetup I come up with nothing. I see it is used in lib/libdevmapper.c and that it has a function prototype in libdevmapper.h. However where is the implementation? As a side note, cryptsetup compiles fine and executes.

Just to check, I grepped through the kernel source as well but it doesn't appear to be implemented in the kernel either.

From the following link http://ift.tt/1IWARep it appears that at least in the past it was implemented in libdevmapper.c.