using System; using System.Collections.Generic; using System.Reflection.Emit; using System.Text; using Sunweaver.Commands.Attributes; using Sunweaver.VM; namespace Sunweaver.Commands { [BasePair("Multiplication")] public class mul : Abstracts.BinaryOperation { public override void Operation(long A, long B) { // Floating point arithmetic stores the high order digits. Integer multiplication stores the low order // digits. Since our 15 digit numbers can fit entirely in the double's 53 bit mantissa, we can abuse both // floating point and integer math to find the final modulo multiplication. // // See https://en.wikipedia.org/wiki/Modular_arithmetic#Example_implementations long a = Math.Abs(A); long b = Math.Abs(B); var m = IntStack.Max + 1; double x = a; Int64 c = (Int64)(x * (double)b / (double)m); Int64 r = (a * b - c * m) % m; Int64 result = r < 0 ? r + m : r; this.VM.Stack.Push(A < 0 ^ B < 0 ? -result : result); } public override void CompileOperation(ILGenerator il, Locals locals) { throw new NotImplementedException(); } } }