File: double.c.c

package info (click to toggle)
foobillardplus 3.43~svn170%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 86,620 kB
  • sloc: ansic: 125,581; makefile: 268; xml: 32; sh: 1
file content (69 lines) | stat: -rw-r--r-- 1,276 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * swap.c
 *
 *  Created on: 17.07.2011
 *      Author: holger
 */

#include "stdio.h"
#include "math.h"

// swap using char pointers
unsigned long long  swap(double d)
{
    unsigned long long a;
    unsigned char *dst = (unsigned char *)&a;
    unsigned char *src = (unsigned char *)&d;

    dst[0] = src[7];
    dst[1] = src[6];
    dst[2] = src[5];
    dst[3] = src[4];
    dst[4] = src[3];
    dst[5] = src[2];
    dst[6] = src[1];
    dst[7] = src[0];

    return a;
}

// unswap using char pointers
double unswap(unsigned long long a)
{

    double d;
    unsigned char *src = (unsigned char *)&a;
    unsigned char *dst = (unsigned char *)&d;

    dst[0] = src[7];
    dst[1] = src[6];
    dst[2] = src[5];
    dst[3] = src[4];
    dst[4] = src[3];
    dst[5] = src[2];
    dst[6] = src[1];
    dst[7] = src[0];

    return d;
}

int main(int argc,char *argv[])
{
    double a;
    unsigned long long b;
    double c;
    for(a=0.0;a<100.0;a+=0.01) {
        // swap to network byte order
        b = swap(a);
        // swap back
        c = unswap(b);
        // now a and C should be EXACTLY the same. but if not, print something
        if (a == c) {
            printf("*****\n%f\n%f\n%f\n%f\n",
            a,b,c,fabs(a-c));
        }


    }
    return 0;
}