在C#中的数学模 [英] Mathematical modulus in c#

查看:122
本文介绍了在C#中的数学模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有在c#为一个数的数学模量的库函数 - 该我特别意味着一个负整数模的正整数应产生一个肯定的结果

Is there a library function in c# for the mathematical modulus of a number - by this I specifically mean that a negative integer modulo a positive integer should yield a positive result.

编辑,以提供一个例子:

edited to provide an example:

-5模3应该返回1

-5 modulo 3 should return 1

推荐答案

<击>尝试(A%b)* Math.Sign(一)

试试这个;它工作正常。

Try this; it works correctly.

static int MathMod(int a, int b) {
    return (Math.Abs(a * b) + a) % b;
}

这篇关于在C#中的数学模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆