-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcasthalf.cpp
42 lines (38 loc) · 1.1 KB
/
casthalf.cpp
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
#include "half/include/half.hpp"
#include <stdio.h>
#include <string.h>
using half_float::half;
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("half cast between internal 16bits interger value and single float value\n");
printf("usage: %s value\n", argv[0]);
printf(" if the input value contains '.', it is considered as single float value\n");
printf(" if the input value does not contain '.', it is considered as internal 16bits value\n");
printf("For example:\n");
printf(" %s 100. #single float 100.0\n", argv[0]);
printf(" %s 100 #internl 16bits value\n", argv[0]);
return 0;
}
if (strchr(argv[1], '.') != NULL)
{
float f = atof(argv[1]);
printf("input float value: %f\n",f);
half h(f);
printf("internal 16bits value: %d (0x%x)\naccurate float value: %f\n", h.data_, h.data_, float(h));
}
else
{
int base = 10;
if (argv[1][1] == 'x' || argv[1][1] == 'X')
{
base = 16;
}
unsigned short s = strtol(argv[1], NULL, base);
printf("input internal 16bits value: %d (0x%x)\n", s, s);
half h;
h.data_ = s;
printf("single float value: %f\n",float(h));
}
}