2

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

Gabe
  • 84,912
  • 12
  • 139
  • 238
xsari3x
  • 442
  • 2
  • 12
  • 36
  • what do you mean by 'long time' ? – magma Apr 21 '11 at 00:56
  • 6
    I seriously doubt that .NET cannot handle values greater than 10^-308... (Just pulling your leg!) – Andreas Rejbrand Apr 21 '11 at 00:59
  • Since you don't say what `pp` is, I'm not sure there's much we can do to help. – Gabe Apr 21 '11 at 01:04
  • The "largest" standard C#/.NET 3.5 data-type is [decimal](http://msdn.microsoft.com/en-us/library/364x0z75%28v=vs.71%29.aspx) (effectively a double with twice the bits). It may or may not be suitable for your needs. –  Apr 21 '11 at 01:09
  • @pst: OP is trying to compute `0.4 ^ 1E+8`, which is a number with 40 million zeros between the decimal point and the first non-zero digit! Clearly `decimal` is not suitable to his needs. – Gabe Apr 21 '11 at 01:20
  • http://www.wolframalpha.com/input/?i=0.4%5e(100000000) ; i.e. I'd look at smarter ways of doing this; I'm assuming there is some algorithm to do this more efficiently (even if that algorithm ends up being "use mathematica"). – Noon Silk Apr 21 '11 at 01:28
  • Running `new BigFloat(0.4, new PrecisionSpec(1000, PrecisionSpec.BaseType.DEC)).Pow(100000000);` takes about 11ms on my machine. That seems pretty fast to me. – Gabe Apr 21 '11 at 02:37

2 Answers2

7

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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
3

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.

Community
  • 1
  • 1
magma
  • 8,432
  • 1
  • 35
  • 33
  • This isn't a bad idea, but it's not clear that this would be any faster than OP's current solution. – Gabe Apr 21 '11 at 01:31
  • 1
    @Gabe you are right - we don't know. I suppose that it *might* be faster, but we are not told much about the purpose, context and reasoning behind the above code, so we cannot even reproduce the issue. Another option would be using Integers. BigInteger in C# 4.0, or GnuMpDotNet (http://www.emilstefanov.net/Projects/GnuMpDotNet/). – magma Apr 21 '11 at 01:55
  • @magma: I don't know how you'd calculate `.4 ^ 1e8` with integers. How would you do it? – Gabe Apr 21 '11 at 02:38
  • @Gabe: Trivially. You'd calculate 2^n and 5^n in integers. Their ratio is .4^n. Of course, you'd need a pretty darn big integer! – Eric Lippert Apr 21 '11 at 06:33
  • @Eric: So you're suggesting using arbitrary precision integers to roll your own arbitrary precision floating point, then? – Gabe Apr 21 '11 at 06:50
  • @Gabe: Since we have no idea why this guy is trying to calculate this crazy thing, I'd first figure that out and then make a recommendation. In general rolling your own math libraries is often not a hot idea though. – Eric Lippert Apr 21 '11 at 06:53
  • @Eric: The OP posted their code in the second comment to this answer. I can't say I understand what he's doing, but he's doing computations outside the exponent range of regular float types. He already has a math library that handles it, he just thinks it's too slow. When magma suggested that maybe using integers would be faster, I asked how. Your suggestion to that was akin to the same method used by the OP's math library. – Gabe Apr 21 '11 at 07:34