Is it possible to get more than 100 decimal digits in C#?
If yes what is the necessary portion of code?
In Java there something call BigDecimal
but it still can't reach more than 55 digits.
The C++ setprecision can also be used to format only the decimal places instead of the whole floating-point or double value. This can be done using the fixed keyword before the setprecision() method.
The significant digits to the left of the decimal and the significant digits to the right of the decimal are stored in separate groups of bytes. At the maximum precision specification, DECIMAL(32,s) data types can store s-1 decimal digits to the right of the decimal point, if s is an odd number.
Unlike whole numbers, fractional place values are ordered from left to right. Starting at the decimal point, the place values are tenths, hundredths, thousandths, ten-thousandths, hundred-thousandths, and millionths. The tenths place is the largest place value for fractional numbers.
There's an interesting thing to point out when comparing double and decimal : the range of double is ±5.0 × 10−324 to ±1.7 × 10308, while the range of decimal is (-7.9 x 1028 to 7.9 x 1028) / (100 to 28). In other words, the range of double is several times larger than the range of decimal .
Download the J# Redistributable to obtain the J# class libraries. This article hints about how to use it to get ZIP-ability in your .NET projects, and explains a bit about BigDecimal, but as Ramhound explained, the article is relatively old.
After download, add the vsjlib
to your project. It didn't work for me on .NET 4.0, but it did work on 2.0 project. It contains a BigDecimal class, among others. Not sure, however, if that gives you up to 100 decimals, but you might give it a try.
You can download a wrapper for the MPIR library for .NET here (download version 0.2). Then, do the following:
\wrapper.*.dll
to your project, make sure that on each built they are copied to your Debug or Release directory.\wrapper\xmpir.cs
to your projectAdd the following code to your project:
// set the precision to 512 bits (adjust this to your needs)
mpir.mpf_set_default_prec(512);
ulong default_prc = mpir.mpf_get_default_prec();
// init vars (important!)
mpir.mpf_t val = mpir.mpf_init_set_d(.5);
mpir.mpf_t result = mpir.mpf_init_set_ui(0);
// calculate 0.5^200
mpir.mpf_pow_ui(result, val, 200);
double dresult = mpir.mpf_get_d(result);
// convert result to a string, in the form 1.2345 (dot not in output) and exp holding exponent
long exp;
string sresult = mpir.mpf_get_string(out exp, 10, 0, result);
// free vars (important!)
mpir.mpf_clear(val);
mpir.mpf_clear(result);
Note that the variable sresult
will only hold the significant digits. You'll have to add the logic for printing the exponent as well, or a bunch of zeroes.
Result is 6.2230152778611417071440640537801242405902521687211671331011166147896988340353834411839448231257136169569665895551224821247160434722900390625E-60
A documentation PDF file on the full MPIR library shows how to use it in more detail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With