Help on code please

10 views
Skip to first unread message

inkyvoyd

unread,
Feb 23, 2012, 2:08:46 AM2/23/12
to mpir-devel
Hi, I'm quite a beginner when it comes to writing code, but I thought
I start learning C++ by writing some code that approximated polynomial
roots with the Newton-Raphson method. The program asks for the number
of terms in the polynomial, and then stores coefficients and exponents
in an array (a bad idea, but I have no idea how to do it in a better/
more efficient way). The program then calculates f(x) and f'(x) (x
being a guess), and then evaluates x-(f(x)/f'(x)). This evaluation
becomes the new x, and after the number of iterations prompted for are
finished, the program pauses. The program seemed to work well enough
for the first two iterations, but then it never got another digit of
accuracy. I assumed (incorrectly?) that this was because the precision
of the numbers were too low to produce a more accurate estimate. I
decided to see the results of trying to do bignum arithmetic (and I
figured that it would be worth it, because without a bignum arithmetic
library it would be worthless to do more than 10 iterations of most
polynomial estimates), and then spent the next three hours figuring
out how to compile, build, and place the libraries in their correct
spots. I then read the documentation, and changed all most of my
calculations into functions with mpf numbers. There are a couple of
things that are giving me some trouble though.
The first would probably be" void mpf_set_default_prec"; if I wanted
to set the default precision to 256 bits, what should the argument be?
(I know that the documentation did cover this, but no matter how I
change the argument, there's always a syntax error)
The second is that mpf_mul does not allow me to multiply an mpf
number by a long double. The long double I must multiply the mpf
number is inside a for loop and changes every time the loop is goes
through once. How should I work around this?
Finally, I was considering storing the mpf numbers in an array, but
I'm not completely sure how to do this. Could someone please explain
to me (or just link me) on how to manage custom arrays?
Thanks

Cactus

unread,
Feb 23, 2012, 3:25:28 AM2/23/12
to mpir-...@googlegroups.com
Hello,

Welcome to mpir-devel!   The first thing that I can say is that MPIR on its own is not the best application to use for multiple precision floating point applications.  The mpf functions work but they have now been superseded by another package called MPFR (see www.mpfr.org) that provides significant improvements for high precision floating point algorithms.  

Although this requires yet another package to install, I think you will find that the extra effort will be worthwhile.  In particular this will give you better control over precision and also allow long double conversions provided that your build environment supports this type (Microsoft build tools on Windows don't support long double).

The second point I would make is that there are also C++ packages that allow high precision floating point types in a way that is essentially identical to ordinary double types - see, for example, the MPFRC++ (http://www.holoborodko.com/pavel/mpfr/).  These packages allows you to develop your code using ordinary doubles and, once it is working, you can then do a simple search and replace operation to convert 'double' into 'mpreal' and it will then work with high precision floating point types (a few other changes might be needed).  Pavel's web  page gives examples and you might also be interested in Rick Regan's 'Exploring Binary' where he explains how he went through the issue you are looking at here:  http://www.exploringbinary.com/how-to-install-and-run-gmp-on-windows-using-mpir/  (he uses mpf types but I would recommend that you use MPFRC++ instead).  He aslo gives examples that might help you.

If you need more help, I would be happy to look at your code and offer advice on how to get it working with MPFRC++.  If you have an email address, I will be willing to advise you off-line or you can continue to ask questions here.

    good luck,

       Brian Gladman
 

inkyvoyd

unread,
Feb 23, 2012, 7:16:59 AM2/23/12
to mpir-devel
Thanks for the helpful response.
I've downloaded mpfr and mpfrcpp, but I'm not sure what to do exactly
to include them (while I'm compiling?). Also, would it be alright if I
sent you all of my code together? To be honest, I have very little
idea of what I'm doing in general; it took me quite a few hours to
understand just how to use custom libraries, and I'm still not sure if
I've used them correctly.
Thanks,
Ailec

On Feb 23, 1:25 am, Cactus <rieman...@gmail.com> wrote:
> Hello,
>
> Welcome to mpir-devel!   The first thing that I can say is that MPIR on its
> own is not the best application to use for multiple precision floating
> point applications.  The mpf functions work but they have now been
> superseded by another package called MPFR (seewww.mpfr.org) that provides
> significant improvements for high precision floating point algorithms.
>
> Although this requires yet another package to install, I think you will
> find that the extra effort will be worthwhile.  In particular this will
> give you better control over precision and also allow long double
> conversions provided that your build environment supports this type
> (Microsoft build tools on Windows don't support long double).
>
> The second point I would make is that there are also C++ packages that
> allow high precision floating point types in a way that is essentially
> identical to ordinary double types - see, for example, the MPFRC++ (http://www.holoborodko.com/pavel/mpfr/).  These packages allows you to
> develop your code using ordinary doubles and, once it is working, you can
> then do a simple search and replace operation to convert 'double' into
> 'mpreal' and it will then work with high precision floating point types (a
> few other changes might be needed).  Pavel's web  page gives examples and
> you might also be interested in Rick Regan's 'Exploring Binary' where he
> explains how he went through the issue you are looking at here:  http://www.exploringbinary.com/how-to-install-and-run-gmp-on-windows-...

Cactus

unread,
Feb 23, 2012, 7:26:02 AM2/23/12
to mpir-...@googlegroups.com
Yes, I can help you via email if you can let me have an email address (or if you know mine).  But I can only help on Windows as I don't use Linux for development.

    Brian

inkyvoyd

unread,
Feb 24, 2012, 12:56:38 AM2/24/12
to mpir-devel
Hello,
My email is \aile\cwu\@gm\ail.co\m (without the backslashes). I also
only work with Windows because I believe that most parts of Linux are
much too technical for me to understand. I don't really understand how
to MPFR, or MPFR C++, so I haven't been able to really edit my code;
what's in my code right now is the MPIR code, and some commented out
code that was originally made to work with cmath. I do have to warn
you that my code might have numerous syntax errors, as well as be very
badly written in general. For that, I do have to apologize beforehand.
Thanks!

Cactus

unread,
Feb 27, 2012, 3:56:08 AM2/27/12
to mpir-...@googlegroups.com

On Friday, February 24, 2012 5:56:38 AM UTC, inkyvoyd wrote:
Hello,
My email is \aile\cwu\@gm\ail.co\m (without the backslashes). I also
only work with Windows because I believe that most parts of Linux are
much too technical for me to understand. I don't really understand how
to MPFR, or MPFR C++, so I haven't been able to really edit my code;
what's in my code right now is the MPIR code, and some commented out
code that was originally made to work with cmath. I do have to warn
you that my code might have numerous syntax errors, as well as be very
badly written in general. For that, I do have to apologize beforehand.
Thanks!

I emailed you but I did not get a reply.  Did you receive my email?

        Brian
 

inkyvoyd

unread,
Feb 29, 2012, 12:07:20 AM2/29/12
to mpir-devel
Oh yes, sorry. I went psuedo camping on the weekend, so I wasn't able
to reply. I'll be somewhat busy this week, so I might not be able to
answer emails often.
Reply all
Reply to author
Forward
0 new messages