From 80b78c4bdba1c7248bddfa10568b28a85c0069ce Mon Sep 17 00:00:00 2001 From: That-One-Nerd Date: Sun, 2 Apr 2023 17:16:11 -0400 Subject: [PATCH] some small biginteger progress. more will come in the future. i want to make a small tweak to the rational type first. --- Nerd_STF/Mathematics/BigInteger.cs | 100 +++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Nerd_STF/Mathematics/BigInteger.cs diff --git a/Nerd_STF/Mathematics/BigInteger.cs b/Nerd_STF/Mathematics/BigInteger.cs new file mode 100644 index 0000000..ec95916 --- /dev/null +++ b/Nerd_STF/Mathematics/BigInteger.cs @@ -0,0 +1,100 @@ +using System.Numerics; + +namespace Nerd_STF.Mathematics; + +public readonly struct BigInteger/* : IAbsolute, IAverage, IClamp, IDivide, + ILerp, IMathOperators, IMax, IMedian, IMin, + IPresets1d, IProduct, ISubtract, ISum*/ +{ + public static BigInteger One => new(false, new byte[] { 1 }); + public static BigInteger Zero => new(false, Array.Empty()); + + public int Sign => p_sign ? -1 : 1; + + private readonly byte[] p_data; + private readonly bool p_sign; + + private BigInteger(bool sign, byte[] data) + { + p_data = data; + p_sign = sign; + } + public BigInteger(byte val) + { + p_sign = false; + p_data = new byte[] { val }; + } + public BigInteger(sbyte val) + { + p_sign = Math.Sign(val) < 0; + p_data = new byte[] { (byte)Math.Abs(val) }; + } + public BigInteger(ushort val) + { + p_sign = false; + p_data = BitConverter.GetBytes(val); + } + public BigInteger(short val) + { + p_sign = Math.Sign(val) < 0; + p_data = BitConverter.GetBytes(Math.Abs(val)); + } + public BigInteger(uint val) + { + p_sign = false; + p_data = BitConverter.GetBytes(val); + } + public BigInteger(int val) + { + p_sign = Math.Sign(val) < 0; + p_data = BitConverter.GetBytes(Math.Abs(val)); + } + public BigInteger(ulong val) + { + p_sign = false; + p_data = BitConverter.GetBytes(val); + } + public BigInteger(long val) + { + p_sign = Math.Sign(val) < 0; + p_data = BitConverter.GetBytes(Math.Abs(val)); + } + + public byte[] GetAbsoluteData() => p_data; + + public sbyte ToInt8() + { + byte[] data = GetDataOfSize(sizeof(sbyte)); + sbyte absNum = (sbyte)data[0]; + return p_sign ? (sbyte)-absNum : absNum; + } + public byte ToUInt8() => (byte)ToInt8(); + public short ToInt16() + { + byte[] data = GetDataOfSize(sizeof(short)); + short absNum = BitConverter.ToInt16(data); + return p_sign ? (short)-absNum : absNum; + } + public ushort ToUInt16() => (ushort)ToInt16(); + public int ToInt32() + { + byte[] data = GetDataOfSize(sizeof(int)); + int absNum = BitConverter.ToInt32(data); + return p_sign ? -absNum : absNum; + } + public uint ToUInt32() => (uint)ToInt32(); + public long ToInt64() + { + byte[] data = GetDataOfSize(sizeof(long)); + long absNum = BitConverter.ToInt64(data); + return p_sign ? -absNum : absNum; + } + public ulong ToUInt64() => (ulong)ToInt64(); + + private byte[] GetDataOfSize(int bytes) + { + byte[] data = new byte[bytes]; + Array.Copy(p_data, data, Mathf.Min(p_data.Length, bytes)); + return data; + } +}