Conversation
| @@ -165,7 +152,7 @@ | |||
| } | |||
|
|
|||
| var channelcolor = | |||
| (int)Math.Round(connectedAccount.Count(x=>x.ChannelId == server.ServerId) / (server.ConnectedAccountLimit + 1) * 20D) | |||
| (int)Math.Round(connectedAccount.Count(x => x.ChannelId == server.ServerId) / (server.ConnectedAccountLimit + 1) * 20D) | |||
Check failure
Code scanning / CodeQL
Possible loss of precision Error
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI about 1 month ago
In general, to avoid loss of precision in this context, one of the operands of the division should be explicitly converted to a floating‑point type before the division so that C# performs floating‑point division instead of integer division. The result will then be a double, which can be safely multiplied by 20D, and Math.Round will round the result as intended.
The best, minimal change here is to cast either the numerator or the denominator to double before the division in line 155. For example, change:
(int)Math.Round(connectedAccount.Count(x => x.ChannelId == server.ServerId) / (server.ConnectedAccountLimit + 1) * 20D)to:
(int)Math.Round(
(double)connectedAccount.Count(x => x.ChannelId == server.ServerId)
/ (server.ConnectedAccountLimit + 1) * 20D)or equivalently cast the denominator. This preserves the overall structure and semantics (still rounded and cast to int afterward) while ensuring the proportion is computed with fractional precision. No new methods or imports are required; System.Math is already available via using System;.
| @@ -152,7 +152,7 @@ | ||
| } | ||
|
|
||
| var channelcolor = | ||
| (int)Math.Round(connectedAccount.Count(x => x.ChannelId == server.ServerId) / (server.ConnectedAccountLimit + 1) * 20D) | ||
| (int)Math.Round((double)connectedAccount.Count(x => x.ChannelId == server.ServerId) / (server.ConnectedAccountLimit + 1) * 20D) | ||
| + 1; | ||
| subpacket.Add(new NsTeStSubPacket | ||
| { |
Check notice
Code scanning / CodeQL
Inefficient use of ContainsKey Note
No description provided.