Multiplication of thousands of small values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SoFaraway
    New Member
    • Jun 2007
    • 9

    Multiplication of thousands of small values

    Dear all,

    I need to do multiplication of thousands of floating points in C, here are some values:
    4.7167839616062 8e-09
    0.0722222262576 93
    4.7167839616062 8e-09
    0.0111111157230 777

    I need to read these values from a file, then do multiplication of them. I use
    Code:
    long double *value;
    value = (long double *)malloc(n * sizeof(long double));
    for(i = 1...n)
      fscanf(fp, "%lf", value[i]);
    However, I always get some very funny values. When I change long double to double, it also fails.

    Anyone can help me?

    Thank you very much!
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    It could be an 'ill conditioned' system you're working with. First read all the numbers,
    sort them and multiply the largest numbers first if they're all |x| < 1

    kind regards,

    Jos

    Comment

    • SoFaraway
      New Member
      • Jun 2007
      • 9

      #3
      I forgot to mention that there is error when I use the following code to read in values:

      3 values to be read from the file:
      # 0.0001528238025 13597 476

      code to read and check the values:
      Code:
      long double d1, d2;
      fscanf(fp, "%s %lf %d", tmp, &d1, &d2);
      printf("%s %.30lf %d ", tmp, d1, d2);
      The values printed are:
      # 0.0001528238025 135970087984432 77 0

      Any idea about this?

      Thank you very much!

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by SoFaraway
        I forgot to mention that there is error when I use the following code to read in values:

        3 values to be read from the file:
        # 0.0001528238025 13597 476

        code to read and check the values:
        Code:
        long double d1, d2;
        fscanf(fp, "%s %lf %d", tmp, &d1, &d2);
        printf("%s %.30lf %d ", tmp, d1, d2);
        The values printed are:
        # 0.0001528238025 135970087984432 77 0

        Any idea about this?

        Thank you very much!
        You're trying to read an int (%d) and store the four bytes in a long double (d2).
        First make sure that you can read those numbers correctly before you attempt
        to multiply certain values. As far as I can see now this has nothing to do with
        ill conditioned numerical systems, just IO bugs.

        kind regards,

        Jos

        Comment

        • SoFaraway
          New Member
          • Jun 2007
          • 9

          #5
          Originally posted by JosAH
          You're trying to read an int (%d) and store the four bytes in a long double (d2).
          First make sure that you can read those numbers correctly before you attempt
          to multiply certain values. As far as I can see now this has nothing to do with
          ill conditioned numerical systems, just IO bugs.

          kind regards,

          Jos

          Hi Jos,

          Thank you for your reply.

          Sorry that I typed wrongly in the previous post. My original code is
          Code:
          long doube d1;
          int d2;
          which is in correct format.

          Do you have any suggestion about the error?

          Thanks again!

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by SoFaraway
            Hi Jos,

            Thank you for your reply.

            Sorry that I typed wrongly in the previous post. My original code is
            Code:
            long doube d1;
            int d2;
            which is in correct format.

            Do you have any suggestion about the error?
            Nope, I'm sorry; after my first answer you come up with something completely
            different; I answer again and then you twist the problem to something else
            again. I can't follow you anymore. Please make up your mind first, state the
            problem you're really experiencing and only *then* raise a question.

            The way this thread works out now is not the way questions are stated and
            possibly answered. You're just playing hide an' seek and I don't play those games.


            kind regards,

            Jos

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Heya, SoFaraway.

              What code are you using right now (copy and paste are your friends ~_^)?

              What is an example of an input file that your script is supposed to read?

              What is the output that you are getting when you run this file through your code?

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                OK. Let's start again:

                This data:

                4.7167839616062 8e-09
                0.0722222262576 93
                4.7167839616062 8e-09
                0.0111111157230 777

                was put into a text file and that file was read by this code:
                [code=cpp]
                int main()
                {
                int n = 1000;
                FILE* fp;
                fp = fopen("C:\\scra tch\\instructor \\data.txt", "r");
                if (!fp)
                {
                cout << "Failed to open" << endl;
                return 0;
                }
                long double *value;
                int numread = 0;
                value = (long double *)malloc(n * sizeof(long double));
                while (fscanf(fp, "%lf", &value[numread]) != EOF)
                {
                ++numread;
                }
                //Playback
                for (int i = 0; i < numread; ++i)
                {
                printf("%e\n", value[i]);}
                }
                [/code]
                These are the results:

                4.71678e-009
                0.0722222
                4.71678e-009
                0.0111111

                You can't reall fscanf() into a long double since the %f means to scan to a float (6 significant figures). The excess is rounded. The %lf really does nothing more than %f and is Microsoft specific.


                There were too many errors in the sample to address each one.

                Comment

                • ravenspoint
                  New Member
                  • Jul 2007
                  • 111

                  #9
                  The trick is to specify the precision you want on output.

                  If you use

                  printf("%.20e\n ", value[i]);

                  you should get

                  4.7167839616062 8030000e-009
                  7.2222226257693 0060000e-002
                  4.7167839616062 8030000e-009
                  1.1111115723077 7000000e-002

                  James

                  Comment

                  Working...