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
using Microsoft.SPOT;
namespace UAM.InformatiX.SPOT
{
public static partial class IEEE
{
/// <summary>
/// Provides access to the individual components of the single-precision floating point number.
/// </summary>
[System.Serializable]
public struct Single
{
/// <summary>
/// True if the number is negative; otherwise false.
/// </summary>
[SerializationHints(BitPacked = 1)]
public bool Sign;
/// <summary>
/// The biased exponent of the number as it is stored in the binary format.
/// </summary>
[SerializationHints(BitPacked = 8)]
public byte BiasedExponent;
//[SerializationHints(BitPacked = 8, RangeBias = -127)] // Unfortunately that biasing does not work for denormalized numbers, like -118.625,
//public sbyte Exponent; // otherwise, the BiasedExponent field would not be needed at all.
/// <summary>
/// The mantissa of the number. Using more than 23-bits will result in exception when casting to float.
/// </summary>
[SerializationHints(BitPacked = 23)]
public uint Mantissa;
/// <summary>
/// Gets or sets the exponent value of the number.
/// </summary>
public int Exponent
{
get {
return BiasedExponent - 127; }
set { BiasedExponent = (
byte)(
value + 127); }
}
/// <summary>
/// Creates a new instance of the single-precision floating point number with specified components.
/// </summary>
/// <param name="sign">True to represent negative number; otherwise false.</param>
/// <param name="exponent">The exponent value.</param>
/// <param name="mantissa">The 23-bit value representing the mantissa.</param>
public Single(
bool sign,
int exponent,
uint mantissa)
{
Sign = sign;
//Exponent = exponent;
BiasedExponent = (
byte)(exponent + 127);
Mantissa = mantissa;
}
/// <summary>
/// Converts a <see cref="IEEE.Single"/> to a single-precision floating-point number.
/// </summary>
/// <param name="f"></param>
/// <returns>A single-precision floating-point number that represents the <see cref="IEEE.Single"/>.</returns>
public static explicit operator
float(Single f)
{
return (
float)Reflection.Deserialize(Reflection.Serialize(f,
typeof(Single)),
typeof(
float));
}
/// <summary>
/// Converts a single-precision floating-point number to a <see cref="IEEE.Single"/>.
/// </summary>
/// <param name="f">A single-precision floating-point number.</param>
/// <returns>A <see cref="IEEE.Single"/> that represents the single-precision floating-point number.</returns>
public static explicit operator Single(
float f)
{
return (Single)Reflection.Deserialize(Reflection.Serialize(f,
typeof(
float)),
typeof(Single));
}
#
if DEBUG
/// <summary>
/// Returns the float value represented by this number.
/// </summary>
public float Value
{
get {
return (
float)
new Single(Sign, Exponent, Mantissa); }
}
/// <summary>
/// Returns the number in S EEEEEEEE MMMMMMMMMMMMMMMMMMMMMMM format, where S is sign, E unbiased exponent and M mantissa.
/// </summary>
public override string ToString()
{
char[] s =
new char[34];
s[0] = Sign ?
'1' :
'0';
s[1] = s[10] =
' ';
uint exp = (
uint)(Exponent);
for (
int i = 9; i >= 2; i--)
{
s[i] = ((exp & 1) == 1) ?
'1' :
'0';
exp >>= 1;
}
uint man = Mantissa;
for (
int i = 33; i >= 11; i--)
{
s[i] = ((man & 1) == 1) ?
'1' :
'0';
man >>= 1;
}
return new string(s);
}
}
#endif
}
}