gpt4 book ai didi

c# - c#中的数学模数

转载 作者:IT王子 更新时间:2023-10-29 04:19:04 32 4
gpt4 key购买 nike

C# 中是否有用于计算数字的数学模数的库函数 - 我的意思是指负整数对正整数求模应该产生正结果。

编辑以提供示例:

-5 模 3 应该返回 1

最佳答案

试试 (a % b) * Math.Sign(a)

试试这个;它工作正常。

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

关于c# - c#中的数学模数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2691025/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com