Problems with multiplications of doubles and/or floats

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • J.K. Becker

    Problems with multiplications of doubles and/or floats

    Hi there,

    I am trying to multiply doubles with floats (actually I tried every
    possible combination by now) and it never works (well, it does something
    but it is always wrong). I have no idea what it is and where to look for
    help, maybe some of you know?

    double=float*do uble; (or every possible combination of it). An example:

    0.3 * 0.7 would result in 1.7 (with lots more digits). Anyone any idea?
    If I change the types of the variables I think the result stays the same
    (but I am not 100% sure)...

    Jens

  • J.K. Becker

    #2
    Re: Problems with multiplications of doubles and/or floats

    Sorry, I forgot:

    Linux (Debian) and gcc3.3

    Jens

    Comment

    • Thomas Matthews

      #3
      Re: Problems with multiplications of doubles and/or floats

      J.K. Becker wrote:[color=blue]
      > Hi there,
      >
      > I am trying to multiply doubles with floats (actually I tried every
      > possible combination by now) and it never works (well, it does something
      > but it is always wrong). I have no idea what it is and where to look for
      > help, maybe some of you know?
      >
      > double=float*do uble; (or every possible combination of it). An example:
      >
      > 0.3 * 0.7 would result in 1.7 (with lots more digits). Anyone any idea?
      > If I change the types of the variables I think the result stays the same
      > (but I am not 100% sure)...
      >
      > Jens
      >[/color]

      I don't get the same answer:
      TH009MA@th009ma-shl2-01 /cygdrive/d/temp
      $ cat junk.c
      #include <stdio.h>
      #include <stdlib.h>

      int main(void)
      {
      const double three_tenths = 0.3;
      const double seven_tenths = 0.7;
      const float f_three_tenths = 0.3;
      const float f_seven_tenths = 0.7;

      printf("direct multiplication: 0.3 * 0.7 = %f\n", 0.3 * 0.7);
      printf("double * double: %f\n", three_tenths * seven_tenths);
      printf("float * float: %f\n", f_three_tenths * f_seven_tenths) ;
      printf("double * float: %f\n", three_tenths * f_seven_tenths) ;
      printf("float * double: %f\n", f_three_tenths * seven_tenths);
      return EXIT_SUCCESS;
      }

      TH009MA@th009ma-shl2-01 /cygdrive/d/temp
      $ gcc --version
      gcc (GCC) 3.3.1 (cygming special)
      Copyright (C) 2003 Free Software Foundation, Inc.
      This is free software; see the source for copying conditions. There is NO
      warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


      TH009MA@th009ma-shl2-01 /cygdrive/d/temp
      $ ./junk
      direct multiplication: 0.3 * 0.7 = 0.210000
      double * double: 0.210000
      float * float: 0.210000
      double * float: 0.210000
      float * double: 0.210000

      *************** *************** *************** ***

      TH009MA@th009ma-shl2-01 /cygdrive/d/temp
      $ bcc32 junk.c
      Borland C++ 5.6 for Win32 Copyright (c) 1993, 2002 Borland
      junk.c:
      Turbo Incremental Link 5.60 Copyright (c) 1997-2002 Borland

      TH009MA@th009ma-shl2-01 /cygdrive/d/temp
      $ ./junk
      direct multiplication: 0.3 * 0.7 = 0.210000
      double * double: 0.210000
      float * float: 0.210000
      double * float: 0.210000
      float * double: 0.210000


      --
      Thomas Matthews

      C++ newsgroup welcome message:

      C++ Faq: http://www.parashift.com/c++-faq-lite
      C Faq: http://www.eskimo.com/~scs/c-faq/top.html
      alt.comp.lang.l earn.c-c++ faq:

      Other sites:
      http://www.josuttis.com -- C++ STL Library book

      Comment

      • Karl Heinz Buchegger

        #4
        Re: Problems with multiplications of doubles and/or floats

        "J.K. Becker" wrote:[color=blue]
        >
        > Hi there,
        >
        > I am trying to multiply doubles with floats (actually I tried every
        > possible combination by now) and it never works (well, it does something
        > but it is always wrong). I have no idea what it is and where to look for
        > help, maybe some of you know?
        >
        > double=float*do uble; (or every possible combination of it). An example:
        >
        > 0.3 * 0.7 would result in 1.7 (with lots more digits). Anyone any idea?[/color]

        Yes. Post the program. You have a bug in it.

        --
        Karl Heinz Buchegger
        kbuchegg@gascad .at

        Comment

        • Rolf Magnus

          #5
          Re: Problems with multiplications of doubles and/or floats

          J.K. Becker wrote:
          [color=blue]
          > Hi there,
          >
          > I am trying to multiply doubles with floats (actually I tried every
          > possible combination by now) and it never works (well, it does
          > something but it is always wrong). I have no idea what it is and where
          > to look for help, maybe some of you know?
          >
          > double=float*do uble; (or every possible combination of it). An
          > example:
          >
          > 0.3 * 0.7 would result in 1.7 (with lots more digits). Anyone any
          > idea? If I change the types of the variables I think the result stays
          > the same (but I am not 100% sure)...[/color]

          Works here:

          #include <iostream>

          int main()
          {
          float a = 0.3;
          double b = 0.7;

          std::cout << a << " * " << b << " = " << a * b << '\n';
          }

          Output:

          0.3 * 0.7 = 0.21

          Comment

          • John Harrison

            #6
            Re: Problems with multiplications of doubles and/or floats


            "J.K. Becker" <jkbecker@becke r.de> wrote in message
            news:c5jfaq$reo $1@news1.zdv.un i-mainz.de...[color=blue]
            > Hi there,
            >
            > I am trying to multiply doubles with floats (actually I tried every
            > possible combination by now) and it never works (well, it does something
            > but it is always wrong). I have no idea what it is and where to look for
            > help, maybe some of you know?
            >
            > double=float*do uble; (or every possible combination of it). An example:
            >
            > 0.3 * 0.7 would result in 1.7 (with lots more digits). Anyone any idea?[/color]

            No that is impossible, you have a bug somewhere else in your program. Since
            you didn't post the program its impossible to help. Post the code.

            What else did you expect, did you think someone would say 'oh yes that's the
            well known 0.7 * 0.3 bug, it's been fixed in the new compiler version'?
            Compilers do not make basic arithmetic errors. If you want help with a buggy
            program you have to post the program code, there is no other way.

            See the FAQ http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

            john


            Comment

            • Matt

              #7
              Re: Problems with multiplications of doubles and/or floats

              J.K. Becker wrote:[color=blue]
              > Hi there,
              >
              > I am trying to multiply doubles with floats (actually I tried every
              > possible combination by now) and it never works (well, it does something
              > but it is always wrong). I have no idea what it is and where to look for
              > help, maybe some of you know?
              >
              > double=float*do uble; (or every possible combination of it). An example:
              >
              > 0.3 * 0.7 would result in 1.7 (with lots more digits). Anyone any idea?
              > If I change the types of the variables I think the result stays the same
              > (but I am not 100% sure)...
              >
              > Jens
              >[/color]

              The problem is in your print statement or output statement.

              man fprintf.

              Comment

              • J.K. Becker

                #8
                Re: Problems with multiplications of doubles and/or floats

                Oh well, you got it all wrong! This stupid calculation was just an
                example, it does not matter what the numbers are, the result is always
                wrong. When I asked for help I was looking for something in the line of
                "you could have a memory leak" (of course I checked that already) or
                "this always happens on sunny days with gcc3.3, try gcc0.1prealpha" or
                something.
                The program is far to long to post here, if you really want to look at
                it have a look at the working parts first here

                Download Elle Microstructural Modelling for free. The goal of the Elle Project is to provide a general purpose modelling environment for the simulation of rock microstructure evolution. The code is based around a unified description of 2D microstructure.


                or a description with links to all kinds of stuff there:



                And now that you know that I am not completely new to c++, no more rtfm,
                or man printf, I think I am past that by now...

                Jens

                J.K. Becker wrote:[color=blue]
                > Hi there,
                >
                > I am trying to multiply doubles with floats (actually I tried every
                > possible combination by now) and it never works (well, it does something
                > but it is always wrong). I have no idea what it is and where to look for
                > help, maybe some of you know?
                >
                > double=float*do uble; (or every possible combination of it). An example:
                >
                > 0.3 * 0.7 would result in 1.7 (with lots more digits). Anyone any idea?
                > If I change the types of the variables I think the result stays the same
                > (but I am not 100% sure)...
                >
                > Jens
                >[/color]

                Comment

                • John Harrison

                  #9
                  Re: Problems with multiplications of doubles and/or floats


                  "J.K. Becker" <jkbecker@becke r.de> wrote in message
                  news:c5lglu$ul3 $1@news1.zdv.un i-mainz.de...[color=blue]
                  > Oh well, you got it all wrong! This stupid calculation was just an
                  > example, it does not matter what the numbers are, the result is always
                  > wrong. When I asked for help I was looking for something in the line of
                  > "you could have a memory leak" (of course I checked that already) or
                  > "this always happens on sunny days with gcc3.3, try gcc0.1prealpha" or
                  > something.
                  > The program is far to long to post here, if you really want to look at
                  > it have a look at the working parts first here
                  >
                  > https://sourceforge.net/projects/elle/
                  >
                  > or a description with links to all kinds of stuff there:
                  >
                  > http://www.microstructure.uni-tuebin...enix/index.php
                  >
                  > And now that you know that I am not completely new to c++, no more rtfm,
                  > or man printf, I think I am past that by now...
                  >[/color]

                  Well how was anyone supposed to know that? We get lots of clueless newbies
                  posting here. One reason for posting source code is that it makes it easy
                  for people to assess your level of expertise.

                  The answer is still to post source code. Take your large program and cut out
                  the irrelevant parts. When doing this one of two things will happen. You may
                  remove some code you don't think is relevant and the problem goes away, this
                  is a good clue as to where the bug is. The other thing that might happen is
                  that you get down to a smallish program that still exhibits the behaviour
                  you don't understand, then you can post that code here and someone will fix
                  it.

                  This is all explained in the FAQ



                  Take the trouble, and you'll get your problem fixed.

                  john


                  Comment

                  • J.K. Becker

                    #10
                    Re: Problems with multiplications of doubles and/or floats

                    I can't take the program apart, that would be more work than
                    reprograming the part that is not working. There is a reason for me not
                    to post the progam here, you know, believe me, if I could I would. I can
                    post the function here, but that is not going to help.
                    If you insist I will.



                    John Harrison wrote:[color=blue]
                    > "J.K. Becker" <jkbecker@becke r.de> wrote in message
                    > news:c5lglu$ul3 $1@news1.zdv.un i-mainz.de...
                    >[color=green]
                    >>Oh well, you got it all wrong! This stupid calculation was just an
                    >>example, it does not matter what the numbers are, the result is always
                    >>wrong. When I asked for help I was looking for something in the line of
                    >>"you could have a memory leak" (of course I checked that already) or
                    >>"this always happens on sunny days with gcc3.3, try gcc0.1prealpha" or
                    >>something.
                    >>The program is far to long to post here, if you really want to look at
                    >>it have a look at the working parts first here
                    >>
                    >>https://sourceforge.net/projects/elle/
                    >>
                    >>or a description with links to all kinds of stuff there:
                    >>
                    >>http://www.microstructure.uni-tuebin...enix/index.php
                    >>
                    >>And now that you know that I am not completely new to c++, no more rtfm,
                    >>or man printf, I think I am past that by now...
                    >>[/color]
                    >
                    >
                    > Well how was anyone supposed to know that? We get lots of clueless newbies
                    > posting here. One reason for posting source code is that it makes it easy
                    > for people to assess your level of expertise.
                    >
                    > The answer is still to post source code. Take your large program and cut out
                    > the irrelevant parts. When doing this one of two things will happen. You may
                    > remove some code you don't think is relevant and the problem goes away, this
                    > is a good clue as to where the bug is. The other thing that might happen is
                    > that you get down to a smallish program that still exhibits the behaviour
                    > you don't understand, then you can post that code here and someone will fix
                    > it.
                    >
                    > This is all explained in the FAQ
                    >
                    > http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
                    >
                    > Take the trouble, and you'll get your problem fixed.
                    >
                    > john
                    >
                    >[/color]

                    Comment

                    • John Harrison

                      #11
                      Re: Problems with multiplications of doubles and/or floats


                      "J.K. Becker" <jkbecker@becke r.de> wrote in message
                      news:c5lhod$umn $1@news1.zdv.un i-mainz.de...[color=blue]
                      > I can't take the program apart, that would be more work than
                      > reprograming the part that is not working. There is a reason for me not
                      > to post the progam here, you know, believe me, if I could I would. I can
                      > post the function here, but that is not going to help.
                      > If you insist I will.
                      >[/color]

                      I'm not insisting on anything, just explaining how this group works. The
                      regulars here contribute a lot of their time for no recompense. I don't
                      think any have the time to take on the sort of in depth study that it sounds
                      like your problem needs.

                      You might have got lucky with you post and someone have recognised the
                      problem but you didn't so I guess there is no alternative but some hard work
                      on your part.

                      Post the function if you like, you never know it might help. One of the
                      things I've learned from this group is that posters are often convinced that
                      a problem is in some piece of code which they post (even though they don't
                      know what the problem is) but when after much resistence that are persuaded
                      to post more code, the problem turns out to have been in some part of the
                      code they were holding back.

                      John

                      BTW top posting is frowned on in this group.


                      Comment

                      • Bruce Clement

                        #12
                        Re: Problems with multiplications of doubles and/or floats

                        J.K. Becker wrote:[color=blue]
                        > [...]
                        >
                        > J.K. Becker wrote:
                        > [...][color=green]
                        >> double=float*do uble; (or every possible combination of it). An example:
                        >>
                        >> 0.3 * 0.7 would result in 1.7 (with lots more digits). Anyone any
                        >> idea? If I change the types of the variables I think the result stays
                        >> the same (but I am not 100% sure)...
                        >>
                        >> Jens
                        >>[/color]
                        >[/color]

                        If I'm understanding the problem correctly, this isn't a C++ problem, but an
                        implementation artifact.

                        Let's say that in your implementation, float has 6 digits of precision & double
                        has 12, so 0.3 is 0.300000, and 0.7 is 0.700000000000. These aren't integers
                        though, they are finite precision representations of real numbers, so 0.3 is
                        actually any number between 0.299999500000 and 0.300000499999. When the
                        generated code, or, more likely, the computer's Floating Point Hardware (FPU),
                        converts the 0.3 from float to double, it is perfectly entitled to convert it to
                        anything within its valid range.

                        Most people thing in base ten, so we intuitively expect that the 0.300000 will
                        be seamlessly converted into 0.300000000000. The FPU is probably constructed as
                        a binary device, and so it will still zero fill, but in binary. When converted
                        back to decimal, this gives some value that is close to, but not, exactly
                        0.300000000000. The value it gives is still correct though.

                        You can see how your compiler / FPU handle these conversions by single stepping
                        through this fragment.

                        float x = 0.3;

                        int main( int, char ** )
                        {
                        some_external_f n( &x ); // stop the optimizer making assumptions about x
                        double y = x;
                        std::cout << y << std::eol;
                        return 0;
                        }

                        Regards

                        Bruce

                        Comment

                        • J.K. Becker

                          #13
                          Re: Problems with multiplications of doubles and/or floats

                          John Harrison wrote:[color=blue]
                          > "J.K. Becker" <jkbecker@becke r.de> wrote in message
                          > news:c5lhod$umn $1@news1.zdv.un i-mainz.de...
                          >[color=green]
                          >>I can't take the program apart, that would be more work than
                          >>reprogramin g the part that is not working. There is a reason for me not
                          >>to post the progam here, you know, believe me, if I could I would. I can
                          >>post the function here, but that is not going to help.
                          >>If you insist I will.
                          >>[/color]
                          >
                          >
                          > I'm not insisting on anything, just explaining how this group works. The
                          > regulars here contribute a lot of their time for no recompense. I don't
                          > think any have the time to take on the sort of in depth study that it sounds
                          > like your problem needs.
                          >
                          > You might have got lucky with you post and someone have recognised the
                          > problem but you didn't so I guess there is no alternative but some hard work
                          > on your part.
                          >
                          > Post the function if you like, you never know it might help. One of the
                          > things I've learned from this group is that posters are often convinced that
                          > a problem is in some piece of code which they post (even though they don't
                          > know what the problem is) but when after much resistence that are persuaded
                          > to post more code, the problem turns out to have been in some part of the
                          > code they were holding back.
                          >
                          > John
                          >
                          > BTW top posting is frowned on in this group.
                          >
                          >[/color]

                          I know that it is not a simple error (well, it might be but I have no
                          idea where). The program consits of, I don't know, 10000 lines or more,
                          something like 100 and more seperate files. And to make it more fun, it
                          has grown over the years so some files are c++ (OO), some are c++ (not
                          OO), some c and some fortran. And it has been written by people in the
                          US, Australia, UK, Germany (that's me) and France. So no way to strip
                          that down to something everybody can understand quickly.
                          But since we all are running out of ideas on what and where to look, I
                          thought I'd try here.

                          Jens

                          Comment

                          • J.K. Becker

                            #14
                            Re: Problems with multiplications of doubles and/or floats

                            This is the function where the error occurs. As you can see there are a
                            lot of calculations in it, they all work fine except the one mentioned
                            (which seems to be the easiest one).

                            Fx = un.x * dEdp;
                            Fy = un.y * dEdp;

                            This always gives the wrong results... Everything before it works and
                            everything after it works too (except that everything is calculated with
                            the wrong values for Fx and Fy).


                            But I guess it is too complex for a quick fix...


                            Thanks anyway

                            Jens


                            int GBE_MoveNode( int index, Coords * movedir )
                            {
                            int i, l, k, moveflag = 0;
                            double E1, E2, E3, E4, vangle[3],lenL[3] , lenK[3];
                            double truetimestep = 3.1536e10;
                            float Fx,Fy;
                            double lenP, lenV, lenF, mobility1, mobility2, mobility3 , switchd;
                            Coords p1, p2, gvector, un, sigma1, sigma2, sigma3, L[3], F;
                            Coords newxy, xynb, xy, prev, V;
                            float dEdp;
                            int nb[3];
                            char cc[255];

                            mobility1 = 1e-12;
                            mobility2 = 1e-12;
                            mobility3 = 1e-12;

                            switchd = ElleSwitchdista nce() / 10;
                            //Get position of first node
                            ElleNodePositio n( index, & p1 );
                            ElleNodePrevPos ition( index, & prev );

                            //this is the first energy we need: E((x+dx),y)
                            p2.x = p1.x + switchd;
                            p2.y = p1.y;
                            ElleSetPosition ( index, & p2 );
                            GetNodeEnergy( index, & p2, & E1 );

                            //this is the second energy we need:E((x-dx),y)
                            p2.x = p1.x - switchd;
                            p2.y = p1.y;
                            ElleSetPosition ( index, & p2 );
                            GetNodeEnergy( index, & p2, & E2 );

                            //this is the third energy we need: E(x,(y+dy))
                            p2.x = p1.x;
                            p2.y = p1.y + switchd;
                            ElleSetPosition ( index, & p2 );
                            GetNodeEnergy( index, & p2, & E3 );

                            //this is the fourth energy we need: E(x,(y-dy))
                            p2.x = p1.x;
                            p2.y = p1.y - switchd;
                            ElleSetPosition ( index, & p2 );
                            GetNodeEnergy( index, & p2, & E4 );

                            //Reset node position to starting values
                            ElleSetPosition ( index, & p1 );
                            ElleSetPrevPosi tion( index, & prev );

                            //So now we can calculate the gradient vector (P)
                            gvector.x = ( E1 - E2 ) / ( 2 * switchd );
                            gvector.y = ( E3 - E4 ) / ( 2 * switchd );
                            if ( gvector.x == 0.0 && gvector.y == 0.0 )
                            {
                            movedir->x = 0.0;
                            movedir->y = 0.0;
                            return ( 0 );
                            }
                            else
                            {
                            //reverse gradient
                            gvector.x *= -1;
                            gvector.y *= -1;
                            //length of gradient
                            lenP = sqrt( ( gvector.x * gvector.x ) + ( gvector.y * gvector.y ) );
                            //unit vector
                            un.x = gvector.x / lenP;
                            un.y = gvector.y / lenP;
                            dEdp = ( ( gvector.x / gvector.y ) + ( gvector.y / gvector.x ) ) /
                            lenP;

                            ////////
                            //////// wrong results in this calculation
                            ////////
                            Fx = un.x * dEdp;
                            Fy = un.y * dEdp;
                            ////////
                            ////////
                            ////////

                            lenF = sqrt( ( Fx * Fx ) + ( Fy * Fy ) );
                            ElleNeighbourNo des( index, nb );
                            for ( i = 0, k = 0; i < 3; i++ )
                            {
                            if ( nb[i] != NO_NB && ElleNodeIsActiv e( index ) )
                            {
                            ElleNodePositio n( nb[i], & xynb );
                            L[k].x = p1.x - xynb.x;
                            L[k].y = p1.y - xynb.y;
                            lenL[k] = sqrt( ( L[k].x * L[k].x ) + ( L[k].y * L[k].y ) );
                            vangle[k] = fabs(90- ( acos( ( Fx * L[k].x + Fy * L[k].y ) / (
                            lenL[k] * lenF ) ) ));
                            lenK[k] = lenL[k] * fabs( sin( vangle[k] * ( 180 / 3.1415926535
                            ) ) );
                            k++;
                            }
                            }
                            sigma1.x = Fx / ( lenK[0] + ( mobility1 / mobility2 ) * lenK[1] );
                            sigma1.y = Fy / ( lenK[0] + ( mobility1 / mobility2 ) * lenK[1] );
                            V.x = sigma1.x * (mobility1/switchd/switchd);
                            V.y = sigma1.y * (mobility1/switchd/switchd);
                            lenV = sqrt( ( V.x * V.x ) + ( V.y * V.y ) );
                            movedir->x = p1.x - V.x;
                            movedir->y = p1.y - V.y;
                            moveflag = 1;
                            return ( moveflag );
                            }
                            }

                            Comment

                            • J.K. Becker

                              #15
                              Re: Problems with multiplications of doubles and/or floats

                              Bruce Clement wrote:
                              [color=blue]
                              > If I'm understanding the problem correctly, this isn't a C++ problem,
                              > but an implementation artifact.
                              >
                              > Let's say that in your implementation, float has 6 digits of precision &
                              > double has 12, so 0.3 is 0.300000, and 0.7 is 0.700000000000. These
                              > aren't integers though, they are finite precision representations of
                              > real numbers, so 0.3 is actually any number between 0.299999500000 and
                              > 0.300000499999. When the generated code, or, more likely, the computer's
                              > Floating Point Hardware (FPU), converts the 0.3 from float to double, it
                              > is perfectly entitled to convert it to anything within its valid range.
                              >
                              > Most people thing in base ten, so we intuitively expect that the
                              > 0.300000 will be seamlessly converted into 0.300000000000. The FPU is
                              > probably constructed as a binary device, and so it will still zero fill,
                              > but in binary. When converted back to decimal, this gives some value
                              > that is close to, but not, exactly 0.300000000000. The value it gives is
                              > still correct though.
                              >
                              > You can see how your compiler / FPU handle these conversions by single
                              > stepping through this fragment.
                              >
                              > float x = 0.3;
                              >
                              > int main( int, char ** )
                              > {
                              > some_external_f n( &x ); // stop the optimizer making assumptions
                              > about x
                              > double y = x;
                              > std::cout << y << std::eol;
                              > return 0;
                              > }
                              >
                              > Regards
                              >
                              > Bruce
                              >[/color]

                              I thought about that too, but the error is to big to be a rounding
                              problem (well I think). 0.3 *0.7 can give something 0.2100012383897 41291
                              (just an example) which would be fine with me, but not 1.72094230948.
                              The error is just too big. It is interesting to note though that it
                              always calculates the same so it does not multiply arbitrary numbers
                              that it gets from memory somewhere, but always the same numbers....

                              Jens

                              Comment

                              Working...