Created
December 1, 2023 03:43
-
-
Save lightjiao/2253085bcf268957e4e8476834b53ac9 to your computer and use it in GitHub Desktop.
浮点数乘以大数和它的四舍五入
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// - Round() 函数是四舍五入的实现,所以对于计算机来说,0.9995 可能实际上是0.999499999,从而导致四舍五入的结果是 0.999 而不是 1.0,所以这些的函数才都加上了 0.001 来保证四舍五入的结果符合预期 | |
/// - Int16 最大值有三万多,可以根据实际情况是否选用 int16, 可以减小网络传输? | |
/// - 不同的数据对精度要求不同,所以有的乘以100,有的乘以1000 | |
/// </summary> | |
public static class PackValueUtil | |
{ | |
private const int Float_Pack_Factor = 100; | |
private const float Float_Pack_Factor_Err = 1.0f / (Float_Pack_Factor * 10); // = 0.001f | |
private const int Float_Pack_Pos_Factor = 1000; | |
private const float Float_Pack_Pos_Factor_Err = 1f / (Float_Pack_Pos_Factor * 10); // = 0.0001f | |
public static Int32 PackFloat(float v) | |
{ | |
var a = (double)((v + Float_Pack_Factor_Err) * Float_Pack_Factor); | |
return (int)Math.Round(a); | |
} | |
public static Int32 PackVectorFloat(float v) | |
{ | |
var a = (double)((v + Float_Pack_Pos_Factor_Err) * Float_Pack_Pos_Factor); | |
return (int)Math.Round(a); | |
} | |
public static void Test() | |
{ | |
var floatList = new List<float> { 1.1f, 9.99f, 9.999f, 0.9995f, 0.995f }; | |
foreach (var item in floatList) | |
{ | |
Console.WriteLine($"{item} --> {PackVectorFloat(item)}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment