Coder Social home page Coder Social logo

bigrational's People

Contributors

adamwhitehat avatar c272 avatar stewienj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

bigrational's Issues

BUG: Numbers between ]-1, 0[ have a wrong sign.

Description
numbers like -0.0001 have a whole part of 0 and a fractional part of 1 /10000, but whole and fractional are positive (sign = 1).
So nothing stores the sign of the complete number.

Error/Exception Message
If you try the following in RoslynPad:

#r "nuget: ExtendedNumerics.BigRational, 2023.162.546"

using ExtendedNumerics;
var value = new BigRational(0.000001);
value.Dump();
(-value).Dump();

you'll get:
image

Expected Behavior
(-value).Sign should be -1 and value.Sign should be 1
And value.Whole.Sign can remain 0, but then value.Fraction.Sign should become -1 in the negative case.
Actual Behavior
Both signs are 0 due to the sign being taken entirely from the whole part.

Notes
Explicitly creating a negative fraction results in Sign = 0

new BigRational(0, -1, 1000000).Dump();

Because the sign of the Fractional isn't even preserved when negating the entire number.

BUG: Arithmetic bug with negative numbers

Description
There's an arithmetic bug with negative numbers, e.g. add -7 and -6 together and halve to find the mid point. I get -5.5, not the expected -6.5.

Steps To Reproduce

[TestMethod, TestCategory("Arithmetic")]
public void TestMidPoint()
{
	var low = (BigRational)(-7);
	var high = (BigRational)(-6);

	// Take the mid point of the above 2 numbers
	var mid = (low + high) / (BigRational)2;

	Assert.IsTrue(mid > low);
	Assert.IsTrue(mid < high);
}

Took me a while to realize I wasn't going crazy.

BUG: Sqrt result in diff from calculator

Description
A clear and concise description of what the bug is.

[Test]
public void BigRationTest()
{
    var a = new BigRational(BigInteger.Parse("160000000000"));
    var b = new BigRational(BigInteger.Parse("249"), new Fraction(0.999));
    var c = BigRational.Divide(a, b);
    Console.WriteLine((decimal)c);
    Console.WriteLine(BigRational.Sqrt(c));
    // this result in 25348.917835671342685370741483M
    var bigRational = (decimal)BigRational.Sqrt(c);
    // this result in 25298.271877941387183714739055M, it is the same as the result from calculator from PC or phone
    var cc = DecimalMath.DecimalEx.Sqrt(160000000000m / 249.999m);
    // failed
    cc.Should().Be(bigRational);
}

BUG: NthRoot Bug?

Description
BigRational.NthRoot(new BigRational(50), 3);

returns exactly 3. I would expect that since this function takes a BigRational and not a BigInteger, that cutting off the decimal is a mistake? The FractionalPart shows a 0 Numerator and casting to any other floating point type is still an integer.

Error/Exception Message
Silent miscalculation

Expected Behavior
3.684031... or something indicating only whole numbers are supported.

Actual Behavior
3

Steps To Reproduce
Evaluate this
BigRational.NthRoot(new BigRational(50), 3));

Additional context/Comments/Notes/Rants
Thanks for creating this library. I hope I'm not missing something obvious :)

BUG: Parse(Double input) fails , if CurrentCulture has NumberFormat with 'comma' instead of 'dot'

Description
BigDecimal Parse(Double input) => Parse(input.ToString(CultureInfo.CurrentCulture)); uses CurrentCulture to convert double into string, while next step uses BigDecimalNumberFormatInfo, which is InvariantCulture: public static BigDecimal Parse(String input) { return Parse(input, BigDecimalNumberFormatInfo); }

So, If CurrentCulture's NumberFormat is not similar to InvarantCulture, Parse + all releated tests will fail.
Example: new CultureInfo("ru-RU") has 'comma' for decimal point.

Same issue as BigDecimal/issues/16 reported by Degot. This issue exists in both these libraries.

Question: In Fraction constructor, why do you convert BigInteger to byte array and back again?

In fraction.cs you have this constructor:

public Fraction(BigInteger numerator, BigInteger denominator)
{
	Numerator = new BigInteger(numerator.ToByteArray());
	Denominator = new BigInteger(denominator.ToByteArray());
}

I was comparing your implementation with the old Microsoft BigRational, and for my usage pattern the old (but buggy) Microsoft one was up to 10x faster, and the reason was the above constructor.

BigInteger is a struct, so it will be copied by value. If there's an issue that can be fixed by the above code then there is a big problem at play somewhere. I changed the code to the following and it still passes all your unit tests and at the same time speeds up my code 10 fold.

public Fraction(BigInteger numerator, BigInteger denominator)
{
	Numerator = numerator;
	Denominator = denominator;
}

Could you please enlighten me about this issue. Here's some code that I was using in my comparison test:

private readonly int _iterations = 1_000;
private readonly int _count = 1_000;

[TestMethod]
public void TestIteratingOnInsertionIndexDownDirectionAlternate2()
{
    List<ExtendedNumerics.BigRational> testSetLower = new List<ExtendedNumerics.BigRational>(_count);
    List<ExtendedNumerics.BigRational> testSetUpper = new List<ExtendedNumerics.BigRational>(_count);
    for (int i = 0; i < _count; ++i)
    {
        testSetLower.Add((ExtendedNumerics.BigRational)i);
        testSetUpper.Add((ExtendedNumerics.BigRational)(i + 1));
    }
    for (int iteration = 0; iteration < _iterations; ++iteration)
    {
        for (int i = 0; i < _count; ++i)
        {
            // Get pairs of number, take half way between them, and check it
            // is less than the high end and greater than the lower end
            ExtendedNumerics.BigRational newValue = (testSetLower[i] + testSetUpper[i]) / (ExtendedNumerics.BigRational)2;
            Assert.IsTrue(newValue > testSetLower[i]);
            Assert.IsTrue(newValue < testSetUpper[i]);
            testSetUpper[i] = newValue;
        }
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.