Is There a Bigfloat Class in C#

Is there a BigFloat class in C#?

Perhaps you're looking for BigRational? Microsoft released it under their BCL project on CodePlex. Not actually sure how or if it will fit your needs.

It keeps it as a rational number. You can get the a string with the decimal value either by casting or some multiplication.

var r = new BigRational(5000, 3768);
Console.WriteLine((decimal)r);
Console.WriteLine((double)r);

Or with a simple(ish) extension method like this:

public static class BigRationalExtensions
{
public static string ToDecimalString(this BigRational r, int precision)
{
var fraction = r.GetFractionPart();

// Case where the rational number is a whole number
if(fraction.Numerator == 0 && fraction.Denominator == 1)
{
return r.GetWholePart() + ".0";
}

var adjustedNumerator = (fraction.Numerator
* BigInteger.Pow(10, precision));
var decimalPlaces = adjustedNumerator / fraction.Denominator;

// Case where precision wasn't large enough.
if(decimalPlaces == 0)
{
return "0.0";
}

// Give it the capacity for around what we should need for
// the whole part and total precision
// (this is kinda sloppy, but does the trick)
var sb = new StringBuilder(precision + r.ToString().Length);

bool noMoreTrailingZeros = false;
for (int i = precision; i > 0; i--)
{
if(!noMoreTrailingZeros)
{
if ((decimalPlaces%10) == 0)
{
decimalPlaces = decimalPlaces/10;
continue;
}

noMoreTrailingZeros = true;
}

// Add the right most decimal to the string
sb.Insert(0, decimalPlaces%10);
decimalPlaces = decimalPlaces/10;
}

// Insert the whole part and decimal
sb.Insert(0, ".");
sb.Insert(0, r.GetWholePart());

return sb.ToString();
}
}

If it's out of the precision range of a decimal or double, they will be cast to their respective types with a value of 0.0. Also, casting to decimal, when the result is outside of its range, will cause an OverflowException to be thrown.

The extension method I wrote (which may not be the best way to calculate a fraction's decimal representation) will accurately convert it to a string, with unlimited precision. However, if the number is smaller than the precision requested, it will return 0.0, just like decimal or double would.

Calculating big float number fast like 0.4 ^ 100000000 ,, any ideas?

Download, and reference, the Microsoft J# .NET Library from your C# project - so that you can use J#'s BigDecimal implementation.

See:

Arbitrary-Precision Decimals in C#

Big Decimal:

Install the J# runtime (it's free):
http://www.microsoft.com/downloads/en/details.aspx?familyid=f72c74b3-ed0e-4af8-ae63-2f0e42501be1&displaylang=en

and:

Arbitrary precision decimals in C#?

and:

http://geekswithblogs.net/gyoung/archive/2006/05/01/76869.aspx

The J# re-distributables contain very
well tested implementations of
BigInteger and BigDecimal that you can
use directly in your .NET apps simply
by referencing the J# assembly
vjslib.dll.
http://download.microsoft.com/download/2/e/9/2e9bde04-3af1-4814-9f1e-733f732369a3/NETMatters0512.exe
discusses this further. It also
contins some Zip classes which are
quite useful.

and:

MSDN - BigInteger, GetFiles, and More

While you can search the Web to find a
plethora of implementations in C#,
C++, and a variety of other languages,
it might not be necessary. If you
don't mind taking a dependency on the
J# libraries, you already have a big
number implementation at your
disposal. In fact, you have two. The
J# run-time library, vjslib.dll, is
available as a redistributable
component, just like the .NET
Framework. You can download it from
Visual J# Downloads (it's also
installed as a prerequisite by Visual
Studio®). In the same manner that a C#
or C++ application can make use of
Microsoft.VisualBasic.dll (the Visual
Basic run-time library), C#, Visual
Basic®, and C++ applications can use
the J# run-time library and the
numerous interesting classes it
exposes.

Is there a 128 or 256 bit double class in .net?

Maybe the Decimal type would work for you?

Very, very large C# floating-point numbers

Use units of megapeople to achieve more headroom.

Also, Decimal lets you have 100,000 planets each with 100000000000000 times the population of the Earth, if my arithmetic is right. Are you sure that's not enough?

How to handle float values that are greater than the maximum value of double

BigRational from the base class library team from Microsoft. It uses big integers to store it as a fraction, but supports all kinds of operators.

When it comes to printing it as a decimal, I think you need to write your own implementation for that. I have one written somewhere for this class, but I'd have to find it.

What is the equivalent of the Java BigDecimal class in C#?

C# only has BigInteger built it (in .NET framework 4).

Is decimal enough precision for your task? It's a 128-bit number that can hold values in the range ±1.0 × 10−28 to ±7.9 × 1028.

Very large number manipulations in C#

For big floating point numbers you could use BigRational. This answer has a nice explanation about it.



Related Topics



Leave a reply



Submit