问题
Ehm ... I got a problem I've a certain calculation that result is over 10^-308 (the biggest value in double .net ) any way I solved this problem through a library called BIGFLOAT http://www.fractal-landscapes.co.uk/bigint.html ,
What ever I need to calculate something like 0.4 ^(1000 or 100000000) the problem it takes very very long time I didn't study parallel or distributed programming yet but I need a solution that is fast and understandable for me I'm going to deliver this project in next 6 Hours!! :D
Here's the code :
private BigFloat getBlocking(double k)
{
double p1, p2;
BigFloat p3;
p3 = new BigFloat(pp);
p1 = this.P / (double)(k / (double)this.N);
p2 = Math.Pow((1 - p1), 2);
p3= new BigFloat(1-p2,pp);
p3.Pow((int)k);
return p3;
}
where K is 1000 , N is 1001
回答1:
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.
回答2:
If you don't need all the digits, you can get away with using logarithms. The log of (0.4 ^ 100000000)
is log(0.4)*100000000
, well within the regular floating point range.
来源:https://stackoverflow.com/questions/5738309/calculating-big-float-number-fast-like-0-4-100000000-any-ideas