gpt4 book ai didi

.net - 有没有办法得到小数的 "significant figures"?

转载 作者:行者123 更新时间:2023-12-01 22:52:04 25 4
gpt4 key购买 nike

更新

好的,经过一番调查,这在很大程度上要感谢乔恩和汉斯提供的有用答案,这就是我能够整理出来的内容。到目前为止,我认为它似乎运作良好。当然,我不会用我的生命去赌它的完全正确性。

public static int GetSignificantDigitCount(this decimal value)
{
/* So, the decimal type is basically represented as a fraction of two
* integers: a numerator that can be anything, and a denominator that is
* some power of 10.
*
* For example, the following numbers are represented by
* the corresponding fractions:
*
* VALUE NUMERATOR DENOMINATOR
* 1 1 1
* 1.0 10 10
* 1.012 1012 1000
* 0.04 4 100
* 12.01 1201 100
*
* So basically, if the magnitude is greater than or equal to one,
* the number of digits is the number of digits in the numerator.
* If it's less than one, the number of digits is the number of digits
* in the denominator.
*/

int[] bits = decimal.GetBits(value);

if (value >= 1M || value <= -1M)
{
int highPart = bits[2];
int middlePart = bits[1];
int lowPart = bits[0];

decimal num = new decimal(lowPart, middlePart, highPart, false, 0);

int exponent = (int)Math.Ceiling(Math.Log10((double)num));

return exponent;
}
else
{
int scalePart = bits[3];

// Accoring to MSDN, the exponent is represented by
// bits 16-23 (the 2nd word):
// http://msdn.microsoft.com/en-us/library/system.decimal.getbits.aspx
int exponent = (scalePart & 0x00FF0000) >> 16;

return exponent + 1;
}
}

我还没有彻底测试过。不过,这里有一些示例输入/输出:

Value          Precision0              1 digit(s).0.000          4 digit(s).1.23           3 digit(s).12.324         5 digit(s).1.2300         5 digit(s).-5             1 digit(s).-5.01          3 digit(s).-0.012         4 digit(s).-0.100         4 digit(s).0.0            2 digit(s).10443.31       7 digit(s).-130.340       6 digit(s).-80.8000       6 digit(s).

Using this code, I imagine I would accomplish my goal by doing something like this:

public static decimal DivideUsingLesserPrecision(decimal x, decimal y)
{
int xDigitCount = x.GetSignificantDigitCount();
int yDigitCount = y.GetSignificantDigitCount();

int lesserPrecision = System.Math.Min(xDigitCount, yDigitCount);

return System.Math.Round(x / y, lesserPrecision);
}

不过,我还没有真正完成这个工作。任何想要分享想法的人:我们将不胜感激!

<小时/>

原始问题

假设我写了这段代码:

decimal a = 1.23M;
decimal b = 1.23000M;

Console.WriteLine(a);
Console.WriteLine(b);

以上将输出:

1.231.23000

I find that this also works if I use decimal.Parse("1.23") for a and decimal.Parse("1.23000") for b (which means this question applies to cases where the program receives user input).

So clearly a decimal value is somehow "aware" of what I'll call its precision. However, I see no members on the decimal type that provide any way of accessing this, aside from ToString itself.

Suppose I wanted to multiply two decimal values and trim the result to the precision of the less precise argument. In other words:

decimal a = 123.4M;
decimal b = 5.6789M;

decimal x = a / b;

Console.WriteLine(x);

以上输出:

21.729560302171195125816619416

我要问的是:我如何编写一个返回 21.73 的方法(因为 123.4M 有四个有效数字)?

需要明确的是:我意识到我可以对两个参数调用 ToString,计算每个字符串中的有效数字,并使用该数字对计算结果进行四舍五入。如果可能的话,我正在寻找一种不同的方式。

(我意识到,在大多数处理有效数字的情况下,您可能不需要使用decimal类型。但是我'我问这个问题是因为,正如我在开头提到的,decimal 类型似乎包含有关精度的信息,而 double 则不包含有关精度的信息。我知道。)

最佳答案

您可以使用Decimal.GetBits获取原始数据,并从中计算出来。

不幸的是,我现在没有时间编写示例代码 - 您可能想使用 BigInteger对于一些操作,如果您使用的是 .NET 4 - 但希望这能让您继续前进。只需计算出精度,然后对原始结果调用 Math.Round 可能是一个好的开始。

关于.net - 有没有办法得到小数的 "significant figures"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3683718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com