Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > pi calculation

Reply
Thread Tools

pi calculation

 
 
bpazolli
Guest
Posts: n/a
 
      12-30-2006
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);
mpf_add (x, x, y);
c=c+2;
mpf_set_si (y, c);
mpf_ui_div (y, f, y);
mpf_add (x, x, 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

 
Reply With Quote
 
 
 
 
Moonlit
Guest
Posts: n/a
 
      12-30-2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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);
> mpf_add (x, x, y);
> c=c+2;
> mpf_set_si (y, c);
> mpf_ui_div (y, f, y);
> mpf_add (x, x, 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
>



 
Reply With Quote
 
 
 
 
bpazolli
Guest
Posts: n/a
 
      12-30-2006
> 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

 
Reply With Quote
 
Moonlit
Guest
Posts: n/a
 
      12-30-2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>> 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
>



 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      12-30-2006

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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what subnet calculation to use for 70-216 jmiddleton67@hotmail.com Microsoft Certification 3 04-07-2005 01:52 PM
CRC polynomal calculation Runar Gjelsvik VHDL 2 04-29-2004 09:08 AM
power calculation in fpga sunil VHDL 2 02-11-2004 12:16 PM
bgp weight calculation Glenn Cisco 4 10-29-2003 02:46 AM
calculation in string Daniel Perl 7 09-10-2003 07:33 PM



Advertisments