c# - 是否有任何功能或正确的方法可以将十进制数解析为指数
问题描述
我正在尝试将C#类型解析为 我自己的类型,该类型具有且只有。我试过这个: decimal
Decimal
significant
exponent
public static MyType.Decimal FromDecimal(decimal decimalValue)
{
decimal tempValue = decimalValue;
int exponent = 0;
while ((long) (tempValue) < decimalValue)
{
tempValue *= 10;
decimalValue *= 10;
exponent--;
}
return new MyType.Decimal()
{
Significand = (long)tempValue,
Exponent = exponent
};
}
public class Decimal
{
public long Significand;
public int Exponent;
}
但在我看来,它会很慢。
解决方案
您可以尝试decimal.GetBits(),例如
public static MyType.Decimal FromDecimal(decimal decimalValue) {
int[] parts = decimal.GetBits(decimalValue);
int sign = 1 - 2 * (parts[3] >> 31 & 1);
long mantissa = ((((long)parts[1]) << 32) | (((long)parts[0]))) * sign;
int exponent = -((parts[3] >> 16) & 0xFF);
return MyType.Decimal() {
Significand = mantissa,
Exponent = exponent
};
}
但要小心:decimal
使用96 位尾数(请注意,我们忽略了它),这就是为什么太短(仅64 位)parts[2]
long Significand
long
演示:
decimal[] tests = new decimal[] {
0m,
10m,
100m,
0.01m,
123e-1m,
-3m,
123456789e4m,
123456789e-4m,
1234567890123456m,
1234567890123456e4m // 12345678901234560000 > long.MaxValue (9223372036854775807)
};
string report = string.Join(Environment.NewLine, tests
.Select(test => {
int[] parts = decimal.GetBits(test);
int sign = 1 - 2 * (parts[3] >> 31 & 1);
long mantissa = ((((long)parts[1]) << 32) | (((long)parts[0]))) * sign;
int exponent = -((parts[3] >> 16) & 0xFF);
return $"{test,20} == {mantissa}e{exponent}";
}));
结果:
0 == 0e0
10 == 10e0
100 == 100e0
0.01 == 1e-2
12.3 == 123e-1
-3 == -3e0
1234567890000 == 1234567890000e0
12345.6789 == 123456789e-4
1234567890123456 == 1234567890123456e0
12345678901234560000 == -350295040e0 -- mantissa is 96 bit, doesn't fit 64-bit long
推荐阅读
- c - 如何从字符串中删除第一个条目
- python - 将 pandas groupby.apply 的输出保存到新列
- css - 如何修复网格中内容旁边的项目,使其始终保持在视图中
- c# - Unity3D:值不能为空。参数名称:obj:Microsoft.MixedReality.SpatialAlignment.SpatialAnchorsCoordinateService.RequestSessionStart()
- c# - 从基本 DBContext 继承时如何使用 IdentityDBContex
- javascript - 如何使用 Firebase 的用户帐户将数据发送到数据库?
- r - 在 R 中调用一个导致不同输出的函数
- java - 如何在新的(未弃用的)SSLSocketFactory 中加载私钥密码?
- python - 我在从 Django 中的关联模型的父模型中获取数据时遇到困难
- tensorflow - 2类分类器能否输出一个类的可能性作为分数