System.Single Structure

public struct Single : IComparable, IFormattable, IComparable<Single>, IEquatable<Single>

Base Types

Object
  ValueType
    Single

This type implements IComparable, IFormattable, System.IComparable<System.Single>, and System.IEquatable<System.Single>.

Assembly

mscorlib

Library

ExtendedNumerics

Summary

Represents a 32-bit single-precision floating-point number.

Description

Single is a 32-bit single precision floating-point type that represents values ranging from approximately 1.5E-45 to 3.4E+38 and from approximately -1.5E-45 to -3.4E+38 with a precision of 7 decimal digits. The Single type conforms to standard IEC 60559:1989, Binary Floating-point Arithmetic for Microprocessor Systems.

A Single can represent the following values:

When performing binary operations, if one of the operands is a floating-point type (Double or Single ), then the other operand is required to be an integral type or a floating-point type and the operation is evaluated as follows:

The floating-point operators, including the assignment operators, do not throw exceptions. Instead, in exceptional situations, the result of a floating-point operation is zero, infinity, or NaN, as described below:

Conforming implementations of the CLI are permitted to perform floating-point operations using a precision that is higher than that required by the Single type. For example, hardware architectures that support an "extended" or "long double" floating-point type with greater range and precision than the Single type could implicitly perform all floating-point operations using this higher precision type. Expressions evaluated using a higher precision might cause a finite result to be produced instead of an infinity.

See Also

System Namespace

Members

Single Methods

Single.CompareTo(float) Method
Single.CompareTo(System.Object) Method
Single.Equals(System.Object) Method
Single.Equals(float) Method
Single.GetHashCode Method
Single.IsInfinity Method
Single.IsNaN Method
Single.IsNegativeInfinity Method
Single.IsPositiveInfinity Method
Single.Parse(System.String) Method
Single.Parse(System.String, System.Globalization.NumberStyles) Method
Single.Parse(System.String, System.IFormatProvider) Method
Single.Parse(System.String, System.Globalization.NumberStyles, System.IFormatProvider) Method
Single.ToString(System.String, System.IFormatProvider) Method
Single.ToString(System.IFormatProvider) Method
Single.ToString() Method
Single.ToString(System.String) Method

Single Fields

Single.Epsilon Field
Single.MaxValue Field
Single.MinValue Field
Single.NaN Field
Single.NegativeInfinity Field
Single.PositiveInfinity Field


Single.CompareTo(float) Method

public int CompareTo(float value);

Summary

Returns the sort order of the current instance compared to the specified Single .

Parameters

value
The Single to compare to the current instance.

Return Value

The return value is a negative number, zero, or a positive number reflecting the sort order of the current instance as compared to value. For non-zero return values, the exact value returned by this method is unspecified. The following table defines the return value:

Return ValueDescription
Any negative numberCurrent instance < value.

-or-

Current instance is a NaN and value is not a NaN.

ZeroCurrent instance == value .

-or-

Current instance and value are both NaN, positive infinity, or negative infinity.

A positive numberCurrent instance > value.

-or-

Current instance is not a NaN and value is a NaN.

Description

[Note: This method is implemented to support the System.IComparable<Single> interface.]

See Also

System.Single Structure, System Namespace

Single.CompareTo(System.Object) Method

public int CompareTo(object value);

Summary

Returns the sort order of the current instance compared to the specified Object .

Parameters

value
The Object to compare to the current instance.

Return Value

The return value is a negative number, zero, or a positive number reflecting the sort order of the current instance as compared to value. For non-zero return values, the exact value returned by this method is unspecified. The following table defines the return value:

Return ValueDescription
Any negative numberCurrent instance < value.

-or-

Current instance is a NaN and value is not a NaN and is not a null reference.

ZeroCurrent instance == value .

-or-

Current instance and value are both NaN, positive infinity, or negative infinity.

A positive numberCurrent instance > value.

-or-

value is a null reference.

-or-

Current instance is not a NaN and value is a NaN.

Exceptions

Exception TypeCondition
ArgumentExceptionvalue is not a null reference and is not of type Single.

Description

[Note: This method is implemented to support the IComparable interface. Note that, although a NaN is not considered to be equal to another NaN (even itself), the IComparable interface requires that A.CompareTo(A) return zero. ]

See Also

System.Single Structure, System Namespace

Single.Equals(System.Object) Method

public override bool Equals(object obj);

Summary

Determines whether the current instance and the specified Object represent the same type and value.

Parameters

obj
The Object to compare to the current instance.

Return Value

true if obj represents the same type and value as the current instance, otherwise false . If obj is a null reference or is not an instance of Single, returns false . If either obj or the current instance is a NaN and the other is not, returns false . If obj and the current instance are both NaN, positive infinity, or negative infinity, returns true .

Description

[Note: This method overrides System.Object.Equals(System.Object). ]

See Also

System.Single Structure, System Namespace

Single.Equals(float) Method

public override bool Equals(float obj);

Summary

Determines whether the current instance and the specified Single represent the same value.

Parameters

obj
The Single to compare to the current instance.

Return Value

true if obj represents the same value as the current instance, otherwise false . If either obj or the current instance is a NaN and the other is not, returns false . If obj and the current instance are both NaN, positive infinity, or negative infinity, returns true .

Description

[Note: This method is implemented to support the System.IEquatable<Single> interface.]

See Also

System.Single Structure, System Namespace

Single.GetHashCode Method

public override int GetHashCode();

Summary

Generates a hash code for the current instance.

Return Value

A Int32 containing the hash code for this instance.

Description

The algorithm used to generate the hash code is unspecified.

[Note: This method overrides System.Object.GetHashCode. ]

See Also

System.Single Structure, System Namespace

Single.IsInfinity Method

public static bool IsInfinity(float f);

Summary

Determines whether the specified Single represents an infinity, which can be either positive or negative.

Parameters

f
The Single to be checked.

Return Value

true if f represents a positive or negative infinity value; otherwise false .

Description

[Note: Floating-point operations return positive or negative infinity values to signal an overflow condition. ]

See Also

System.Single Structure, System Namespace

Single.IsNaN Method

public static bool IsNaN(float f);

Summary

Determines whether the value of the specified Single is undefined (Not-a-Number).

Parameters

f
The Single to be checked.

Return Value

true if f represents a NaN value; otherwise false .

Description

[Note: Floating-point operations return NaN values to signal that the result of the operation is undefined. For example, dividing (Single) 0.0 by 0.0 results in a NaN value.]

See Also

System.Single Structure, System Namespace

Single.IsNegativeInfinity Method

public static bool IsNegativeInfinity(float f);

Summary

Determines whether the specified Single represents a negative infinity value.

Parameters

f
The Single to be checked.

Return Value

true if f represents a negative infinity value; otherwise false .

Description

[Note: Floating-point operations return negative infinity values to signal an overflow condition. ]

See Also

System.Single Structure, System Namespace

Single.IsPositiveInfinity Method

public static bool IsPositiveInfinity(float f);

Summary

Determines whether the specified Single represents a positive infinity value.

Parameters

f
The Single to be checked.

Return Value

true if f represents a positive infinity value; otherwise false .

Description

[Note: Floating-point operations return positive infinity values to signal an overflow condition. ]

See Also

System.Single Structure, System Namespace

Single.Parse(System.String) Method

public static float Parse(string s);

Summary

Returns the specified String converted to a Single value.

Parameters

s
A String containing the value to convert. The string is interpreted using the System.Globalization.NumberStyles.Float and/or System.Globalization.NumberStyles.AllowThousands style.

Return Value

The Single value obtained from s. If s equals System.Globalization.NumberFormatInfo.NaNSymbol, this method returns System.Single.NaN .

Exceptions

Exception TypeCondition
ArgumentNullExceptions is a null reference.
FormatExceptions is not in the correct style.
OverflowExceptions represents a value that is less than System.Single.MinValue or greater than System.Single.MaxValue.

Description

This version of System.Single.Parse(System.String) is equivalent to System.Single.Parse(System.String)(s, System.Globalization.NumberStyles.Float| System.Globalization.NumberStyles.AllowThousands, null ).

The string s is parsed using the formatting information in a NumberFormatInfo initialized for the current system culture. [Note: For more information, see System.Globalization.NumberFormatInfo.CurrentInfo. ]

See Also

System.Single Structure, System Namespace

Single.Parse(System.String, System.Globalization.NumberStyles) Method

public static float Parse(string s, NumberStyles style);

Summary

Returns the specified String converted to a Single value.

Parameters

s
A String containing the value to convert. The string is interpreted using the style specified by style .
style
Zero or more NumberStyles values that specify the style of s. Specify multiple values for style using the bitwise OR operator. If style is a null reference, the string is interpreted using the System.Globalization.NumberStyles.Float and System.Globalization.NumberStyles.AllowThousands styles.

Return Value

The Single value obtained from s. If s equals System.Globalization.NumberFormatInfo.NaNSymbol, this method returns System.Single.NaN .

Exceptions

Exception TypeCondition
ArgumentNullExceptions is a null reference.
FormatExceptions is not in the correct style.
OverflowExceptions represents a value that is less than System.Single.MinValue or greater than System.Single.MaxValue.

Description

This version of System.Single.Parse(System.String) is equivalent to System.Single.Parse(System.String) (s, style, null).

The string s is parsed using the formatting information in a NumberFormatInfo initialized for the current system culture. [Note: For more information, see System.Globalization.NumberFormatInfo.CurrentInfo. ]

See Also

System.Single Structure, System Namespace

Single.Parse(System.String, System.IFormatProvider) Method

public static float Parse(string s, IFormatProvider provider);

Summary

Returns the specified String converted to a Single value.

Parameters

s
A String containing the value to convert. The string is interpreted using the System.Globalization.NumberStyles.Float and/or System.Globalization.NumberStyles.AllowThousands style.
provider
A IFormatProvider that supplies a NumberFormatInfo containing culture-specific formatting information about s.

Return Value

The Single value obtained from s. If s equals System.Globalization.NumberFormatInfo.NaNSymbol, this method returns System.Single.NaN .

Exceptions

Exception TypeCondition
ArgumentNullExceptions is a null reference.
FormatExceptions is not in the correct style.
OverflowExceptions represents a value that is less than System.Single.MinValue or greater than System.Single.MaxValue.

Description

This version of System.Single.Parse(System.String) is equivalent to System.Single.Parse(System.String) (s, System.Globalization.NumberStyles.Float | System.Globalization.NumberStyles.AllowThousands , provider).

The string s is parsed using the culture-specific formatting information from the NumberFormatInfo instance supplied by provider. If provider is null or a NumberFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used.

See Also

System.Single Structure, System Namespace

Single.Parse(System.String, System.Globalization.NumberStyles, System.IFormatProvider) Method

public static float Parse(string s, NumberStyles style, IFormatProvider provider);

Summary

Returns the specified String converted to a Single value.

Parameters

s
A String containing the value to convert. The string is interpreted using the style specified by style .
style
Zero or more NumberStyles values that specify the style of s. Specify multiple values for style using the bitwise OR operator. If style is a null reference, the string is interpreted using the System.Globalization.NumberStyles.Float and System.Globalization.NumberStyles.AllowThousands styles.
provider
A IFormatProvider that supplies a NumberFormatInfo containing culture-specific formatting information about s.

Return Value

The Single value obtained from s. If s equals System.Globalization.NumberFormatInfo.NaNSymbol, this method returns NaN.

Exceptions

Exception TypeCondition
ArgumentNullExceptions is a null reference.
FormatExceptions is not in the correct style.
OverflowExceptions represents a value that is less than System.Single.MinValue or greater than System.Single.MaxValue.

Description

The string s is parsed using the culture-specific formatting information from the NumberFormatInfo instance supplied by provider. If provider is null or a NumberFormatInfo cannot be obtained from provider , the formatting information for the current system culture is used.

See Also

System.Single Structure, System Namespace

Single.ToString(System.String, System.IFormatProvider) Method

public string ToString(string format, IFormatProvider provider);

Summary

Returns a String representation of the value of the current instance.

Parameters

format
A String containing a character that specifies the format of the returned string, optionally followed by a non-negative integer that specifies the precision of the number in the returned String.
provider
A IFormatProvider that supplies a NumberFormatInfo instance containing culture-specific formatting information.

Return Value

A String representation of the current instance formatted as specified by format. The string takes into account the information in the NumberFormatInfo instance supplied by provider.

Exceptions

Exception TypeCondition
FormatExceptionformat is invalid.

Description

If provider is null or a NumberFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used.

If format is a null reference, the general format specifier "G" is used.

The following table lists the format characters that are valid for the Single type.

Format CharactersDescription
"C", "c"Currency format.
"E", "e"Exponential notation format.
"F", "f"Fixed-point format.
"G", "g"General format.
"N", "n"Number format.
"P", "p"Percent format.
"R", "r"Round-trip format.
[Note: For a detailed description of the format strings, see the IFormattable interface.

This method is implemented to support the IFormattable interface.

]

See Also

System.Single Structure, System Namespace

Single.ToString(System.IFormatProvider) Method

public string ToString(IFormatProvider provider);

Summary

Returns a String representation of the value of the current instance.

Parameters

provider
A IFormatProvider that supplies a NumberFormatInfo containing culture-specific formatting information.

Return Value

A String representation of the current instance formatted using the general format specifier, ("G"). The string takes into account the formatting information in the NumberFormatInfo instance supplied by provider.

Description

This version of System.Single.ToString is equivalent to System.Single.ToString (null , provider ).

If provider is null or a NumberFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used.

[Note: The general format specifier formats the number in either fixed-point or exponential notation form. For a detailed description of the general format, see the IFormattable interface. ]

See Also

System.Single Structure, System Namespace

Single.ToString() Method

public override string ToString();

Summary

Returns a String representation of the value of the current instance.

Return Value

A String representation of the current instance formatted using the general format specifier, ("G"). The string takes into account the current system culture.

Description

This version of System.Single.ToString is equivalent to System.Single.ToString (null , null ).

[Note: The general format specifier formats the number in either fixed-point or exponential notation form. For a detailed description of the general format, see the IFormattable interface.

This method overrides System.Object.ToString.

]

See Also

System.Single Structure, System Namespace

Single.ToString(System.String) Method

public string ToString(string format);

Summary

Returns a String representation of the value of the current instance.

Parameters

format
A String that specifies the format of the returned string. [Note: For a list of valid values, see System.Single.ToString (String, IFormatProvider ).]

Return Value

A String representation of the current instance formatted as specified by format . The string takes into account the current system culture.

Exceptions

Exception TypeCondition
FormatExceptionformat is invalid.

Description

This version of System.Single.ToString is equivalent to System.Single.ToString (format, null ).

If format is a null reference, the general format specifier "G" is used.

Example

The following example shows the effects of various formats on the string returned by System.Single.ToString .

using System;
class test {
 public static void Main() {
   float f = 1234.567f;
   Console.WriteLine(f);
   string[] fmts = {"C","E","e5","F","G","N","P","R"};
   for (int i=0;i<fmts.Length;i++)
     Console.WriteLine("{0}: {1}", 
       fmts[i],f.ToString(fmts[i]));
 }
}
   
The output is

1234.567

C: $1,234.57

E: 1.234567E+003

e5: 1.23457e+003

F: 1234.57

G: 1234.567

N: 1,234.57

P: 123,456.70 %

R: 1234.567

See Also

System.Single Structure, System Namespace

Single.Epsilon Field

public const float Epsilon = (float)1.401298E-45;

Summary

Represents the smallest positive Single value greater than zero.

Description

The value of this constant is 1.401298E-45.

See Also

System.Single Structure, System Namespace

Single.MaxValue Field

public const float MaxValue = (float)3.402823E+38;

Summary

Contains the maximum positive value for the Single type.

Description

The value of this constant is 3.40282346638528859E+38 converted to Single .

See Also

System.Single Structure, System Namespace

Single.MinValue Field

public const float MinValue = (float)-3.402823E+38;

Summary

Contains the minimum (most negative) value for the Single type.

Description

The value of this constant is -3.40282346638528859E+38 converted to Single .

See Also

System.Single Structure, System Namespace

Single.NaN Field

public const float NaN = (float)0.0 / (float)0.0;

Summary

Represents an undefined result of operations involving Single .

Description

Not-a-Number (NaN) values are returned when the result of a Single operation is undefined.

A NaN value is not equal to any other value, including another NaN value.

The value of this field is obtained by dividing Single zero by zero.

[Note: System.Single.NaN represents one of many possible NaN values. To test whether a Single value is a NaN, use the System.Single.IsNaN(System.Single) method. ]

See Also

System.Single Structure, System Namespace

Single.NegativeInfinity Field

public const float NegativeInfinity = (float)-1.0 / (float)0.0;

Summary

Represents a negative infinity of type Single .

Description

The value of this constant can be obtained by dividing a negative Single by zero.

[Note: To test whether a Single value is a negative infinity value, use the System.Single.IsNegativeInfinity(System.Single) method. ]

See Also

System.Single Structure, System Namespace

Single.PositiveInfinity Field

public const float PositiveInfinity = (float)1.0 / (float)0.0;

Summary

Represents a positive infinity of type Single.

Description

The value of this constant can be obtained by dividing a positive Single by zero.

[Note: To test whether a Single value is a positive infinity value, use the System.Single.IsPositiveInfinity(System.Single) method. ]

See Also

System.Single Structure, System Namespace