-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat-ieee.c
65 lines (61 loc) · 1.31 KB
/
float-ieee.c
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
#include <stdio.h>
#include <stdint.h>
/* IEEE754 floats are represented in mem like so:
*
* sign | exponent | mantissa
* float: 1 | 8 (127 bias) | 23
* double: 1 | 11 (1023 bias) | 52
*
* Floats are converted to from decimal following these steps:
*
* 420.69 -> 110100100.101100001010010... -> 1.10100100101100001010010 * 2^8
* sign: 0
* expo: 8 + 127
* mant: 10100100101100001010010
* repr: 0 10000111 10100100101100001010010
* hexa: 0x43d25852
*
* now we try it out with our program */
# ifdef DOUBLEPREC
# define FLIT(x) x
# define FMT "l"
# define BIAS 1023
# define MANT 52
# define EXPO 11
# define F double
# define U uint64_t
# define I int64_t
# else
# define FLIT(x) x##f
# define FMT
# define BIAS 127
# define MANT 23
# define EXPO 8
# define F float
# define U uint32_t
# define I int32_t
# endif
typedef union floatrep ieee;
union floatrep {
F f;
struct {
U mant : MANT;
U expo : EXPO;
U sign : 1;
};
U i;
};
int
main ()
{
ieee num = {.f = FLIT(420.69)};
printf (
"number: %.2f\n"
"floatrep: %#"FMT"x\n"
"neg-sign: %s\n"
"exponent: %"FMT"u, without bias: %"FMT"d (both in decimal)\n"
"mantissa: %#"FMT"x\n"
, num.f, num.i, num.sign? "true" : "false"
, num.expo, (I)num.expo - BIAS, num.mant );
return 0;
}