From 12ae678e86d4baee8bad5a8169a0b35929480037 Mon Sep 17 00:00:00 2001 From: That-One-Nerd Date: Mon, 24 Apr 2023 07:15:26 -0400 Subject: [PATCH] some adding to the big integer (DOESNT WORK) --- Nerd_STF/Mathematics/BigInteger.cs | 74 +++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 6 deletions(-) diff --git a/Nerd_STF/Mathematics/BigInteger.cs b/Nerd_STF/Mathematics/BigInteger.cs index 638ce0e..78e3517 100644 --- a/Nerd_STF/Mathematics/BigInteger.cs +++ b/Nerd_STF/Mathematics/BigInteger.cs @@ -1,6 +1,4 @@ -using System.Numerics; - -namespace Nerd_STF.Mathematics; +namespace Nerd_STF.Mathematics; public readonly struct BigInteger/* : IAbsolute, IAverage, IClamp, IComparable, IComparable, IComparable, IComparable, IComparable, @@ -15,6 +13,10 @@ public readonly struct BigInteger/* : IAbsolute, IAverage new(false, new byte[] { 1 }); public static BigInteger Zero => new(false, Array.Empty()); + public bool IsNegative => p_sign; + public bool IsPositive => !p_sign; + + public int Length => p_data.Length; public int Sign => p_sign ? -1 : 1; private readonly byte[] p_data; @@ -111,10 +113,9 @@ public readonly struct BigInteger/* : IAbsolute, IAverage CompareTo((BigInteger)other); public int CompareTo(BigInteger other) { - int compare = p_sign.CompareTo(other.p_sign); - if (compare != 0) return compare; + if (p_sign ^ other.p_sign) return p_sign ? -1 : 1; - compare = p_data.Length.CompareTo(other.p_data); + int compare = p_data.Length.CompareTo(other.p_data.Length); if (compare != 0) return compare; for (int i = p_data.Length - 1; i >= 0; i--) @@ -168,6 +169,22 @@ public readonly struct BigInteger/* : IAbsolute, IAverage= 0; i--) + { + compare = p_data[i].CompareTo(other.p_data[i]); + if (compare != 0) return compare; + } + + return 0; + } private byte[] TrimmedData() { int start = 0; @@ -175,6 +192,15 @@ public readonly struct BigInteger/* : IAbsolute, IAverage new(false, val.p_data); + public static BigInteger Max(params BigInteger[] vals) + { + if (vals.Length < 1) return Zero; + BigInteger val = vals[0]; + foreach (BigInteger b in vals) val = b > val ? b : val; + return val; + } + public static bool operator ==(BigInteger a, BigInteger b) => a.Equals(b); public static bool operator !=(BigInteger a, BigInteger b) => !a.Equals(b); public static bool operator >(BigInteger a, BigInteger b) => a.CompareTo(b) > 0; @@ -206,4 +232,40 @@ public readonly struct BigInteger/* : IAbsolute, IAverage val.ToInt32(); public static explicit operator double(BigInteger val) => val.ToInt64(); public static explicit operator decimal(BigInteger val) => val.ToInt64(); + + public static BigInteger operator +(BigInteger a, BigInteger b) + { + // Add padding to integer data to keep lists the same size. + int desiredLength = Mathf.Max(a.Length, b.Length) + 1; + + List dataA = new(a.p_data), + dataB = new(b.p_data), + dataC = new(); + + while (dataA.Count < desiredLength) dataA.Add(0x00); + while (dataB.Count < desiredLength) dataB.Add(0x00); + + // Add the data of A and B from left to right. Roll over remainders. + sbyte remainder = 0; + byte result; + for (int i = 0; i < desiredLength - 1; i++) + { + short sum = (short)(dataA[i] * a.Sign + dataB[i] * b.Sign + remainder); + remainder = (sbyte)(sum >> 8); + result = (byte)(sum & 0xFF); + dataC.Add(result); + } + dataC.Add((byte)Math.Abs(remainder)); // Add the remainder for the last addition. + + bool sign; + if (!(a.p_sign ^ b.p_sign)) sign = a.p_sign; + else + { + bool aBigger = Absolute(a) > Absolute(b); + if (aBigger) sign = a.p_sign; + else sign = b.p_sign; + } + + return new(sign, dataC.ToArray()); + } }