Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a much-expanded version of Simon Hewitt's class:</p> <pre><code>/// &lt;summary&gt; /// Safely converts a &lt;see cref="float"/&gt; to an &lt;see cref="int"/&gt; for floating-point comparisons. /// &lt;/summary&gt; [StructLayout(LayoutKind.Explicit)] public struct FloatToInt : IEquatable&lt;FloatToInt&gt;, IEquatable&lt;float&gt;, IEquatable&lt;int&gt;, IComparable&lt;FloatToInt&gt;, IComparable&lt;float&gt;, IComparable&lt;int&gt; { /// &lt;summary&gt; /// Initializes a new instance of the &lt;see cref="FloatToInt"/&gt; class. /// &lt;/summary&gt; /// &lt;param name="floatValue"&gt;The &lt;see cref="float"/&gt; value to be converted to an &lt;see cref="int"/&gt;.&lt;/param&gt; public FloatToInt(float floatValue) : this() { FloatValue = floatValue; } /// &lt;summary&gt; /// Gets the floating-point value as an integer. /// &lt;/summary&gt; [FieldOffset(0)] public readonly int IntValue; /// &lt;summary&gt; /// Gets the floating-point value. /// &lt;/summary&gt; [FieldOffset(0)] public readonly float FloatValue; /// &lt;summary&gt; /// Indicates whether the current object is equal to another object of the same type. /// &lt;/summary&gt; /// &lt;returns&gt; /// true if the current object is equal to the &lt;paramref name="other"/&gt; parameter; otherwise, false. /// &lt;/returns&gt; /// &lt;param name="other"&gt;An object to compare with this object.&lt;/param&gt; public bool Equals(FloatToInt other) { return other.IntValue == IntValue; } /// &lt;summary&gt; /// Indicates whether the current object is equal to another object of the same type. /// &lt;/summary&gt; /// &lt;returns&gt; /// true if the current object is equal to the &lt;paramref name="other"/&gt; parameter; otherwise, false. /// &lt;/returns&gt; /// &lt;param name="other"&gt;An object to compare with this object.&lt;/param&gt; public bool Equals(float other) { return IntValue == new FloatToInt(other).IntValue; } /// &lt;summary&gt; /// Indicates whether the current object is equal to another object of the same type. /// &lt;/summary&gt; /// &lt;returns&gt; /// true if the current object is equal to the &lt;paramref name="other"/&gt; parameter; otherwise, false. /// &lt;/returns&gt; /// &lt;param name="other"&gt;An object to compare with this object.&lt;/param&gt; public bool Equals(int other) { return IntValue == other; } /// &lt;summary&gt; /// Compares the current object with another object of the same type. /// &lt;/summary&gt; /// &lt;returns&gt; /// A value that indicates the relative order of the objects being compared. The return value has the following meanings: Value Meaning Less than zero This object is less than the &lt;paramref name="other"/&gt; parameter.Zero This object is equal to &lt;paramref name="other"/&gt;. Greater than zero This object is greater than &lt;paramref name="other"/&gt;. /// &lt;/returns&gt; /// &lt;param name="other"&gt;An object to compare with this object.&lt;/param&gt; public int CompareTo(FloatToInt other) { return IntValue.CompareTo(other.IntValue); } /// &lt;summary&gt; /// Compares the current object with another object of the same type. /// &lt;/summary&gt; /// &lt;returns&gt; /// A value that indicates the relative order of the objects being compared. The return value has the following meanings: Value Meaning Less than zero This object is less than the &lt;paramref name="other"/&gt; parameter.Zero This object is equal to &lt;paramref name="other"/&gt;. Greater than zero This object is greater than &lt;paramref name="other"/&gt;. /// &lt;/returns&gt; /// &lt;param name="other"&gt;An object to compare with this object.&lt;/param&gt; public int CompareTo(float other) { return IntValue.CompareTo(new FloatToInt(other).IntValue); } /// &lt;summary&gt; /// Compares the current object with another object of the same type. /// &lt;/summary&gt; /// &lt;returns&gt; /// A value that indicates the relative order of the objects being compared. The return value has the following meanings: Value Meaning Less than zero This object is less than the &lt;paramref name="other"/&gt; parameter.Zero This object is equal to &lt;paramref name="other"/&gt;. Greater than zero This object is greater than &lt;paramref name="other"/&gt;. /// &lt;/returns&gt; /// &lt;param name="other"&gt;An object to compare with this object.&lt;/param&gt; public int CompareTo(int other) { return IntValue.CompareTo(other); } /// &lt;summary&gt; /// Indicates whether this instance and a specified object are equal. /// &lt;/summary&gt; /// &lt;returns&gt; /// true if &lt;paramref name="obj"/&gt; and this instance are the same type and represent the same value; otherwise, false. /// &lt;/returns&gt; /// &lt;param name="obj"&gt;Another object to compare to. &lt;/param&gt;&lt;filterpriority&gt;2&lt;/filterpriority&gt; public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) { return false; } if (obj.GetType() != typeof(FloatToInt)) { return false; } return Equals((FloatToInt)obj); } /// &lt;summary&gt; /// Returns the hash code for this instance. /// &lt;/summary&gt; /// &lt;returns&gt; /// A 32-bit signed integer that is the hash code for this instance. /// &lt;/returns&gt; /// &lt;filterpriority&gt;2&lt;/filterpriority&gt; public override int GetHashCode() { return IntValue; } /// &lt;summary&gt; /// Implicitly converts from a &lt;see cref="FloatToInt"/&gt; to an &lt;see cref="int"/&gt;. /// &lt;/summary&gt; /// &lt;param name="value"&gt;A &lt;see cref="FloatToInt"/&gt;.&lt;/param&gt; /// &lt;returns&gt;An integer representation of the floating-point value.&lt;/returns&gt; public static implicit operator int(FloatToInt value) { return value.IntValue; } /// &lt;summary&gt; /// Implicitly converts from a &lt;see cref="FloatToInt"/&gt; to a &lt;see cref="float"/&gt;. /// &lt;/summary&gt; /// &lt;param name="value"&gt;A &lt;see cref="FloatToInt"/&gt;.&lt;/param&gt; /// &lt;returns&gt;The floating-point value.&lt;/returns&gt; public static implicit operator float(FloatToInt value) { return value.FloatValue; } /// &lt;summary&gt; /// Determines if two &lt;see cref="FloatToInt"/&gt; instances have the same integer representation. /// &lt;/summary&gt; /// &lt;param name="left"&gt;A &lt;see cref="FloatToInt"/&gt;.&lt;/param&gt; /// &lt;param name="right"&gt;A &lt;see cref="FloatToInt"/&gt;.&lt;/param&gt; /// &lt;returns&gt;true if the two &lt;see cref="FloatToInt"/&gt; have the same integer representation; otherwise, false.&lt;/returns&gt; public static bool operator ==(FloatToInt left, FloatToInt right) { return left.IntValue == right.IntValue; } /// &lt;summary&gt; /// Determines if two &lt;see cref="FloatToInt"/&gt; instances have different integer representations. /// &lt;/summary&gt; /// &lt;param name="left"&gt;A &lt;see cref="FloatToInt"/&gt;.&lt;/param&gt; /// &lt;param name="right"&gt;A &lt;see cref="FloatToInt"/&gt;.&lt;/param&gt; /// &lt;returns&gt;true if the two &lt;see cref="FloatToInt"/&gt; have different integer representations; otherwise, false.&lt;/returns&gt; public static bool operator !=(FloatToInt left, FloatToInt right) { return !(left == right); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload