Add Mod function

See kaitai-io/kaitai_struct#33
This commit is contained in:
Daniel Walder 2016-10-08 18:19:22 +10:00
parent 9104c76b4c
commit 5767488867
1 changed files with 23 additions and 0 deletions

View File

@ -508,5 +508,28 @@ namespace Kaitai
}
#endregion
#region Misc utility methods
/// <summary>
/// Performs modulo operation between two integers.
/// </summary>
/// <remarks>
/// This method is required because C# lacks a "true" modulo
/// operator, the % operator rather being the "remainder"
/// operator. We want mod operations to always be positive.
/// </remarks>
/// <param name="a">The value to be divided</param>
/// <param name="b">The value to divide by. Must be greater than zero.</param>
/// <returns>The result of the modulo opertion. Will always be positive.</returns>
public static int Mod(int a, int b)
{
if (b <= 0) throw new ArgumentException("Divisor of mod operation must be greater than zero.", nameof(b));
var r = a % b;
if (r < 0) r += b;
return r;
}
#endregion
}
}