输出双精度值的完整精度。 [英] Output the full pricision of a double value.

查看:99
本文介绍了输出双精度值的完整精度。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是给出R ^ n的确切值。其中R是实数(0.0


示例:

My question is to give the exact value of R^n. where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.

Example:

95.123^12



答案是


The answer is

548815620517731830194541.899025343415715973535967221869852721



另一个例子是:


Another example is:

0.4321^20



输出应为:


The output should be:

.00000005148554641076956121994511276767154838481760200726351203835429763013462401



我的代码是:


My code is:

var result = Math.Pow(R, n);
string s = result.ToString("R");
Console.WriteLine(s);



我得到的是:


What I get is:

5.4881562051773216E+23
5.1485546410769524E-08

推荐答案

如果要显示10位小数,请尝试 Console.WriteLine(c.ToString(N10));
If you want to display, say 10, decimal places, try Console.WriteLine(c.ToString("N10"));.


您可以将'double result'转换为'decimal',以便能够生成包含更多数字的字符串结果。一个大的'double'将用尾数/指数表示法呈现,因为它就是它的内幕。
You can convert your 'double result to 'decimal to be able to produce a string result with more digits. A big 'double is going to be rendered with the mantissa/exponent notation because that is what it is, under-the-hood.
private string PowerDoubleToDecimal(double doubleValue, double toPower, string formatString)
{
    decimal result = Convert.ToDecimal(Math.Pow(doubleValue, toPower));
    return string.Format(result.ToString(), formatString);
}

// test in some method
string test1 = PowerDoubleToDecimal(95.123, 12.0, "C");
Console.WriteLine(test1);

string test1a = PowerDoubleToDecimal(0.4321, 20.0, "C");
Console.WriteLine(test1a);

// output
548815620517732000000000

0.0000000514855464107695

Jon Skeet有公共域代码,'DoubleConverter:'[ ^ ]从Double到String获得更好的'转录'。使用Skeet的代码,我从两个试验中得到这些结果:

Jon Skeet has public domain code, 'DoubleConverter:' [^] to get a much better 'transcription' from a Double to String. Using Skeet's code, I get these results from your two trials:

private string DblToExactString(double doubleValue, double toPower)
{
    double result = Math.Pow(doubleValue, toPower);
    return SkeetDoubleConverter.ToExactString(result);
}

// test in some method
string test2 = DblToExactString(95.123, 12.0);
Console.WriteLine(test2);

string test2a = DblToExactString(0.4321, 20.0);
Console.WriteLine(test2a);

548815620517732158537728

0.0000000514855464107695238993420865959704801895213677198626101016998291015625

而且,我注意到结果与您的结果不一样。这导致了一个问题:如果你需要这种高精度的某些关键任务应用(如航空,金融,医疗保健等),你会打赌你的公司未来的第三天。派对代码(即使是Jon Skeet的案例,如果是大师的.NET大师),如果它来自你认为可靠的来源吗?



我不会:)我想把钱花在一家知名公司的图书馆里,这家公司在我需要的任何专业高精度性能方面都有多年的经验。



有许多特殊的库可用于处理高精度浮点数(一些开源,一些提供免费和商业版本,一些纯商业版)。对于其他图书馆的链接:[ ^ ]。

And, I note the results are not the same as yours. That leads to the question: if you need this high-precision for some mission-critical application (as in aviation, finance, health-care, etc.), would you bet your company's future on 3rd. party code (even, as is the case with Jon Skeet, a guru's guru in .NET) if it comes from a source you believe reliable ?

I wouldn't :) I'd want to spend the money for a library from a very well-known company with years of experience in whatever specialized high-precision performance I needed.

There are many special libraries available for handling high-precision floating point numbers (some open source, some offering both free and commercial versions, some purely commercial). For links to other libraries: [^].


这篇关于输出双精度值的完整精度。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆