Skip to content
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
24 changes: 24 additions & 0 deletions src/FixedPointNano/FixedPointNano.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,30 @@ public static bool IsInteger(FixedPointNano value)
return value.RawValue % Scale == 0;
}

/// <summary>Returns <see langword="true"/> if <paramref name="value"/> is strictly greater than zero.</summary>
/// <param name="value">The value to test.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsPositive(FixedPointNano value)
{
return value.RawValue > 0;
}

/// <summary>Returns <see langword="true"/> if <paramref name="value"/> is strictly less than zero.</summary>
/// <param name="value">The value to test.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNegative(FixedPointNano value)
{
return value.RawValue < 0;
}

/// <summary>Returns <see langword="true"/> if <paramref name="value"/> is exactly zero.</summary>
/// <param name="value">The value to test.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsZero(FixedPointNano value)
{
return value.RawValue == 0;
}

public static FixedPointNano FromDecimal(decimal value)
{
var scaledValue = decimal.Round(value * Scale, 0, MidpointRounding.ToEven);
Expand Down
32 changes: 32 additions & 0 deletions tests/FixedPointNano.Tests/FixedPointNanoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,38 @@ public void IsIntegerShouldDetectWholeness()
Assert.That(FixedPointNano.IsInteger(FixedPointNano.FromDecimal(-2m)), Is.True);
}

[Test]
public void IsPositiveShouldReturnTrueOnlyForPositiveValues()
{
Assert.That(FixedPointNano.IsPositive(FixedPointNano.Zero), Is.False);
Assert.That(FixedPointNano.IsPositive(FixedPointNano.Epsilon), Is.True);
Assert.That(FixedPointNano.IsPositive(FixedPointNano.One), Is.True);
Assert.That(FixedPointNano.IsPositive(FixedPointNano.MaxValue), Is.True);
Assert.That(FixedPointNano.IsPositive(FixedPointNano.NegativeOne), Is.False);
Assert.That(FixedPointNano.IsPositive(FixedPointNano.MinValue), Is.False);
}

[Test]
public void IsNegativeShouldReturnTrueOnlyForNegativeValues()
{
Assert.That(FixedPointNano.IsNegative(FixedPointNano.Zero), Is.False);
Assert.That(FixedPointNano.IsNegative(FixedPointNano.Epsilon), Is.False);
Assert.That(FixedPointNano.IsNegative(FixedPointNano.One), Is.False);
Assert.That(FixedPointNano.IsNegative(FixedPointNano.MaxValue), Is.False);
Assert.That(FixedPointNano.IsNegative(FixedPointNano.NegativeOne), Is.True);
Assert.That(FixedPointNano.IsNegative(FixedPointNano.MinValue), Is.True);
}

[Test]
public void IsZeroShouldReturnTrueOnlyForZero()
{
Assert.That(FixedPointNano.IsZero(FixedPointNano.Zero), Is.True);
Assert.That(FixedPointNano.IsZero(FixedPointNano.Epsilon), Is.False);
Assert.That(FixedPointNano.IsZero(-FixedPointNano.Epsilon), Is.False);
Assert.That(FixedPointNano.IsZero(FixedPointNano.One), Is.False);
Assert.That(FixedPointNano.IsZero(FixedPointNano.NegativeOne), Is.False);
}

[Test]
public void FractionalPartShouldReturnDecimalComponent()
{
Expand Down
Loading