Double to string conversion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin

    #16
    Re: Integer power function and multidimensiona l arrays as functionparamet ers

    Ivan Vecerina wrote:[color=blue]
    > "Martin" <lillierom@cti. ecp.fr> wrote in message
    > news:dtvclg$l2d $1@smilodon.ecp .fr...
    > : Tomás wrote:
    > : > Martin posted:
    > : >
    > : >
    > : >>I want to raise a long integer to another long integer and have the
    > : >>result as a long integer.
    > : >
    > : >
    > : > Why not just write a function. Potentially buggy code:
    > : >
    > : >
    > : > /*************** ************
    > : > Undefined Behaviour if
    > : > either argument is zero
    > : > *************** ************/
    > : >
    > : > unsigned long RaisePowerInteg er(unsigned long a, unsigned long b)
    > : > {
    > : > unsigned long value = a;
    > : >
    > : > for ( unsigned long i = 0; i < b; ++i )
    > : > {
    > : > value *= a;
    > : > }
    > : >
    > : > return value;
    > : > }
    > :
    > : Good idea, thanks!
    >
    > Well, only if you can afford making a lot of multiplications
    > ( the above has linear complexity w.r.t. b ).
    > Logarithmic complexity can be achieved with little more
    > effort:
    >
    > unsigned long RaisePowerInteg er(unsigned long a, unsigned long b)
    > {
    > unsigned long result = 1;
    > for( ; b > 0 ; b>>=1 )
    > {
    > if(b&1) result *= a;
    > a *= a;
    > }
    > return result;
    > }
    >
    > Further optimization is possible, but that's an easy start.
    >
    >
    > I hope this helps,
    > Ivan[/color]

    Great, thanks!

    Martin

    Comment

    • andy@servocomm.freeserve.co.uk

      #17
      Re: Integer power function and multidimensiona l arrays as function parameters

      Martin wrote:[color=blue]
      > I want to raise a long integer to another long integer and have the
      > result as a long integer. Is there a nice way to do this, or do I have
      > to use this approach:
      > (long)pow((doub le)some_long_in teger,(int)some _other_long_int eger)
      > ?
      > It seems very unnecessary and risky (in terms of potential data loss).[/color]

      FWIW I wrote various pow functions which can be either evaluated at
      compile time or easily optimised to a constant.

      //Easily optimisable pow function

      template <unsigned int Exp>
      inline long pow( int base)
      {
      return base * pow<Exp-1>(base);
      }

      template<>
      inline long pow<0>(int base) { return 1;}

      int main()
      {
      std::cout << pow<6>(10) <<'\n';
      std::cout << pow<0>(10) <<'\n';
      std::cout << pow<7>(2) << '\n';
      std::cout << pow<32>(0x100) << '\n';
      std::cout << pow<2>(0x100) <<'\n';
      }

      //compile time pow

      template<typena me Ta, Ta A,long B>
      struct power;

      template<typena me Ta , Ta A>
      struct power<Ta,A,0>{
      static const Ta value = 1;
      };

      template<typena me Ta, Ta A,long B>
      struct power{
      static const int value = power<Ta,A,B-1>::value*A;
      };

      int main()
      {

      std::cout << power<int,2,2>: :value <<'\n';
      }

      It would be possible to make a constant time pow function with both
      values runtime modifiable using the techniques at:


      regards
      Andy Little

      Comment

      • BobR

        #18
        Re: Multidimensiona l arrays as function parameters


        Martin wrote in message ...[color=blue]
        >Gavin Deane wrote:[color=green]
        >> Martin wrote:[color=darkred]
        >>>------------------------------------------
        >>>Is it much easier to use nested vectors (I suppose you mean the Vector
        >>>class?) as function parameters, than multidimensiona l arrays?[/color]
        >>
        >> Yes.
        >>
        >> #include <vector>
        >>
        >> typedef std::vector<std ::vector<std::v ector<int> > > vec3d;
        >>
        >> void foo(const vec3d& v){
        >> // ...
        >> }
        >>
        >> int main(){
        >> vec3d bar; // Use appropriate constructor to make the vectors the
        >> right size.
        >> foo(bar);
        >> }
        >>
        >> Also, any memory management and the associated exception safety is
        >> taken care of for you.
        >> Gavin Deane
        >>[/color]
        >
        >Thanks Gavin! I learned a lot from your answers. I used compile-time
        >constants which worked well.
        >
        >As for the nested vector approach, I'm a little curious to know how one
        >uses an appropriate constructor to make the vectors the right size. Is
        >it complicated?
        >Martin[/color]


        This is what I came up with to init a 5x5x5 all set to 7.
        Q(for others): Is there any simpler way to init this vector 'structure'? [ I
        know a 2D 5x5 can be 'sized' with vector<vector<T > > vec2D(5, 5); ]. This
        example looks 'butt ugly', but appears to work.


        typedef std::vector<std ::vector<std::v ector<int> > > vec3d;

        int main(){
        // [ assume 'using std::vector;' in order to shorten line.]

        vec3d vec3D( 5, vector<vector<i nt> >( 5, vector<int>( 5, int(7) ) ) );

        cout<<"\n vec3D.at(0).at( 0).size()= "<<vec3D.at(0). at(0).size();
        cout<<"\n vec3D.at(0).at( 0).at(0)= "<<vec3D.at(0). at(0).at(0)
        <<std::endl;
        // ......
        } // main()end

        // -- output --
        // vec3D.at(0).at( 0).size()= 5
        // vec3D.at(0).at( 0).at(0)= 7

        --
        Bob R
        POVrookie


        Comment

        • Fei Liu

          #19
          Re: Multidimensiona l arrays as function parameters


          Martin wrote:[color=blue]
          > Code snippet:
          > ------------------------------------------
          > double MC(double K,
          > double S[],
          > double v[],
          > double d[],
          > double r,
          > double exDates[],
          > const unsigned long DIMENSIONS,
          > const unsigned long GRIDPOINTS,
          > double frontiers[][DIMENSIONS][GRIDPOINTS], // Here's the problem
          > double gridStep[],
          > unsigned long evaldate,
          > unsigned long NumberOfPaths)
          > ------------------------------------------
          >
          > The reason for not using vectors of vectors of vectors instead of
          > threedimensiona l arrays is that the code is intended for parallel
          > computing, currently using MPI, and a colleague encountered difficulties
          > sending vectors between nodes. Perhaps there's a way around that though.
          >
          > Is it much easier to use nested vectors (I suppose you mean the Vector
          > class?) as function parameters, than multidimensiona l arrays?
          >
          > This is my first program in C/C++.
          >
          > Martin[/color]

          I believe std::valarray is what you want.

          Comment

          • Gavin Deane

            #20
            Re: Multidimensiona l arrays as function parameters


            Martin wrote:[color=blue]
            > Gavin Deane wrote:[color=green]
            > > Martin wrote:[color=darkred]
            > >>Is it much easier to use nested vectors (I suppose you mean the Vector
            > >>class?) as function parameters, than multidimensiona l arrays?[/color]
            > >
            > > Yes.
            > >
            > > #include <vector>
            > >
            > > typedef std::vector<std ::vector<std::v ector<int> > > vec3d;
            > >
            > > void foo(const vec3d& v)
            > > {
            > > // ...
            > > }
            > >
            > > int main()
            > > {
            > > vec3d bar; // Use appropriate constructor to make the vectors the
            > > right size.
            > > foo(bar);
            > > }
            > >
            > > Also, any memory management and the associated exception safety is
            > > taken care of for you.[/color]
            >
            > As for the nested vector approach, I'm a little curious to know how one
            > uses an appropriate constructor to make the vectors the right size. Is
            > it complicated?[/color]

            I guess complicated is in the eye of the beholder. The construction
            syntax is quite cumbersome if you've not seen it before. But what's
            going on isn't too complicated, and the parameter passing syntax is a
            lot simpler - no need to put the array sizes in the function
            declaration at all (so avoiding the risk of making a mistake by, for
            example, getting them the wrong way round or using the wrong values
            entirely).

            #include <vector>

            typedef std::vector<std ::vector<std::v ector<int> > > vec3d;
            const int dimensions = 5;
            const int grid_points = 42;
            const int the_third_one = 7;

            int main()
            {
            vec3d bar(dimensions, std::vector<std ::vector<int> >(grid_points ,
            std::vector<int >(the_third_one )));
            }
            [color=blue][color=green]
            > > It sounds like you may have a good reason to avoid std::vector in this
            > > case, but it's generally good practice to use the standard library
            > > tools in preference to hand written equivalents when you can. They are
            > > designed to make your life easier and they achieve that.[/color]
            >
            > Do you mean that nested vectors are more standard than multidimensiona l
            > arrays?[/color]

            Not at all. Multideimension al arrays are part of the language and
            std::vector is part of the library. Both are standard. The point is
            that, if the standard library provides some tool that does what you
            need, you are better of using it than writing your own equivalent code.
            The standard library implementation will be well tested, familiar
            (hopefully) to other C++ programmers working on you code, and should
            lead you more quickly to a clear and correct program.

            As a reference, this book is worth it's weight in gold IMO.


            Gavin Deane

            Comment

            • BobR

              #21
              Re: Multidimensiona l arrays as function parameters


              Gavin Deane wrote in message
              <1141125801.914 407.267070@t39g 2000cwt.googleg roups.com>...[color=blue]
              >
              >#include <vector>
              >typedef std::vector<std ::vector<std::v ector<int> > > vec3d;
              >const int dimensions = 5;
              >const int grid_points = 42;
              >const int the_third_one = 7;[/color]

              Small (somewhat) safety issue; Those should be unsigned, not int.

              const int dimensions = -5; // ouch, a HUGE size vector.
              // vector ctor interprets it as unsigned.

              const size_t dimensions = -5; // let the compiler warn first.
              // I guess 'std::vector::s ize_type' would be even better (?).

              to OP: the 'const' isn't required here, just good practice.
              size_t dimensions( 5 );
              std::vector<int > vecA(dimensions , int(42));
              dimensions = 22;
              std::vector<int > vecB(dimensions );
              [color=blue]
              >
              >int main(){
              > vec3d bar(dimensions, std::vector<std ::vector<int> >(grid_points ,
              >std::vector<in t>(the_third_on e)));[/color]

              // I guess that answers my question in my other post. Thanks!
              [color=blue]
              >}
              >
              >Gavin Deane[/color]

              Just a thought.
              --
              Bob R
              POVrookie


              Comment

              • Gavin Deane

                #22
                Re: Multidimensiona l arrays as function parameters


                BobR wrote:[color=blue]
                > Gavin Deane wrote in message
                > <1141125801.914 407.267070@t39g 2000cwt.googleg roups.com>...[color=green]
                > >
                > >#include <vector>
                > >typedef std::vector<std ::vector<std::v ector<int> > > vec3d;
                > >const int dimensions = 5;
                > >const int grid_points = 42;
                > >const int the_third_one = 7;[/color]
                >
                > Small (somewhat) safety issue; Those should be unsigned, not int.
                >
                > const int dimensions = -5; // ouch, a HUGE size vector.
                > // vector ctor interprets it as unsigned.
                >
                > const size_t dimensions = -5; // let the compiler warn first.
                > // I guess 'std::vector::s ize_type' would be even better (?).[/color]

                The vector constructor expects std::vector::si ze_type so that would be
                the best choice, assuming you don't think you might be changing the
                container to a deque or something later.
                [color=blue]
                > to OP: the 'const' isn't required here, just good practice.
                > size_t dimensions( 5 );
                > std::vector<int > vecA(dimensions , int(42));
                > dimensions = 22;
                > std::vector<int > vecB(dimensions );[/color]

                Yes, another advantage of vectors is that you don't need to know the
                sizes at compile time. But the discussion with the OP so far had been
                around the concept of compile time constants. I just didn't want to add
                too much information and risk confusion.

                Gavin Deane

                Comment

                Working...