File: floattest.c

package info (click to toggle)
bock 0.20.2.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,228 kB
  • ctags: 1,370
  • sloc: ansic: 7,367; java: 5,553; yacc: 963; lex: 392; makefile: 243; sh: 90; perl: 42
file content (71 lines) | stat: -rw-r--r-- 1,585 bytes parent folder | download | duplicates (2)
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
70
71
#include <stdio.h>
#include <math.h>

double
llong2double(unsigned long long int l)
{
	union { double d; unsigned long long int l; } u;
	/* l=(l >> 32) | (l << 32); */
	u.l=l;
	return u.d;
}

float
int2float(unsigned long int l)
{
	union { float f; unsigned long int l; } u;
	u.l = l;
	return u.f;
}

void
printfloat(float f)
{
	double d = f;
	union { float f; unsigned int i; } u;

	u.f = f;
	if (isnan(d)) {
		puts("NaN (float from int)");
	} else {
		printf("%f (float) = 0x%08X (int)\n", d, u.i);
	}
}

void
printdouble(double d)
{
	union { double d; unsigned long long int l; } u;

	u.d = d;
	if (isnan(d)) {
		puts("NaN (double from long long int)");
	} else {
		printf("%f (double) = 0x%08X%08X (long long int)\n", d,
		       (unsigned int) (u.l / 0x100000000ULL),
		       (unsigned int) (u.l % 0x100000000ULL));
	}
}

int
main(int ac, char *av[])
{
	printfloat(int2float(0x7f800000UL));
	printfloat(int2float(0xff800000UL));
	printfloat(int2float(0x7f800001UL));
	printfloat(int2float(0x7fffffffUL));
	printfloat(int2float(0xff800001UL));
	printfloat(int2float(0xffffffffUL));
	printfloat(int2float(0x00000000UL));
	printfloat(int2float(0x80000000UL));
	printdouble(llong2double(0x7ff0000000000000ULL));
	printdouble(llong2double(0xfff0000000000000ULL));
	printdouble(llong2double(0x7ff0000000000001ULL));
	printdouble(llong2double(0x7fffffffffffffffULL));
	printdouble(llong2double(0xfff0000000000001ULL));
	printdouble(llong2double(0xffffffffffffffffULL));
	printdouble(llong2double(0x0000000000000000ULL));
	printdouble(llong2double(0x8000000000000000ULL));

	return 0;
}