-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdouble_generator.c
More file actions
67 lines (61 loc) · 2.11 KB
/
double_generator.c
File metadata and controls
67 lines (61 loc) · 2.11 KB
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
/* Typhoon 20190109 */
/* Generate Double-precision Floating Point Number */
/* We aussume sign bit as 0 and travers exponent bits from 0 to 2047.
All 0 and all 1 is meaningless so the acutual range is 1~2046 .
For fraction bits ,we travers first n bits("n" is defined by FRA_TRAVERSED_COUNT_DOUBLE),
and the rest bits can be either 1 or 0 */
/* Updated by Typhoon 20200412 */
/* We aussume sign bit can be either 1 or 0 */
/* Set the first bit of fraction as 1 to ensure the value ranging from 0.5 to 1 as
normalized representation required by IEEE-754 */
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define FRA_TRAVERSED_COUNT_DOUBLE 12
#define FRA_TRAVERSED_POW_DOUBLE pow(2,FRA_TRAVERSED_COUNT_DOUBLE)
int main(int argc,char *argv[])
{
int sign; //we aussume it positive as it makes no difference
int exponent; //(-1023~1024)according to the bais,the actual range is 0~2047 without all 0 and all 1
unsigned long fraction; //travers first n bits,and the rest bits can be either 1 or 0
long restMaxVal=pow(2,52- FRA_TRAVERSED_COUNT_DOUBLE)-1;//FRA_TRAVERSED_COUNT_DOUBLE=12
long restFraction=0;
long firstFraction=0;
int sum=0;
FILE *stream;
stream=fopen("double_samples.txt","w");
/* generate float numbers */
for (sign=0;sign<=1;sign++)
{
for(exponent=1;exponent<=2046;exponent++)
{
/* produce fisrt fraction bits */
long i=0;
long j=0;
long ii=0;
for(i=0;i<FRA_TRAVERSED_POW_DOUBLE;i++)//FRA_TRAVERSED_POW_DOUBLE=pow(2,FRA_TRAVERSED_COUNT)=pow(2,12)
{
ii=i;
for(j=0;j<FRA_TRAVERSED_COUNT_DOUBLE-1;j++)
{
sum=ii & 1;
if(sum==1)
{
firstFraction+=pow(2,52-(FRA_TRAVERSED_COUNT_DOUBLE-j));
}
ii=ii>>1;
}
firstFraction+=pow(2,51); //the fisrt bit must be 1 as mentioned above
/* generate the rest fraction bits */
restFraction=rand()%(restMaxVal+1);
/* combine */
fraction=firstFraction+restFraction;
/* reset */
firstFraction=0;
/* write to file */
fprintf(stream,"%d\t%d\t%ld\n",sign,exponent,fraction);
}
}
}
return 0;
}