亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

這是 Partitioner.Create(int fromInclusive)

這是 Partitioner.Create(int fromInclusive)

C#
開心每一天1111 2023-07-22 16:32:42
我想我發現了該方法中的一個錯誤Partitioner.Create(int fromInclusive, int toExclusive)。rangeSize當范圍超過 時,它會計算未提供的參數 的負值Int32.MaxValue。這是演示該問題的代碼示例:var partitioner = Partitioner.Create(-1, Int32.MaxValue);var partitions = partitioner.GetPartitions(1);foreach (var partition in partitions){? ? while (partition.MoveNext())? ? {? ? ? ? var range = partition.Current;? ? ? ? Console.WriteLine($"Range: {range.Item1,11} => {range.Item2,11}");? ? }}輸出:Range:? ? ? ? ? -1 =>? -178956971Range:? -178956971 =>? -357913941Range:? -357913941 =>? -536870911Range:? -536870911 =>? -715827881Range:? -715827881 =>? -894784851Range:? -894784851 => -1073741821Range: -1073741821 => -1252698791Range: -1252698791 => -1431655761Range: -1431655761 => -1610612731Range: -1610612731 => -1789569701Range: -1789569701 => -1968526671Range: -1968526671 => -2147483641Range: -2147483641 =>? 2147483647因此,循環拋出這些范圍for (int i = range.Item1; i < range.Item2; i++)將導致除最后一個范圍之外的所有范圍的零循環,這將有效地循環該Int32類型的整個范圍。有一個特殊情況。下面的分區器計算 arangeSize為 1。Partitioner.Create(Int32.MinValue, Int32.MaxValue);下面是該方法的源代碼:public static OrderablePartitioner<Tuple<int, int>> Create(? ? int fromInclusive, int toExclusive){? ? // How many chunks do we want to divide the range into?? If this is 1, then the? ? // answer is "one chunk per core".? Generally, though, you'll achieve better? ? // load balancing on a busy system if you make it higher than 1.? ? int coreOversubscriptionRate = 3;? ? if (toExclusive <= fromInclusive) throw new ArgumentOutOfRangeException("toExclusive");? ? int rangeSize = (toExclusive - fromInclusive) /? ? ? ? (PlatformHelper.ProcessorCount * coreOversubscriptionRate);? ? if (rangeSize == 0) rangeSize = 1;? ? return Partitioner.Create(CreateRanges(fromInclusive, toExclusive, rangeSize),? ? ? ? EnumerablePartitionerOptions.NoBuffering); // chunk one range at a time}看來減法時發生了整數溢出toExclusive - fromInclusive。如果這確實是一個錯誤,在 .NET Framework 的未來版本中修復該錯誤之前,您建議采用什么解決方法?
查看完整描述

2 回答

?
動漫人物

TA貢獻1815條經驗 獲得超10個贊

看來整數溢出發生在除法 toExclusive - fromInclusive 上。

是的,看起來這是一個錯誤。

在 .NET Framework 的未來版本中修復該問題之前,您建議采用什么解決方法?

我建議將您的輸入轉換為long并調用該版本。它仍然存在類似的溢出錯誤,但如果您的原始輸入是,int您絕對不會遇到溢出情況long。


查看完整回答
反對 回復 2023-07-22
?
喵喵時光機

TA貢獻1846條經驗 獲得超7個贊

這是一個錯誤,我已記錄了一個問題,以便在 .NET Core 的未來版本中修復此問題https://github.com/dotnet/corefx/issues/40201

并且建議的解決方法也是正確的。只需將輸入參數轉換為 long 即可。


查看完整回答
反對 回復 2023-07-22
  • 2 回答
  • 0 關注
  • 189 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號