Skip to content

Commit 8808be5

Browse files
authored
feat: Added IsInteger method in Fixed64.cs and Fixed64.Extensions.cs (#21)
* feat: Added IsInteger method in Fixed64.cs and Fixed64.Extensions.cs * fix: using MAX_SHIFTED_AMOUNT_UI instead of uint.MaxValue
1 parent 51f4fe2 commit 8808be5

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

src/FixedMathSharp/Fixed64.Extensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ public static int Sign(this Fixed64 value)
1616
return Fixed64.Sign(value);
1717
}
1818

19+
/// <inheritdoc cref="Fixed64.IsInteger(Fixed64)" />
20+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
21+
public static bool IsInteger(this Fixed64 value)
22+
{
23+
return Fixed64.IsInteger(value);
24+
}
25+
1926
/// <inheritdoc cref="FixedMath.Squared(Fixed64)" />
2027
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2128
public static Fixed64 Squared(this Fixed64 value)

src/FixedMathSharp/Fixed64.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,15 @@ public static int Sign(Fixed64 value)
150150
// Return the sign of the value, optimizing for branchless comparison
151151
return value.m_rawValue < 0 ? -1 : (value.m_rawValue > 0 ? 1 : 0);
152152
}
153+
154+
/// <summary>
155+
/// Returns true if the number has no decimal part (i.e., if the number is equivalent to an integer) and False otherwise.
156+
/// </summary>
157+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
158+
public static bool IsInteger(Fixed64 value)
159+
{
160+
return ((ulong)value.m_rawValue & FixedMath.MAX_SHIFTED_AMOUNT_UI) == 0;
161+
}
153162

154163
#endregion
155164

tests/FixedMathSharp.Tests/Fixed64.Tests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,44 @@ public void Subtract_OverflowProtection_ReturnsMinValue()
190190
}
191191

192192
#endregion
193+
194+
#region Test: Operations
195+
196+
[Fact]
197+
public void IsInteger_PositiveInteger_ReturnsTrue()
198+
{
199+
var a = new Fixed64(42);
200+
Assert.True(a.IsInteger());
201+
}
202+
203+
[Fact]
204+
public void IsInteger_NegativeInteger_ReturnsTrue()
205+
{
206+
var a = new Fixed64(-42);
207+
Assert.True(a.IsInteger());
208+
}
209+
210+
[Fact]
211+
public void IsInteger_WhenZero_ReturnsTrue()
212+
{
213+
var a = Fixed64.Zero;
214+
Assert.True(a.IsInteger());
215+
}
216+
217+
[Fact]
218+
public void IsInteger_PositiveDecimal_ReturnsFalse()
219+
{
220+
var a = new Fixed64(4.2);
221+
Assert.False(a.IsInteger());
222+
}
223+
224+
[Fact]
225+
public void IsInteger_NegativeDecimal_ReturnsFalse()
226+
{
227+
var a = new Fixed64(-4.2);
228+
Assert.False(a.IsInteger());
229+
}
230+
231+
#endregion
193232
}
194233
}

0 commit comments

Comments
 (0)