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
#define HIGH_PRECISION
using Microsoft.SPOT;
using System;
namespace UAM.InformatiX.SPOT
{
/// <summary>
/// Provides contants and methods for mathematical functions.
/// </summary>
public static class Math
{
#region Constants
/// <summary>
/// Represents not a number (NaN). This field is constant.
/// </summary>
public const float NaN = 0.0f / 0.0f;
/// <summary>
/// Represents negative infinity. This field is constant.
/// </summary>
public const float NegativeInfinity = -1.0f / 0.0f;
/// <summary>
/// Represents positive infinity. This field is constant.
/// </summary>
public const float PositiveInfinity = 1.0f / 0.0f;
/// <summary>Returns a value indicating whether the specified number evaluates to not a number (<see cref="F:System.Single.NaN"></see>).</summary>
/// <returns>true if f evaluates to not a number (<see cref="F:System.Single.NaN"></see>); otherwise, false.</returns>
/// <param name="f">A single-precision floating point number. </param>
public static bool IsNaN(
float f)
{
#pragma warning disable 1718
return (f != f);
#pragma warning restore 1718
}
#endregion
#region Single-point precision logarithms
/// <summary>
/// Returns the base 2 logarithm of a specified number.
/// </summary>
/// <param name="d">A number whose logarithm is to be found.</param>
public static float Log2(
float f)
{
if (f < 0)
return NaN;
if (f == 0)
return NegativeInfinity;
if (f == 1)
return 0f;
IEEE.Single result = (IEEE.Single)f;
float wholeLog = result.Exponent;
float squaring = (
float)
new IEEE.Single(
false, 0, result.Mantissa);
result.Mantissa = 0;
#
if HIGH_PRECISION
for (
int j = 0; j < 4; j++)
#
else
for (
int j = 0; j < 3; j++)
#endif
{
result.Mantissa <<= 7;
for (
int i = 0; i < 7; i++)
squaring *= squaring;
IEEE.Single parsedSquaring = (IEEE.Single)squaring;
result.Mantissa += (
uint)parsedSquaring.Exponent;
parsedSquaring.Exponent = 0;
squaring = (
float)parsedSquaring;
}
#
if HIGH_PRECISION
result.Mantissa >>= 5;
#
else
result.Mantissa <<= 2;
#endif
result.Exponent = 0;
return wholeLog + (
float)result - 1;
}
/// <summary>
/// Returns the natural (base e) logarithm of a specified number.
/// </summary>
/// <param name="f">A number whose logarithm is to be found.</param>
public static float Log(
float f) {
return Log2(f) * SingleLog2toE; }
private const float SingleLog2toE = 0.69314718055994529f;
/// <summary>
/// Returns the base 10 logarithm of a specified number.
/// </summary>
/// <param name="f">A number whose logarithm is to be found.</param>
public static float Log10(
float f) {
return Log2(f) * SingleLog2to10; }
private const float SingleLog2to10 = 0.30102999566398114f;
/// <summary>
/// Returns the logarithm of a specified number in a specified base.
/// </summary>
/// <param name="f">A number whose logarithm is to be found.</param>
/// <param name="newBase">The base of the logarithm.</param>
public static float Log(
float f,
float newBase)
{
if (newBase == 1)
return NaN;
if (f != 1 && (newBase == 0 || newBase >= PositiveInfinity))
return NaN;
return Log2(f) / Log2(newBase);
}
/// <summary>
/// Returns the base 2 fast logarithm of a specified number. No parameter checking is performed and the result has maximum error of +0.086071 (about 3.5 bits) at 1/ln 2.
/// </summary>
/// <param name="f"></param>
/// <returns></returns>
public static float FastLog2(
float f)
{
IEEE.Single parsed = (IEEE.Single)f;
return parsed.Exponent + (
float)
new IEEE.Single(
false, 0, parsed.Mantissa) - 1;
}
#endregion
}
}