diff --git a/source/nanoFramework.CoreLibrary/System/AssemblyInfo.cs b/source/nanoFramework.CoreLibrary/System/AssemblyInfo.cs index 806a0916..216071d8 100644 --- a/source/nanoFramework.CoreLibrary/System/AssemblyInfo.cs +++ b/source/nanoFramework.CoreLibrary/System/AssemblyInfo.cs @@ -14,4 +14,4 @@ [assembly: AssemblyProduct("nanoFramework mscorlib")] [assembly: AssemblyCopyright("Copyright © nanoFramework Contributors 2017")] -[assembly: AssemblyNativeVersion("100.4.3.0")] +[assembly: AssemblyNativeVersion("100.4.5.0")] diff --git a/source/nanoFramework.CoreLibrary/System/Convert.cs b/source/nanoFramework.CoreLibrary/System/Convert.cs index 54089150..047c6358 100644 --- a/source/nanoFramework.CoreLibrary/System/Convert.cs +++ b/source/nanoFramework.CoreLibrary/System/Convert.cs @@ -202,6 +202,18 @@ public static double ToDouble(string value) return NativeToDouble(value); } +#pragma warning disable S4200 // Native methods should be wrapped + /// + /// Converts the specified string representation of a number to an equivalent single-precision floating-point number. + /// + /// A string that contains the number to convert. + /// A single-precision floating-point number that is equivalent to the number in value, or 0 (zero) if value is . + public static float ToSingle(string value) +#pragma warning restore S4200 // Native methods should be wrapped + { + return (float)NativeToDouble(value); + } + /// /// Converts an array of 8-bit unsigned integers to its equivalent String representation encoded with base 64 digits. /// diff --git a/source/nanoFramework.CoreLibrary/System/Double.cs b/source/nanoFramework.CoreLibrary/System/Double.cs index 1e51c443..65736d9f 100644 --- a/source/nanoFramework.CoreLibrary/System/Double.cs +++ b/source/nanoFramework.CoreLibrary/System/Double.cs @@ -15,6 +15,10 @@ namespace System [Serializable] public struct Double { + internal const string _naNSymbol = "Nan"; + internal const string _negativeInfinitySymbol = "-" + _positiveInfinitySymbol; + internal const string _positiveInfinitySymbol = "Infinity"; + // this field is required in the native end #pragma warning disable 0649 internal double _value; @@ -30,34 +34,49 @@ public struct Double /// /// The value of this constant is positive 1.7976931348623157E+308. public const double MaxValue = 1.7976931348623157E+308; + /// /// Represents the smallest positive Double value that is greater than zero. This field is constant. /// /// The value of this constant is 4.94065645841247e-324. public const double Epsilon = 4.9406564584124650E-324; + /// /// Represents negative infinity. This field is constant. /// public const double NegativeInfinity = -1.0 / 0.0; + /// /// Represents positive infinity. This field is constant. /// public const double PositiveInfinity = 1.0 / 0.0; + +#pragma warning disable S1764 // Identical expressions should not be used on both sides of a binary operator + // intended as the purpose is to a NaN value + /// /// Represents a value that is not a number (NaN). This field is constant. /// public const double NaN = 0.0 / 0.0; +#pragma warning restore S1764 // Identical expressions should not be used on both sides of a binary operator /// - /// Documentation missing + /// 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. /// - /// Documentation missing - /// Documentation missing - /// Documentation missing + /// A double-precision floating-point number to compare. + /// A double-precision floating-point number to compare. + /// A signed number indicating the relative values of this instance and value. + /// Less than zero: This instance is less than value. -or- This instance is not a number () and value is a number. + /// Zero: This instance is equal to value. -or- Both this instance and value are not a number (), , or . + /// Greater than zero: This instance is greater than value. -or- This instance is a number and value is not a number (). + /// + public int CompareTo(double value) + { + return CompareTo(this, value); + } + [MethodImpl(MethodImplOptions.InternalCall)] -#pragma warning disable S4200 // Native methods should be wrapped - public static extern int CompareTo(double d, double value); -#pragma warning restore S4200 // Native methods should be wrapped + internal static extern int CompareTo(double d, double value); /// /// Returns a value indicating whether the specified number evaluates to negative or positive infinity @@ -126,12 +145,9 @@ public static double Parse(String s) /// Converts the numeric value of this instance to its equivalent string representation. /// /// The string representation of the value of this instance. - public override String ToString() + public override string ToString() { - if (IsPositiveInfinity(this)) return "Infinity"; - if (IsNegativeInfinity(this)) return "-Infinity"; - - return IsNaN(this) ? "NaN" : Number.Format(_value, false, "G", NumberFormatInfo.CurrentInfo); + return ToString("G"); } /// @@ -139,12 +155,22 @@ public override String ToString() /// /// A numeric format string. /// The string representation of the value of this instance as specified by format. - public String ToString(String format) + public string ToString(string format) { - if (IsPositiveInfinity(this)) return "Infinity"; - if (IsNegativeInfinity(this)) return "-Infinity"; + if (IsPositiveInfinity(this)) + { + return _positiveInfinitySymbol; + } + else if (IsNegativeInfinity(this)) + { + return _negativeInfinitySymbol; + } + else if (IsNaN(this)) + { + return _naNSymbol; + } - return IsNaN(this) ? "NaN" : Number.Format(_value, false, format, NumberFormatInfo.CurrentInfo); + return Number.Format(_value, false, format, NumberFormatInfo.CurrentInfo); } /// diff --git a/source/nanoFramework.CoreLibrary/System/Single.cs b/source/nanoFramework.CoreLibrary/System/Single.cs index 2a71f92c..e88b3129 100644 --- a/source/nanoFramework.CoreLibrary/System/Single.cs +++ b/source/nanoFramework.CoreLibrary/System/Single.cs @@ -7,6 +7,7 @@ namespace System { using Globalization; + using System.Runtime.CompilerServices; /// /// Represents a single-precision floating-point number. @@ -24,23 +25,122 @@ public struct Single /// /// The value of this constant is negative 3.402823e38. public const float MinValue = (float)-3.40282346638528859e+38; + /// /// Represents the smallest positive Single value that is greater than zero. This field is constant. /// public const float Epsilon = (float)1.4e-45; + /// /// Represents the largest possible value of Single. This field is constant. /// /// The value of this constant is positive 3.40282347E+38. public const float MaxValue = (float)3.40282346638528859e+38; + /// + /// Represents negative infinity. This field is constant. + /// + public const float NegativeInfinity = -1.0f / 0.0f; + + /// + /// Represents positive infinity. This field is constant. + /// + public const float PositiveInfinity = 1.0f / 0.0f; + +#pragma warning disable S1764 // Identical expressions should not be used on both sides of a binary operator + // intended as the purpose is to a NaN value + + /// + /// Represents a value that is not a number (NaN). This field is constant. + /// + public const float NaN = 0.0f / 0.0f; +#pragma warning restore S1764 // Identical expressions should not be used on both sides of a binary operator + + /// + /// Returns a value indicating whether the specified number evaluates to negative or positive infinity. + /// + /// A single-precision floating-point number. + /// + /// if f evaluates to or ; otherwise, . + /// + public static bool IsInfinity(float f) + { + return double.IsInfinity(f); + } + + /// + /// Returns a value that indicates whether the specified value is not a number (). + /// + /// A single-precision floating-point number. + /// + /// if f evaluates to ; otherwise, . + /// + public static bool IsNaN(float f) + { + return double.IsNaN(f); + } + + /// + /// Returns a value indicating whether the specified number evaluates to negative infinity. + /// + /// A single-precision floating-point number. + /// + /// if f evaluates to ; otherwise, . + /// + public static bool IsNegativeInfinity(float f) + { + return double.IsNegativeInfinity(f); + } + + /// + /// Returns a value indicating whether the specified number evaluates to positive infinity. + /// + /// A single-precision floating-point number. + /// + /// if d evaluates to ; otherwise, . + /// + public static bool IsPositiveInfinity(float f) + { + return double.IsPositiveInfinity(f); + } + + /// + /// 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. + /// + /// A single-precision floating-point number to compare. + /// A single-precision floating-point number to compare. + /// A signed number indicating the relative values of this instance and value. + /// Less than zero: This instance is less than value. -or- This instance is not a number () and value is a number. + /// Zero: This instance is equal to value. -or- Both this instance and value are not a number (), , or . + /// Greater than zero: This instance is greater than value. -or- This instance is a number and value is not a number (). + /// + public int CompareTo(float value) + { + return double.CompareTo(this, value); + } + + /// + /// Converts the string representation of a number to its single-precision floating-point number equivalent. + /// + /// A string that contains a number to convert. + /// A single-precision floating-point number equivalent to the numeric value or symbol specified in s. + /// + public static float Parse(string s) + { +#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one + if (s == null) throw new ArgumentNullException(); +#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one + + return Convert.ToSingle(s); + } + /// /// Converts the numeric value of this instance to its equivalent string representation. /// /// The string representation of the value of this instance. - public override String ToString() + public override string ToString() { - return Number.Format(_value, false, "G", NumberFormatInfo.CurrentInfo); + return ToString("G"); } /// @@ -48,9 +148,47 @@ public override String ToString() /// /// A numeric format string. /// The string representation of the value of this instance as specified by format. - public String ToString(String format) + public string ToString(string format) { + if (IsPositiveInfinity(this)) + { + return double._positiveInfinitySymbol; + } + else if (IsNegativeInfinity(this)) + { + return double._negativeInfinitySymbol; + } + else if(IsNaN(this)) + { + return double._naNSymbol; + } + return Number.Format(_value, false, format, NumberFormatInfo.CurrentInfo); } + + /// + /// 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. + /// + /// A string containing a number to convert. + /// 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 or Empty, is not a number in a valid format, or represents a number less than or greater than . This parameter is passed uninitialized; any value originally supplied in result will be overwritten. + /// if s was converted successfully; otherwise, . + public static bool TryParse(string s, out float result) + { + result = 0.0f; + + if (s == null) return false; + + try + { + result = Convert.ToSingle(s); + return true; + } + catch + { + result = 0.0f; + } + + return false; + } } } diff --git a/source/version.json b/source/version.json index 2b9d5950..5c52dff7 100644 --- a/source/version.json +++ b/source/version.json @@ -1,6 +1,6 @@ { "$schema": "https://raw.githubusercontent.com/AArnott/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json", - "version": "1.5.0-preview.{height}", + "version": "1.5.1-preview.{height}", "assemblyVersion": { "precision": "revision" },