Skip to content

Commit

Permalink
Added Abs method and fixed a bug in fraction comparison when fraction…
Browse files Browse the repository at this point in the history
…s have negative denominator
  • Loading branch information
bazzilic committed Sep 12, 2022
1 parent 1a36a11 commit 4654924
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 74 deletions.
42 changes: 19 additions & 23 deletions .github/workflows/release-dotnet-nuget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@ jobs:
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup nuget
uses: nuget/setup-nuget@v1
- name: Update nuget source
uses: actions/checkout@v3
- name: Add github packages store as NuGet source
run: |
nuget sources update -Name 'github' `
-Source https://nuget.pkg.github.com/aprismatic/index.json `
-UserName ${{ secrets.GithubUsername }} `
-Password ${{ secrets.GithubToken }} `
-ConfigFile ./nuget.config
dotnet nuget update source github `
--source https://nuget.pkg.github.com/aprismatic/index.json `
--username "${{ github.actor }}" `
--password "${{ github.token }}" `
--configfile ./nuget.config
- name: Restore packages
run: nuget restore
run: dotnet restore
- name: Build with dotnet
run: dotnet build --configuration Release --no-restore
- name: Run tests
Expand All @@ -31,24 +29,22 @@ jobs:
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup nuget
uses: nuget/setup-nuget@v1
- name: Update nuget source
uses: actions/checkout@v3
- name: Add github packages store as NuGet source
run: |
nuget sources update -Name 'github' `
-Source https://nuget.pkg.github.com/aprismatic/index.json `
-UserName ${{ secrets.GithubUsername }} `
-Password ${{ secrets.GithubToken }} `
-ConfigFile ./nuget.config
dotnet nuget update source github `
--source https://nuget.pkg.github.com/aprismatic/index.json `
--username "${{ github.actor }}" `
--password "${{ github.token }}" `
--configfile ./nuget.config
- name: Restore packages
run: nuget restore
run: dotnet restore
- name: Build with dotnet
run: dotnet build --configuration Release --no-restore -p:Version=${{ github.event.release.tag_name }}
- name: Pack nuget packages
run: dotnet pack --configuration Release --no-build --output nupkgs -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg -p:PackageVersion=${{ github.event.release.tag_name }}
- name: Publish nuget packages
run: dotnet nuget push **/*.nupkg --source "github"
run: dotnet nuget push **/*.nupkg --source "github" --skip-duplicate --api-key "${{ github.token }}"
- name: Upload artifacts
uses: actions/upload-artifact@v1
with:
Expand All @@ -67,6 +63,6 @@ jobs:
- name: Release artifacts
uses: skx/github-action-publish-binaries@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ github.token }}
with:
args: 'nupkgs/*'
args: 'nupkgs/*'
20 changes: 9 additions & 11 deletions .github/workflows/test-dotnet-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ jobs:
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup nuget
uses: nuget/setup-nuget@v1
- name: Update nuget source
uses: actions/checkout@v3
- name: Add github packages store as NuGet source
run: |
nuget sources update -Name 'github' `
-Source https://nuget.pkg.github.com/aprismatic/index.json `
-UserName ${{ secrets.GithubUsername }} `
-Password ${{ secrets.GithubToken }} `
-ConfigFile ./nuget.config
dotnet nuget update source github `
--source https://nuget.pkg.github.com/aprismatic/index.json `
--username "${{ github.actor }}" `
--password "${{ github.token }}" `
--configfile ./nuget.config
- name: Restore packages
run: nuget restore
run: dotnet restore
- name: Build with dotnet
run: dotnet build --configuration Release --no-restore
- name: Run tests
run: dotnet test --configuration Release --no-build
run: dotnet test --configuration Release --no-build
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,6 @@ MigrationBackup/
# VS Code
.vscode/


# Common IntelliJ Platform excludes
.idea/

Expand Down
1 change: 1 addition & 0 deletions src/Aprismatic.BigFraction/Aprismatic.BigFraction.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<TargetFramework>netstandard1.1</TargetFramework>
<Description>BigFraction implementation using .NET BigInteger class.</Description>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<RepositoryUrl>https://github.com/aprismatic/bigfraction</RepositoryUrl>
</PropertyGroup>

<PropertyGroup>
Expand Down
61 changes: 31 additions & 30 deletions src/Aprismatic.BigFraction/BigFraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public readonly struct BigFraction
private static readonly BigInteger MAX_DECIMAL = new BigInteger(decimal.MaxValue);
private static readonly BigInteger MIN_DECIMAL = new BigInteger(decimal.MinValue);


/// <summary>
/// Predefined constant of a BigFraction that represents –1.
/// </summary>
Expand All @@ -40,7 +39,7 @@ public readonly struct BigFraction

// FIELDS
/// <summary>
/// Returns the sign of the fraction.
/// Returns the sign of the fraction (-1, 0, or 1).
/// </summary>
public int Sign =>
Numerator.IsZero ? 0 :
Expand All @@ -58,6 +57,7 @@ public readonly struct BigFraction
public bool IsOne => Numerator == Denominator;


#region Constructors
// CONSTRUCTORS

// Fractional constructor
Expand Down Expand Up @@ -151,7 +151,9 @@ public BigFraction(int i)
Numerator = new BigInteger(i);
Denominator = BigInteger.One;
}
#endregion

#region Operators
// OPERATORS

// BigInteger to BigFraction
Expand Down Expand Up @@ -196,22 +198,6 @@ public BigFraction(int i)
/// <param name="value">Fraction to negate</param>
public static BigFraction operator -(BigFraction value) => new BigFraction(-value.Numerator, value.Denominator);

/*
// Operator %
/// <summary>
/// Modulus operator
/// </summary>
/// <param name="r"></param>
/// <param name="mod"></param>
public static BigFraction operator %(BigFraction r, BigInteger mod)
{
var modmulden = r.Denominator * mod;
var remainder = r.Numerator % modmulden;
var answer = new BigFraction(remainder, r.Denominator);
return answer;
}*/

// Operator >
/// <summary>
/// Operator "greater than"
/// </summary>
Expand All @@ -221,7 +207,8 @@ public BigFraction(int i)
{
var r1compare = r1.Numerator * r2.Denominator;
var r2compare = r2.Numerator * r1.Denominator;
return r1compare.CompareTo(r2compare) == 1;
var res = r1compare.CompareTo(r2compare);
return r1.Denominator.Sign == r2.Denominator.Sign ? res > 0 : res < 0;
}

/// <summary>
Expand All @@ -233,7 +220,8 @@ public BigFraction(int i)
{
var r1compare = r1.Numerator;
var r2compare = r2 * r1.Denominator;
return r1compare.CompareTo(r2compare) == 1;
var res = r1compare.CompareTo(r2compare);
return r1.Denominator.Sign == 1 ? res > 0 : res < 0;
}

/// <summary>
Expand All @@ -245,7 +233,8 @@ public BigFraction(int i)
{
var r1compare = r1 * r2.Denominator;
var r2compare = r2.Numerator;
return r1compare.CompareTo(r2compare) == 1;
var res = r1compare.CompareTo(r2compare);
return r2.Denominator.Sign == 1 ? res > 0 : res < 0;
}

// Operator <
Expand All @@ -258,7 +247,8 @@ public BigFraction(int i)
{
var r1compare = r1.Numerator * r2.Denominator;
var r2compare = r2.Numerator * r1.Denominator;
return r1compare.CompareTo(r2compare) == -1;
var res = r1compare.CompareTo(r2compare);
return r1.Denominator.Sign == r2.Denominator.Sign ? res < 0 : res > 0;
}

/// <summary>
Expand All @@ -270,7 +260,8 @@ public BigFraction(int i)
{
var r1compare = r1.Numerator;
var r2compare = r2 * r1.Denominator;
return r1compare.CompareTo(r2compare) == -1;
var res = r1compare.CompareTo(r2compare);
return r1.Denominator.Sign == 1 ? res < 0 : res > 0;
}

/// <summary>
Expand All @@ -282,7 +273,8 @@ public BigFraction(int i)
{
var r1compare = r1 * r2.Denominator;
var r2compare = r2.Numerator;
return r1compare.CompareTo(r2compare) == -1;
var res = r1compare.CompareTo(r2compare);
return r2.Denominator.Sign == 1 ? res < 0 : res > 0;
}

// Operator ==
Expand Down Expand Up @@ -369,7 +361,8 @@ public BigFraction(int i)
{
var r1compare = r1.Numerator * r2.Denominator;
var r2compare = r2.Numerator * r1.Denominator;
return r1compare.CompareTo(r2compare) == -1 || r1compare.CompareTo(r2compare) == 0;
var res = r1compare.CompareTo(r2compare);
return r1.Denominator.Sign == r2.Denominator.Sign ? res <= 0 : res >= 0;
}

/// <summary>
Expand All @@ -381,7 +374,8 @@ public BigFraction(int i)
{
var r1compare = r1.Numerator;
var r2compare = r2 * r1.Denominator;
return r1compare.CompareTo(r2compare) == -1 || r1compare.CompareTo(r2compare) == 0;
var res = r1compare.CompareTo(r2compare);
return r1.Denominator.Sign == 1 ? res <= 0 : res >= 0;
}

/// <summary>
Expand All @@ -393,7 +387,8 @@ public BigFraction(int i)
{
var r1compare = r1 * r2.Denominator;
var r2compare = r2.Numerator;
return r1compare.CompareTo(r2compare) == -1 || r1compare.CompareTo(r2compare) == 0;
var res = r1compare.CompareTo(r2compare);
return r2.Denominator.Sign == 1 ? res <= 0 : res >= 0;
}

// Operator >=
Expand All @@ -406,7 +401,8 @@ public BigFraction(int i)
{
var r1compare = r1.Numerator * r2.Denominator;
var r2compare = r2.Numerator * r1.Denominator;
return r1compare.CompareTo(r2compare) == 1 || r1compare.CompareTo(r2compare) == 0;
var res = r1compare.CompareTo(r2compare);
return r1.Denominator.Sign == r2.Denominator.Sign ? res >= 0 : res <= 0;
}

/// <summary>
Expand All @@ -418,7 +414,8 @@ public BigFraction(int i)
{
var r1compare = r1.Numerator;
var r2compare = r2 * r1.Denominator;
return r1compare.CompareTo(r2compare) == 1 || r1compare.CompareTo(r2compare) == 0;
var res = r1compare.CompareTo(r2compare);
return r1.Denominator.Sign == 1 ? res >= 0 : res <= 0;
}

/// <summary>
Expand All @@ -430,7 +427,8 @@ public BigFraction(int i)
{
var r1compare = r1 * r2.Denominator;
var r2compare = r2.Numerator;
return r1compare.CompareTo(r2compare) == 1 || r1compare.CompareTo(r2compare) == 0;
var res = r1compare.CompareTo(r2compare);
return r2.Denominator.Sign == 1 ? res >= 0 : res <= 0;
}

// Operator -
Expand Down Expand Up @@ -532,6 +530,7 @@ public BigFraction(int i)
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
public static BigFraction operator /(BigInteger a, BigFraction b) => new BigFraction(a * b.Denominator, b.Numerator);
#endregion

// Override Equals
/// <summary>
Expand Down Expand Up @@ -689,5 +688,7 @@ public static BigFraction FromDouble(double value, double accuracy)
}
}
}

public static BigFraction Abs(BigFraction value) => value < 0 ? -value : value;
}
}
Loading

0 comments on commit 4654924

Please sign in to comment.