Click here to Skip to main content
15,921,622 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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:
C#
95.123^12

The answer is
C#
548815620517731830194541.899025343415715973535967221869852721

Another example is:
C#
0.4321^20

The output should be:
C#
.00000005148554641076956121994511276767154838481760200726351203835429763013462401

My code is:
C#
var result = Math.Pow(R, n);
string s = result.ToString("R");
Console.WriteLine(s);

What I get is:
C#
5.4881562051773216E+23
5.1485546410769524E-08
Posted
Comments
PIEBALDconsult 17-Jan-16 20:59pm    
Precision 15-16 digits
https://msdn.microsoft.com/en-us/library/678hzkk9.aspx

Maybe try a Custom Numeric Format String? I haven't tried it, but how about ################0.################
https://msdn.microsoft.com/en-us/library/0c899ak8.aspx
[no name] 17-Jan-16 21:02pm    
It is incorrect string s = result.ToString("################0.################");.
PIEBALDconsult 17-Jan-16 21:37pm    
In what way? You may need to use 308 number signs (#) on each side of the decimal point.

If you want to display, say 10, decimal places, try Console.WriteLine(c.ToString("N10"));.
 
Share this answer
 
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.
C#
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 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:
C#
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
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: [^].
 
Share this answer
 
v2
Comments
[no name] 18-Jan-16 8:29am    
Thanks, it is from here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900