【发布时间】:2021-11-19 04:05:10
【问题描述】:
我有一个程序可以下载一些讲座视频以供离线观看。我使用下面的代码来下载和计算百分比。大约 32-33% 的计算百分比总是负数,但以一种奇怪的方式 (33 -> -33 -> -32 -> -31 -> -30 etc...) 直到达到又是33。然后下载完成。我什至手动计算。调试输出也在代码下方。
这种情况一直发生,所以我肯定做错了什么,但在哪里?
private async Task CopyStream(
Lecture lecture,
Stream source,
Stream destination,
int sourceLength,
CancellationToken token,
int bufferSize = (16 * 1024))
{
var buffer = new byte[bufferSize];
if (sourceLength <= 0) return;
var totalBytesCopied = 0;
var bytesRead = -1;
while (bytesRead != 0)
{
bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, token);
if (bytesRead == 0) break;
await destination.WriteAsync(buffer, 0, buffer.Length, token);
totalBytesCopied += bytesRead;
var progress = (int)Math.Round((double)(100 * totalBytesCopied) / sourceLength);
Debug.WriteLine(
$"Lecture={lecture.Title}, " +
$"sourceLength={sourceLength}, " +
$"totalBytesCopied={totalBytesCopied}, " +
$"bytesRead={bytesRead}, progress={progress}");
RaiseLectureDownloadProgressChangedEvent(null,
new LectureDownloadProgressChangedEventArgs(lecture, progress));
}
}
Lecture=Lecture1, sourceLength=65777753, totalBytesCopied=21404088, bytesRead=16384, progress=33
Lecture=Lecture1, sourceLength=65777753, totalBytesCopied=21420472, bytesRead=16384, progress=33
Lecture=Lecture1, sourceLength=65777753, totalBytesCopied=21436856, bytesRead=16384, progress=33
Lecture=Lecture1, sourceLength=65777753, totalBytesCopied=21453240, bytesRead=16384, progress=33
Lecture=Lecture1, sourceLength=65777753, totalBytesCopied=21469624, bytesRead=16384, progress=33
Lecture=Lecture1, sourceLength=65777753, totalBytesCopied=21486008, bytesRead=16384, progress=-33
Lecture=Lecture1, sourceLength=65777753, totalBytesCopied=21502392, bytesRead=16384, progress=-33
Lecture=Lecture1, sourceLength=65777753, totalBytesCopied=21518776, bytesRead=16384, progress=-33
Lecture=Lecture1, sourceLength=65777753, totalBytesCopied=21535160, bytesRead=16384, progress=-33
Lecture=Lecture1, sourceLength=65777753, totalBytesCopied=21551544, bytesRead=16384, progress=-33
Lecture=Lecture1, sourceLength=65777753, totalBytesCopied=21567928, bytesRead=16384, progress=-33
Lecture=Lecture1, sourceLength=65777753, totalBytesCopied=21584312, bytesRead=16384, progress=-32
Lecture=Lecture1, sourceLength=65777753, totalBytesCopied=21600696, bytesRead=16384, progress=-32
【问题讨论】:
-
我的猜测是
100 * totalBytesCopied溢出了int。将变量键入long,或使用100D * ...在进行乘法运算时将其强制为浮点数。 -
非常感谢,使用 100D 有效。但是为什么这么少的数字会发生溢出呢?
-
@YunusEmreÖRCÜN 什么是
100 * 21486008?int只能达到int.MaxValue,即2**31-1或2147483647。 -
@JeppeStigNielsen 谢谢你的解释。
标签: c# percentage