# pi calculation

Discussion in 'C++' started by bpazolli, Dec 30, 2006.

1. ### bpazolliGuest

Ok here we go. I downloaded a pre compiled version of The GNU Multiple
Precision Arithmetic Library. Now I have created a program with it,
that I think should work. I have gotten it down to one error. But being
a noob, I have no idea what it means. Here it is.

Error E2277 main2.cpp 33: Lvalue required in function picalc(int)

Here is my sourcecode.

#include <iostream>
#include <gmp.h>
#include <stdio.h>
using namespace std;

void picalc (int a)
{
char pistring[100];
mpf_t x, y;
mpf_init (x);
mpf_init (y);
double piresult;
int counter;
unsigned long int f;
long int exponent;
counter = 1;
a = (a + 1);
for (long int c=1; c<a; c=c+2)
{
cout << "Doing iteration ..." << counter << "\n";
counter++;
mpf_set_si (y, c);
f=1;
mpf_ui_div (y, f, y);
c=c+2;
mpf_set_si (y, c);
mpf_ui_div (y, f, y);
}
f=4;
mpf_mul_ui (x, x, f);
pistring = mpf_get_str (NULL, &exponent, 10, 0, x); // Here is line 33
no comment
cout << "The result is ... " << pistring;
}

int main ()
{
int n;
cout << "Enter the number of iterations?\n";
cin >> n;

picalc (n);

return (0);
}

I am sure mpf_get_str is causing the problem becuase I have no idea how
to use it. Here is the manual entry below.

char * mpf_get_str (char *str, mp exp t *expptr, int base, size t
n_digits, mpf t op)
Convert op to a string of digits in base base. base can be 2 to 36. Up
to n digits digits
will be generated. Trailing zeros are not returned. No more digits than
can be accurately
represented by op are ever generated. If n digits is 0 then that
accurate maximum number
of digits are generated.
If str is NULL, the result string is allocated using the current
allocation function (see Chapter
14 [Custom Allocation], page 84). The block will be strlen(str)+1
bytes, that being
exactly enough for the string and null-terminator.
If str is not NULL, it should point to a block of n digits + 2 bytes,
that being enough for
the mantissa, a possible minus sign, and a null-terminator. When n
digits is 0 to get all
significant digits, an application won't be able to know the space
required, and str should be
NULL in that case.
The generated string is a fraction, with an implicit radix point
immediately to the left of the
first digit. The applicable exponent is written through the expptr
pointer. For example, the
number 3.1416 would be returned as string "31416" and exponent 1.
When op is zero, an empty string is produced and the exponent returned
is 0.
A pointer to the result string is returned, being either the allocated
block or the given str.

Help Me,
Ben Pazolli

bpazolli, Dec 30, 2006

2. ### MoonlitGuest

Hi,

It says "you are trying to assing a value to something which you can't
assign a value to"

In your case this is pistring[100]

Make it into

char *pistring = 0;

You can't assign a string to a character array.

and don't forget to free or delete pistring (depending whether it was
malloc'ed or new'ed, my guess is the first though).

Regards, Ron AF Greve

http://moonlit.xs4all.nl

"bpazolli" <> wrote in message
news:...
> Ok here we go. I downloaded a pre compiled version of The GNU Multiple
> Precision Arithmetic Library. Now I have created a program with it,
> that I think should work. I have gotten it down to one error. But being
> a noob, I have no idea what it means. Here it is.
>
> Error E2277 main2.cpp 33: Lvalue required in function picalc(int)
>
> Here is my sourcecode.
>
> #include <iostream>
> #include <gmp.h>
> #include <stdio.h>
> using namespace std;
>
> void picalc (int a)
> {
> char pistring[100];
> mpf_t x, y;
> mpf_init (x);
> mpf_init (y);
> double piresult;
> int counter;
> unsigned long int f;
> long int exponent;
> counter = 1;
> a = (a + 1);
> for (long int c=1; c<a; c=c+2)
> {
> cout << "Doing iteration ..." << counter << "\n";
> counter++;
> mpf_set_si (y, c);
> f=1;
> mpf_ui_div (y, f, y);
> c=c+2;
> mpf_set_si (y, c);
> mpf_ui_div (y, f, y);
> }
> f=4;
> mpf_mul_ui (x, x, f);
> pistring = mpf_get_str (NULL, &exponent, 10, 0, x); // Here is line 33
> no comment
> cout << "The result is ... " << pistring;
> }
>
> int main ()
> {
> int n;
> cout << "Enter the number of iterations?\n";
> cin >> n;
>
> picalc (n);
>
> return (0);
> }
>
> I am sure mpf_get_str is causing the problem becuase I have no idea how
> to use it. Here is the manual entry below.
>
> char * mpf_get_str (char *str, mp exp t *expptr, int base, size t
> n_digits, mpf t op)
> Convert op to a string of digits in base base. base can be 2 to 36. Up
> to n digits digits
> will be generated. Trailing zeros are not returned. No more digits than
> can be accurately
> represented by op are ever generated. If n digits is 0 then that
> accurate maximum number
> of digits are generated.
> If str is NULL, the result string is allocated using the current
> allocation function (see Chapter
> 14 [Custom Allocation], page 84). The block will be strlen(str)+1
> bytes, that being
> exactly enough for the string and null-terminator.
> If str is not NULL, it should point to a block of n digits + 2 bytes,
> that being enough for
> the mantissa, a possible minus sign, and a null-terminator. When n
> digits is 0 to get all
> significant digits, an application won't be able to know the space
> required, and str should be
> NULL in that case.
> The generated string is a fraction, with an implicit radix point
> immediately to the left of the
> first digit. The applicable exponent is written through the expptr
> pointer. For example, the
> number 3.1416 would be returned as string "31416" and exponent 1.
> When op is zero, an empty string is produced and the exponent returned
> is 0.
> A pointer to the result string is returned, being either the allocated
> block or the given str.
>
> Help Me,
> Ben Pazolli
>

Moonlit, Dec 30, 2006

3. ### bpazolliGuest

> In your case this is pistring[100]
>
> Make it into
>
> char *pistring = 0;
>
> You can't assign a string to a character array.
>
> and don't forget to free or delete pistring (depending whether it was
> malloc'ed or new'ed, my guess is the first though).

Thanks moonlit, I have the thing compiled now. Now it is time for the
fun stuff, the linking. I had massive errors on this one. I am using
Borland C++ Command Line Compiler 5.5. I get the following errors.

Error: Unresolved external '_mpf_init' referenced from C:\A Bunch of My
Folders\MAIN2.OBJ
Error: Unresolved external '_mpf_set_si' referenced from C:\A Bunch of
My Folders\MAIN2.OBJ
Error: Unresolved external '_mpf_ui_div' referenced from C:\A Bunch of
My Folders\MAIN2.OBJ
Error: Unresolved external '_mpf_add' referenced from C:\A Bunch of My
Folders\MAIN2.OBJ
Error: Unresolved external '_mpf_mul_ui' referenced from C:\A Bunch of
My Folders\MAIN2.OBJ
Error: Unresolved external '_mpf_get_str' referenced from C:\A Bunch of
My Folders\MAIN2.OBJ

I'll search for the answer myself. But any help is obviously
appreciated.

Ben Pazolli

bpazolli, Dec 30, 2006
4. ### MoonlitGuest

Hi,

You didn't link the mpf library with the rest of your objects. Unfortunately
I don't use Borland myself so I give any help to where you can add them,

Regards, Ron AF Greve

http://moonlit.xs4all.nl

"bpazolli" <> wrote in message
news:...
>> In your case this is pistring[100]
>>
>> Make it into
>>
>> char *pistring = 0;
>>
>> You can't assign a string to a character array.
>>
>> and don't forget to free or delete pistring (depending whether it was
>> malloc'ed or new'ed, my guess is the first though).

>
> Thanks moonlit, I have the thing compiled now. Now it is time for the
> fun stuff, the linking. I had massive errors on this one. I am using
> Borland C++ Command Line Compiler 5.5. I get the following errors.
>
> Error: Unresolved external '_mpf_init' referenced from C:\A Bunch of My
> Folders\MAIN2.OBJ
> Error: Unresolved external '_mpf_set_si' referenced from C:\A Bunch of
> My Folders\MAIN2.OBJ
> Error: Unresolved external '_mpf_ui_div' referenced from C:\A Bunch of
> My Folders\MAIN2.OBJ
> Error: Unresolved external '_mpf_add' referenced from C:\A Bunch of My
> Folders\MAIN2.OBJ
> Error: Unresolved external '_mpf_mul_ui' referenced from C:\A Bunch of
> My Folders\MAIN2.OBJ
> Error: Unresolved external '_mpf_get_str' referenced from C:\A Bunch of
> My Folders\MAIN2.OBJ
>
> I'll search for the answer myself. But any help is obviously
> appreciated.
>
> Ben Pazolli
>

Moonlit, Dec 30, 2006
5. ### BobRGuest

Moonlit > wrote in message ...
>Hi,
>You didn't link the mpf library with the rest of your objects. Unfortunately
>I don't use Borland myself so I give any help to where you can add them,
>Regards, Ron AF Greve

http://www.parashift.com/c++-faq-lite/how-to-post.html

BobR, Dec 30, 2006