[英]C# modulus equation return wrong value
var newPosition = (position - key) % alphabet.Length;
當position為66時,鍵為7964,並且Alphabet.length = 91時,newPosition以某種方式為-72,即使應該為19。為什么?
請注意,%實際上不是模數,而是余數
static decimal modulus(decimal a, decimal b)
{
return a - b * Math.Floor(a / b);
}
...
Console.WriteLine( nfmod(66-7964 , 91));
輸出繼電器
19
使用/用於模塊
var newPosition = (position - key) / alphabet.Length;
((position - key) % alphabet.Length + alphabet.Length) % alphabet.Length
絕招
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.