aboutsummaryrefslogtreecommitdiffstats
path: root/mbf.cpp
blob: 039a0240db3f2f964d982273bd04232b7a7beaf9 (plain) (blame)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// mbf.cpp
// Revision 9-jul-2004

#include "mbf.h"
#include "error.h"

#include <math.h>

// For debugging:
#include <iostream>
#include <iomanip>

double mbf::mbf_s (const std::string & s)
{
	if (s.size () != 4)
		throw ErrFunctionCall;
	unsigned char b0= s [0], b1= s [1], b2= s [2], b3= s [3];
	double n= (b2 & 0x7F) | 0x80;
	n= 256 * n + b1;
	n= 256 * n + b0;
	short e= static_cast <short> (b3 - 128 - 24);
	if (e > 0)
		for ( ; e > 0; --e)
			n*= 2;
	else
		for ( ; e < 0; ++e)
			n/= 2;
	if (b2 & 0x80)
		n= -n;
	return n;
}

double mbf::mbf_d (const std::string & s)
{
	if (s.size () != 8)
		throw ErrFunctionCall;
	unsigned char b0= s [0], b1= s [1], b2= s [2], b3= s [3],
		b4= s [4], b5= s [5], b6= s [6], b7= s [7];
	double n= (b6 & 0x7F) | 0x80;
	n= 256 * n + b5;
	n= 256 * n + b4;
	n= 256 * n + b3;
	n= 256 * n + b2;
	n= 256 * n + b1;
	n= 256 * n + b0;
	short e= static_cast <short> (b7 - 128 - 56);
	if (e > 0)
		for ( ; e > 0; --e)
			n*= 2;
	else
		for ( ; e < 0; ++e)
			n/= 2;
	if (b6 & 0x80)
		n= -n;
	return n;
}

namespace {

double zero= 0.0;

} // namespace

std::string mbf::to_mbf_s (double v)
{
	bool negative= v < 0;
	if (negative)
		v= -v;
	int e= static_cast <int> (log (v) / log (2) );
	v/= pow (2, e - 23);
	unsigned long l= static_cast <unsigned long> (v);

	#if 0
	std::cerr << "e= " << std::dec << e <<
		" l= " << std::hex <<
		std::setw (8) << std::setfill ('0') <<
		l << std::endl;
	#endif

	unsigned char b3= static_cast <unsigned char> (e + 128 + 1);
	unsigned char b2= static_cast <unsigned char> (l / (256 * 256) );
	unsigned char b1= static_cast <unsigned char> ((l / 256) % 256);
	unsigned char b0= static_cast <unsigned char> (l % 256);
	b2&= 0x7F;
	if (negative)
		b2|= 0x80;
	#if 0
	std::cerr << std::setw (2) << int (b0) << int (b1) <<
		int (b2) << int (b3) << std::endl;
	#endif

	return std::string (1, char (b0) ) + char (b1) + char (b2) + char (b3);
}

std::string mbf::to_mbf_d (double v)
{
	bool negative= v < 0;
	if (negative)
		v= -v;
	int e= static_cast <int> (log (v) / log (2) );
	v/= pow (2, e - 55);
	//unsigned long l= static_cast <unsigned long> (v);
	double l;
	modf (v, & l);

	#if 0
	std::cerr << "e= " << std::dec << e <<
		" l= " << std::hex <<
		std::setw (8) << std::setfill ('0') <<
		l << std::endl;
	#endif

	unsigned char b7= static_cast <unsigned char> (e + 128 + 1);

	#if 0
	unsigned char b6= l / (256.0 * 256 * 256 * 256 * 256 * 256);
	unsigned char b5= (l / (256.0 * 256 * 256 * 256 * 256) ) % 256.0;
	unsigned char b4= (l / (256.0 * 256 * 256 * 256) ) % 256.0;
	unsigned char b3= (l / (256UL * 256 * 256) ) % 256.0;
	unsigned char b2= (l / (256 * 256) ) % 256.0;
	unsigned char b1= (l / 256) % 256.0;
	unsigned char b0= l % 256.0;
	#else
	double ll;
	modf (l / 256, & ll);
	unsigned char b0= static_cast <unsigned char> (l - 256 * ll);
	l= ll;
	modf (l / 256, & ll);
	unsigned char b1= static_cast <unsigned char> (l - 256 * ll);
	l= ll;
	modf (l / 256, & ll);
	unsigned char b2= static_cast <unsigned char> (l - 256 * ll);
	l= ll;
	modf (l / 256, & ll);
	unsigned char b3= static_cast <unsigned char> (l - 256 * ll);
	l= ll;
	modf (l / 256, & ll);
	unsigned char b4= static_cast <unsigned char> (l - 256 * ll);
	l= ll;
	modf (l / 256, & ll);
	unsigned char b5= static_cast <unsigned char> (l - 256 * ll);
	l= ll;
	unsigned char b6= static_cast <unsigned char> (l);
	#endif
	b6&= 0x7F;
	if (negative)
		b6|= 0x80;

	#if 0
	std::cerr << std::setw (2) << int (b0) << int (b1) <<
		int (b2) << int (b3) << std::endl;
	#endif

	return std::string (1, char (b0) ) + char (b1) + char (b2) +
		char (b3) + char (b4) + char (b5) + char (b6) + char (b7);
}

// End of mbf.cpp
Un proyecto texto-plano.xyz