pi calculation

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

  1. bpazolli

    bpazolli Guest

    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
     
    bpazolli, Dec 30, 2006
    #1
    1. Advertising

  2. bpazolli

    Moonlit Guest

    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);
    > 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
    >
     
    Moonlit, Dec 30, 2006
    #2
    1. Advertising

  3. bpazolli

    bpazolli Guest

    > 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
    #3
  4. bpazolli

    Moonlit Guest

    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
    #4
  5. bpazolli

    BobR Guest

    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
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sunil

    power calculation in fpga

    sunil, Feb 10, 2004, in forum: VHDL
    Replies:
    2
    Views:
    1,293
    Brendan Cullen
    Feb 11, 2004
  2. Runar Gjelsvik

    CRC polynomal calculation

    Runar Gjelsvik, Apr 28, 2004, in forum: VHDL
    Replies:
    2
    Views:
    2,296
    Allan Herriman
    Apr 29, 2004
  3. Daniel

    calculation in string

    Daniel, Aug 27, 2003, in forum: Perl
    Replies:
    7
    Views:
    2,855
    Trent Curry
    Sep 10, 2003
  4. =?Utf-8?B?SXNzYWMgR29tZXo=?=

    Date Calculation

    =?Utf-8?B?SXNzYWMgR29tZXo=?=, Jun 9, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    4,434
    mikeb
    Jun 9, 2004
  5. vadim

    bandwidth calculation

    vadim, Dec 31, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    512
    Ken Dopierala Jr.
    Dec 31, 2004
Loading...

Share This Page