Skip to content

Commit 63e6221

Browse files
authored
Improvements on double and single types (#95)
***PUBLISH_RELEASE*** ***UPDATE_DEPENDENTS***
1 parent 226b210 commit 63e6221

File tree

5 files changed

+197
-21
lines changed

5 files changed

+197
-21
lines changed

source/nanoFramework.CoreLibrary/System/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
[assembly: AssemblyProduct("nanoFramework mscorlib")]
1515
[assembly: AssemblyCopyright("Copyright © nanoFramework Contributors 2017")]
1616

17-
[assembly: AssemblyNativeVersion("100.4.3.0")]
17+
[assembly: AssemblyNativeVersion("100.4.5.0")]

source/nanoFramework.CoreLibrary/System/Convert.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,18 @@ public static double ToDouble(string value)
202202
return NativeToDouble(value);
203203
}
204204

205+
#pragma warning disable S4200 // Native methods should be wrapped
206+
/// <summary>
207+
/// Converts the specified string representation of a number to an equivalent single-precision floating-point number.
208+
/// </summary>
209+
/// <param name="value">A string that contains the number to convert.</param>
210+
/// <returns>A single-precision floating-point number that is equivalent to the number in value, or 0 (zero) if value is <see langword="null"/>.</returns>
211+
public static float ToSingle(string value)
212+
#pragma warning restore S4200 // Native methods should be wrapped
213+
{
214+
return (float)NativeToDouble(value);
215+
}
216+
205217
/// <summary>
206218
/// Converts an array of 8-bit unsigned integers to its equivalent String representation encoded with base 64 digits.
207219
/// </summary>

source/nanoFramework.CoreLibrary/System/Double.cs

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ namespace System
1515
[Serializable]
1616
public struct Double
1717
{
18+
internal const string _naNSymbol = "Nan";
19+
internal const string _negativeInfinitySymbol = "-" + _positiveInfinitySymbol;
20+
internal const string _positiveInfinitySymbol = "Infinity";
21+
1822
// this field is required in the native end
1923
#pragma warning disable 0649
2024
internal double _value;
@@ -30,34 +34,49 @@ public struct Double
3034
/// </summary>
3135
/// <remarks>The value of this constant is positive 1.7976931348623157E+308.</remarks>
3236
public const double MaxValue = 1.7976931348623157E+308;
37+
3338
/// <summary>
3439
/// Represents the smallest positive Double value that is greater than zero. This field is constant.
3540
/// </summary>
3641
/// <remarks>The value of this constant is 4.94065645841247e-324.</remarks>
3742
public const double Epsilon = 4.9406564584124650E-324;
43+
3844
/// <summary>
3945
/// Represents negative infinity. This field is constant.
4046
/// </summary>
4147
public const double NegativeInfinity = -1.0 / 0.0;
48+
4249
/// <summary>
4350
/// Represents positive infinity. This field is constant.
4451
/// </summary>
4552
public const double PositiveInfinity = 1.0 / 0.0;
53+
54+
#pragma warning disable S1764 // Identical expressions should not be used on both sides of a binary operator
55+
// intended as the purpose is to a NaN value
56+
4657
/// <summary>
4758
/// Represents a value that is not a number (NaN). This field is constant.
4859
/// </summary>
4960
public const double NaN = 0.0 / 0.0;
61+
#pragma warning restore S1764 // Identical expressions should not be used on both sides of a binary operator
5062

5163
/// <summary>
52-
/// Documentation missing
64+
/// Compares this instance to a specified double-precision floating-point number and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the specified double-precision floating-point number.
5365
/// </summary>
54-
/// <param name="d">Documentation missing</param>
55-
/// <param name="value">Documentation missing</param>
56-
/// <returns>Documentation missing</returns>
66+
/// <param name="d">A double-precision floating-point number to compare.</param>
67+
/// <param name="value">A double-precision floating-point number to compare.</param>
68+
/// <returns>A signed number indicating the relative values of this instance and value.
69+
/// Less than zero: This instance is less than value. -or- This instance is not a number (<see cref="NaN"/>) and value is a number.
70+
/// Zero: This instance is equal to value. -or- Both this instance and value are not a number (<see cref="NaN"/>), <see cref="PositiveInfinity"/>, or <see cref="NegativeInfinity"/>.
71+
/// Greater than zero: This instance is greater than value. -or- This instance is a number and value is not a number (<see cref="NaN"/>).
72+
/// </returns>
73+
public int CompareTo(double value)
74+
{
75+
return CompareTo(this, value);
76+
}
77+
5778
[MethodImpl(MethodImplOptions.InternalCall)]
58-
#pragma warning disable S4200 // Native methods should be wrapped
59-
public static extern int CompareTo(double d, double value);
60-
#pragma warning restore S4200 // Native methods should be wrapped
79+
internal static extern int CompareTo(double d, double value);
6180

6281
/// <summary>
6382
/// Returns a value indicating whether the specified number evaluates to negative or positive infinity
@@ -126,25 +145,32 @@ public static double Parse(String s)
126145
/// Converts the numeric value of this instance to its equivalent string representation.
127146
/// </summary>
128147
/// <returns>The string representation of the value of this instance.</returns>
129-
public override String ToString()
148+
public override string ToString()
130149
{
131-
if (IsPositiveInfinity(this)) return "Infinity";
132-
if (IsNegativeInfinity(this)) return "-Infinity";
133-
134-
return IsNaN(this) ? "NaN" : Number.Format(_value, false, "G", NumberFormatInfo.CurrentInfo);
150+
return ToString("G");
135151
}
136152

137153
/// <summary>
138154
/// Converts the numeric value of this instance to its equivalent string representation, using the specified format.
139155
/// </summary>
140156
/// <param name="format">A numeric format string.</param>
141157
/// <returns>The string representation of the value of this instance as specified by format.</returns>
142-
public String ToString(String format)
158+
public string ToString(string format)
143159
{
144-
if (IsPositiveInfinity(this)) return "Infinity";
145-
if (IsNegativeInfinity(this)) return "-Infinity";
160+
if (IsPositiveInfinity(this))
161+
{
162+
return _positiveInfinitySymbol;
163+
}
164+
else if (IsNegativeInfinity(this))
165+
{
166+
return _negativeInfinitySymbol;
167+
}
168+
else if (IsNaN(this))
169+
{
170+
return _naNSymbol;
171+
}
146172

147-
return IsNaN(this) ? "NaN" : Number.Format(_value, false, format, NumberFormatInfo.CurrentInfo);
173+
return Number.Format(_value, false, format, NumberFormatInfo.CurrentInfo);
148174
}
149175

150176
/// <summary>

source/nanoFramework.CoreLibrary/System/Single.cs

Lines changed: 141 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace System
88
{
99
using Globalization;
10+
using System.Runtime.CompilerServices;
1011

1112
/// <summary>
1213
/// Represents a single-precision floating-point number.
@@ -24,33 +25,170 @@ public struct Single
2425
/// </summary>
2526
/// <remarks>The value of this constant is negative 3.402823e38.</remarks>
2627
public const float MinValue = (float)-3.40282346638528859e+38;
28+
2729
/// <summary>
2830
/// Represents the smallest positive Single value that is greater than zero. This field is constant.
2931
/// </summary>
3032
public const float Epsilon = (float)1.4e-45;
33+
3134
/// <summary>
3235
/// Represents the largest possible value of Single. This field is constant.
3336
/// </summary>
3437
/// <remarks>The value of this constant is positive 3.40282347E+38.</remarks>
3538
public const float MaxValue = (float)3.40282346638528859e+38;
3639

40+
/// <summary>
41+
/// Represents negative infinity. This field is constant.
42+
/// </summary>
43+
public const float NegativeInfinity = -1.0f / 0.0f;
44+
45+
/// <summary>
46+
/// Represents positive infinity. This field is constant.
47+
/// </summary>
48+
public const float PositiveInfinity = 1.0f / 0.0f;
49+
50+
#pragma warning disable S1764 // Identical expressions should not be used on both sides of a binary operator
51+
// intended as the purpose is to a NaN value
52+
53+
/// <summary>
54+
/// Represents a value that is not a number (NaN). This field is constant.
55+
/// </summary>
56+
public const float NaN = 0.0f / 0.0f;
57+
#pragma warning restore S1764 // Identical expressions should not be used on both sides of a binary operator
58+
59+
/// <summary>
60+
/// Returns a value indicating whether the specified number evaluates to negative or positive infinity.
61+
/// </summary>
62+
/// <param name="f">A single-precision floating-point number. </param>
63+
/// <returns>
64+
/// <see langword="true"/> if f evaluates to <see cref="PositiveInfinity"/> or <see cref="NegativeInfinity"/>; otherwise, <see langword="false"/>.
65+
/// </returns>
66+
public static bool IsInfinity(float f)
67+
{
68+
return double.IsInfinity(f);
69+
}
70+
71+
/// <summary>
72+
/// Returns a value that indicates whether the specified value is not a number (<see cref="NaN"/>).
73+
/// </summary>
74+
/// <param name="f">A single-precision floating-point number. </param>
75+
/// <returns>
76+
/// <see langword="true"/> if f evaluates to <see cref="NaN"/>; otherwise, <see langword="false"/>.
77+
/// </returns>
78+
public static bool IsNaN(float f)
79+
{
80+
return double.IsNaN(f);
81+
}
82+
83+
/// <summary>
84+
/// Returns a value indicating whether the specified number evaluates to negative infinity.
85+
/// </summary>
86+
/// <param name="f">A single-precision floating-point number.</param>
87+
/// <returns>
88+
/// <see langword="true"/> if f evaluates to <see cref="NegativeInfinity"/>; otherwise, <see langword="false"/>.
89+
/// </returns>
90+
public static bool IsNegativeInfinity(float f)
91+
{
92+
return double.IsNegativeInfinity(f);
93+
}
94+
95+
/// <summary>
96+
/// Returns a value indicating whether the specified number evaluates to positive infinity.
97+
/// </summary>
98+
/// <param name="f">A single-precision floating-point number. </param>
99+
/// <returns>
100+
/// <see langword="true"/> if d evaluates to <see cref="PositiveInfinity"/>; otherwise, <see langword="false"/>.
101+
/// </returns>
102+
public static bool IsPositiveInfinity(float f)
103+
{
104+
return double.IsPositiveInfinity(f);
105+
}
106+
107+
/// <summary>
108+
/// Compares this instance to a specified single-precision floating-point number and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the specified single-precision floating-point number.
109+
/// </summary>
110+
/// <param name="f">A single-precision floating-point number to compare.</param>
111+
/// <param name="value">A single-precision floating-point number to compare.</param>
112+
/// <returns>A signed number indicating the relative values of this instance and value.
113+
/// Less than zero: This instance is less than value. -or- This instance is not a number (<see cref="NaN"/>) and value is a number.
114+
/// Zero: This instance is equal to value. -or- Both this instance and value are not a number (<see cref="NaN"/>), <see cref="PositiveInfinity"/>, or <see cref="NegativeInfinity"/>.
115+
/// Greater than zero: This instance is greater than value. -or- This instance is a number and value is not a number (<see cref="NaN"/>).
116+
/// </returns>
117+
public int CompareTo(float value)
118+
{
119+
return double.CompareTo(this, value);
120+
}
121+
122+
/// <summary>
123+
/// Converts the string representation of a number to its single-precision floating-point number equivalent.
124+
/// </summary>
125+
/// <param name="s">A string that contains a number to convert. </param>
126+
/// <returns>A single-precision floating-point number equivalent to the numeric value or symbol specified in <code>s</code>.</returns>
127+
/// <exception cref="System.ArgumentNullException"></exception>
128+
public static float Parse(string s)
129+
{
130+
#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one
131+
if (s == null) throw new ArgumentNullException();
132+
#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one
133+
134+
return Convert.ToSingle(s);
135+
}
136+
37137
/// <summary>
38138
/// Converts the numeric value of this instance to its equivalent string representation.
39139
/// </summary>
40140
/// <returns>The string representation of the value of this instance.</returns>
41-
public override String ToString()
141+
public override string ToString()
42142
{
43-
return Number.Format(_value, false, "G", NumberFormatInfo.CurrentInfo);
143+
return ToString("G");
44144
}
45145

46146
/// <summary>
47147
/// Converts the numeric value of this instance to its equivalent string representation, using the specified format.
48148
/// </summary>
49149
/// <param name="format">A numeric format string.</param>
50150
/// <returns>The string representation of the value of this instance as specified by format.</returns>
51-
public String ToString(String format)
151+
public string ToString(string format)
52152
{
153+
if (IsPositiveInfinity(this))
154+
{
155+
return double._positiveInfinitySymbol;
156+
}
157+
else if (IsNegativeInfinity(this))
158+
{
159+
return double._negativeInfinitySymbol;
160+
}
161+
else if(IsNaN(this))
162+
{
163+
return double._naNSymbol;
164+
}
165+
53166
return Number.Format(_value, false, format, NumberFormatInfo.CurrentInfo);
54167
}
168+
169+
/// <summary>
170+
/// Converts the string representation of a number to its single-precision floating-point number equivalent. A return value indicates whether the conversion succeeded or failed.
171+
/// </summary>
172+
/// <param name="s">A string containing a number to convert. </param>
173+
/// <param name="result">When this method returns, contains single-precision floating-point number equivalent to the numeric value or symbol contained in s, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is <see langword="null"/> or Empty, is not a number in a valid format, or represents a number less than <see cref="MinValue"/> or greater than <see cref="MaxValue"/>. This parameter is passed uninitialized; any value originally supplied in result will be overwritten.</param>
174+
/// <returns><see langword="true"/> if s was converted successfully; otherwise, <see langword="false"/>.</returns>
175+
public static bool TryParse(string s, out float result)
176+
{
177+
result = 0.0f;
178+
179+
if (s == null) return false;
180+
181+
try
182+
{
183+
result = Convert.ToSingle(s);
184+
return true;
185+
}
186+
catch
187+
{
188+
result = 0.0f;
189+
}
190+
191+
return false;
192+
}
55193
}
56194
}

source/version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/AArnott/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
3-
"version": "1.5.0-preview.{height}",
3+
"version": "1.5.1-preview.{height}",
44
"assemblyVersion": {
55
"precision": "revision"
66
},

0 commit comments

Comments
 (0)