I want to represent A LOT of decimal digits of a irrational number like sqrt(2) or cos(23). For example i need to show the first 100,000 decimal digits of sqrt(2) , this is possible in c#?, how i can do this?
Asked
Active
Viewed 730 times
-1
-
2You're looking for a `BigDecimal` type. – SLaks Dec 20 '16 at 20:50
-
3It depends on the number - if you can find a taylor series or other method to calculate the number then yes, it's possible. Not _simple_ but _possible_. – D Stanley Dec 20 '16 at 20:50
-
possible duplicate of http://stackoverflow.com/questions/10359372/is-there-a-bigfloat-class-in-c – Georg Dec 20 '16 at 20:52
-
with build in type? Nope. Custom calculation, yes. But whether you want to take it that far is another question – Steve Dec 20 '16 at 20:55
-
it is simple. go to wolframalpha and get everything you want. for example https://www.wolframalpha.com/input/?i=cos+23 – M.kazem Akhgary Dec 20 '16 at 20:56
2 Answers
0
AFAIK (please any reader correct me if I'm wrong) there's no built in type for that. However, as workarounds:
- An answer here with some system for it.
- Use BigInteger and do the rest yourself.
- Use BigRational mentioned here.
0
I solved this problem with the GNU MPFR Library , specifically with a c# wrapper by Luboš Hemala, then i code an algorithm for Nth roots.
public static void Nth_Root(ref StringBuilder Output,ref long ComaPos, String Radicand, String Index, ulong Precision,int NBase)
{
// (1 / Input Index) = N_Index
Decimal D_index = Convert.ToDecimal(Index, new CultureInfo("en-US"));
D_index = 1.00m / D_index;
String N_Index = D_index.ToString(new CultureInfo("en-US"));
//Calculating Precision
Decimal Prec02 = Precision * 3.31125m;
ulong Prec03 = Convert.ToUInt64(Prec02);
int Prec04 = Convert.ToInt32(Precision);
// Declaring MPFR Variables
var Rad = new mpfr_struct();
var Ind = new mpfr_struct();
var Res = new mpfr_struct();
MPFRLibrary.mpfr_init2(Rad, 1000LU);
MPFRLibrary.mpfr_init2(Ind, 1000LU);
MPFRLibrary.mpfr_init2(Res, Prec03);
//String inputs to MPFR variables
MPFRLibrary.mpfr_set_str(Rad, Radicand, 10, (int)Rounding.AwayFromZero);
MPFRLibrary.mpfr_set_str(Ind, N_Index, 10, (int)Rounding.AwayFromZero);
//Calculating Power of Radicand to the N_Index
var sb = new StringBuilder(Prec04);
long expptr = 0;
MPFRLibrary.mpfr_pow(Res, Rad, Ind, (int)Rounding.AwayFromZero);
//Converting MPFR Result to StringB
MPFRLibrary.mpfr_get_str(sb, ref expptr, NBase, 0, Res, (int)Rounding.AwayFromZero);
Output = sb;
ComaPos = expptr;
}

Julian Zatloukal
- 109
- 7