I have to divide 1 by a number X of more than 4000 digits that I have stored in a string and obviously this is going to return a floating point number. I'm looking for algorithms to perform this division efficiently but I could not find anything that convinces me.
As a side note, I would like to implement the algorithm on my own without using a third-party library.
Anyone have any idea?
Thanks!
EDIT: The reason why I do not want to use a third-party library it's that I want to do t开发者_JAVA技巧his operation using openCL but without losing too much accuracy in the process. Therefore using one of those libraries is actually not possible in this case.
You are describing a special case of division, known as inverting a number. Here's a paper which gives a description of Picarte's Iteration method of inverting a large integer: http://www.dcc.uchile.cl/~cgutierr/ftp/picarte.pdf
You should take a look at the GNU Multiple Precision Arithmetic Library, it has no limits to the size of the numbers handled, and will obviously have insanely well optimized number crunching algorithms.
As for implementing it yourself, if it's not for educational purposes, I'd say don't fall prey to the NIH syndrome! And a Web search on binary arithmetic
should provide a wealth of documents to start with…
You should use the System.Numerics.BigInteger structure, it allows you to make a lot of calculations however it's only available in the .NET 4.0
If your number X is an integer you may well not be able to do what you want. float
and double
are pretty much out; you'll have to use a long double
. On some platforms a long double
is just a double
.
If you don't want to use a third-party bignum package (why?), you will have to implement the division algorithm on your own (and that is pretty much going to require you to develop a good chunk of a bignum package).
精彩评论