Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added IsInteger method in Fixed64.cs and Fixed64.Extensions.cs #21

Merged
merged 2 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/FixedMathSharp/Fixed64.Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ public static int Sign(this Fixed64 value)
return Fixed64.Sign(value);
}

/// <inheritdoc cref="Fixed64.IsInteger(Fixed64)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsInteger(this Fixed64 value)
{
return Fixed64.IsInteger(value);
}

/// <inheritdoc cref="FixedMath.Squared(Fixed64)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Fixed64 Squared(this Fixed64 value)
Expand Down
9 changes: 9 additions & 0 deletions src/FixedMathSharp/Fixed64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@
// Return the sign of the value, optimizing for branchless comparison
return value.m_rawValue < 0 ? -1 : (value.m_rawValue > 0 ? 1 : 0);
}

/// <summary>
/// Returns true if the number has no decimal part (i.e., if the number is equivalent to an integer) and False otherwise.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsInteger(Fixed64 value)
{
return ((ulong)value.m_rawValue & FixedMath.MAX_SHIFTED_AMOUNT_UI) == 0;
}

#endregion

Expand Down Expand Up @@ -646,7 +655,7 @@
/// Determines whether this instance equals another object.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Equals(object obj)

Check warning on line 658 in src/FixedMathSharp/Fixed64.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of type of parameter 'obj' doesn't match overridden member (possibly because of nullability attributes).
{
return obj is Fixed64 other && Equals(other);
}
Expand Down
39 changes: 39 additions & 0 deletions tests/FixedMathSharp.Tests/Fixed64.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,44 @@ public void Subtract_OverflowProtection_ReturnsMinValue()
}

#endregion

#region Test: Operations

[Fact]
public void IsInteger_PositiveInteger_ReturnsTrue()
{
var a = new Fixed64(42);
Assert.True(a.IsInteger());
}

[Fact]
public void IsInteger_NegativeInteger_ReturnsTrue()
{
var a = new Fixed64(-42);
Assert.True(a.IsInteger());
}

[Fact]
public void IsInteger_WhenZero_ReturnsTrue()
{
var a = Fixed64.Zero;
Assert.True(a.IsInteger());
}

[Fact]
public void IsInteger_PositiveDecimal_ReturnsFalse()
{
var a = new Fixed64(4.2);
Assert.False(a.IsInteger());
}

[Fact]
public void IsInteger_NegativeDecimal_ReturnsFalse()
{
var a = new Fixed64(-4.2);
Assert.False(a.IsInteger());
}

#endregion
}
}
Loading