【发布时间】:2020-02-18 18:05:49
【问题描述】:
我似乎在使用Int64 var 的算术运算中找不到准确的结果。我需要对两个 Int64 值进行平均。
static void Main(string[] args)
{
Int64 avg
int testCase = 0;
testCase = int.Parse(Console.ReadLine());
for (int i = 0; i < testCase; i++)
{
string[] str = Console.ReadLine().Split();
avg = ( ( Int64.Parse( str[0] ) + Int64.Parse( str[1] ) ) / 2 );
Console.WriteLine(avg);
}
}
输入:str[0] == 9082296538332151448str[1] == 5601337573664003844
输出:-1881554980856698162
为什么它会导致负数。试图找到平均值
【问题讨论】:
-
9082296538332151448 + 5601337573664003844 > Int64.MaxValue,你有整数溢出。 -
你能具体说明你提供的输入吗?如果我只提供这两个数字之一,它会抛出 OverflowException,因为 9082296538332151448 无法放入 int。此外,代码不会编译,因为您在
Int64 avg之后缺少分号,这让我担心问题中的代码不是您的真实代码。你能发布一个不依赖用户输入而是硬编码输入的minimal reproducible example 吗? -
注意
(9082296538332151448m + 5601337573664003844m) / 2 == 7341817055998077646;Decimal对此有足够的精度。但是请注意,即使Decimal的精度也不是无限的——更多您可以切换到BigInteger。 -
分号丢失必须在发帖时将其删除。对不起这个错误。除此之外,除非使用 Int64 ,否则您在 Int 中尝试的数字将不适合。