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

2

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

Salify answered 21/4, 2011 at 0:49 Comment(7)
what do you mean by 'long time' ?Runty
I seriously doubt that .NET cannot handle values greater than 10^-308... (Just pulling your leg!)Narcose
Since you don't say what pp is, I'm not sure there's much we can do to help.Warmongering
The "largest" standard C#/.NET 3.5 data-type is decimal (effectively a double with twice the bits). It may or may not be suitable for your needs.Eleni
@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.Warmongering
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").Diagonal
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.Warmongering
R
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.

Runty answered 21/4, 2011 at 1:13 Comment(7)
This isn't a bad idea, but it's not clear that this would be any faster than OP's current solution.Warmongering
@Warmongering 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 (emilstefanov.net/Projects/GnuMpDotNet).Runty
@magma: I don't know how you'd calculate .4 ^ 1e8 with integers. How would you do it?Warmongering
@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!Pelvic
@Eric: So you're suggesting using arbitrary precision integers to roll your own arbitrary precision floating point, then?Warmongering
@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.Pelvic
@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.Warmongering
S
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.

Simsar answered 21/4, 2011 at 1:59 Comment(0)
R
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.

Runty answered 21/4, 2011 at 1:13 Comment(7)
This isn't a bad idea, but it's not clear that this would be any faster than OP's current solution.Warmongering
@Warmongering 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 (emilstefanov.net/Projects/GnuMpDotNet).Runty
@magma: I don't know how you'd calculate .4 ^ 1e8 with integers. How would you do it?Warmongering
@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!Pelvic
@Eric: So you're suggesting using arbitrary precision integers to roll your own arbitrary precision floating point, then?Warmongering
@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.Pelvic
@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.Warmongering

© 2022 - 2024 — McMap. All rights reserved.