C++ Primer ex 7.3

Discussion in 'C++' started by arnuld, Aug 9, 2007.

  1. arnuld

    arnuld Guest

    i wanted to know if that's a good design style (upto the knowledge i have
    gained till chpater 7):

    /* C++ Primer - 4/e
    *
    * exercise 7.4
    * STATEMENT
    * write a programme to take two int paramaters and generate the
    result of raising the first to the power of the second. write a
    programme to call your function passing it two ints. verify the
    result.
    *
    */

    #include <iostream>

    int raise_power(int x, int y)
    {
    int mult = 1;

    while( y )
    {
    mult = mult * x;
    --y;
    }

    return mult;
    }

    int main()
    {
    int i = 2;
    int j = 3;
    int mult_result = 8;

    std::cout << static_cast<bool>(mult_result == raise_power(i, j))
    << std::endl;
    /* well, never got converted to "true", it alwasy remains 1 :( */
    return 0;
    }




    --
    http://arnuld.blogspot.com
     
    arnuld, Aug 9, 2007
    #1
    1. Advertising

  2. arnuld

    Guest

    On Aug 8, 9:59 pm, arnuld <> wrote:
    > i wanted to know if that's a good design style (upto the knowledge i have
    > gained till chpater 7):
    >
    > /* C++ Primer - 4/e
    > *
    > * exercise 7.4
    > * STATEMENT
    > * write a programme to take two int paramaters and generate the
    > result of raising the first to the power of the second. write a
    > programme to call your function passing it two ints. verify the
    > result.
    > *
    > */
    >
    > #include <iostream>
    >
    > int raise_power(int x, int y)


    I would suggest using more meaningful variable names other than 'x'
    and 'y'. 'base' and 'exponent' come to mind.

    > {
    > int mult = 1;
    >
    > while( y )


    If 'y' is a negative number (which is allowed since it's defined as an
    int), this while condition will cause a major bug n your program.

    > {
    > mult = mult * x;
    > --y;


    Your while condition is wrong. But if it was correct, you could have
    simply written "while( y-- )" to achieve the same result without the
    stand-alone decrement.

    > }
    >
    > return mult;
    >
    > }
    >
    > int main()
    > {
    > int i = 2;
    > int j = 3;
    > int mult_result = 8;
    >
    > std::cout << static_cast<bool>(mult_result == raise_power(i, j))
    > << std::endl;
    > /* well, never got converted to "true", it alwasy remains 1 :( */
    > return 0;
    >
    > }
    >
    > --http://arnuld.blogspot.com



    Cheers,
    Andre
     
    , Aug 9, 2007
    #2
    1. Advertising

  3. arnuld

    Ian Collins Guest

    arnuld wrote:
    > i wanted to know if that's a good design style (upto the knowledge i have
    > gained till chpater 7):
    >
    > /* C++ Primer - 4/e
    > *
    > * exercise 7.4
    > * STATEMENT
    > * write a programme to take two int paramaters and generate the
    > result of raising the first to the power of the second. write a
    > programme to call your function passing it two ints. verify the
    > result.
    > *
    > */
    >
    > #include <iostream>
    >
    > int raise_power(int x, int y)
    > {
    > int mult = 1;
    >
    > while( y )
    > {
    > mult = mult * x;
    > --y;
    > }
    >
    > return mult;
    > }
    >

    That's OK for positive numbers, but what about negative?

    > int main()
    > {
    > int i = 2;
    > int j = 3;
    > int mult_result = 8;
    >
    > std::cout << static_cast<bool>(mult_result == raise_power(i, j))
    > << std::endl;


    Be brutal,

    assert( raise_power(i, j) == mult_result );

    Don't forget to include <cassert>

    --
    Ian Collins.
     
    Ian Collins, Aug 9, 2007
    #3
  4. arnuld

    arnuld Guest

    > On Aug 9, 10:14 am, Ian Collins <> wrote:
    > > arnuld wrote:


    > > ...[SNIP]...


    > > std::cout << static_cast<bool>(mult_result == raise_power(i, j))
    > > << std::endl;



    > Be brutal,
    >
    > assert( raise_power(i, j) == mult_result );
    >
    > Don't forget to include <cassert>


    that raises an error:

    /home/arnuld/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra
    ex_07-03.cpp
    ex_07-03.cpp: In function 'int main()':
    ex_07-03.cpp:32: error: no match for 'operator<<' in 'std::cout <<
    ((raise_power(i, ((unsigned int)j)) != mult_result) ?
    __assert_fail(((const char*)"mult_result == raise_power(i, j)"),
    ((const char*)"ex_07-03.cpp"), 32u, ((const char*)(&
    __PRETTY_FUNCTION__))) : 0)'
    /usr/lib/gcc/i686-pc-linux-gnu/4.2.1/../../../../include/c++/4.2.1/
    ostream:112: note: candidates are: std::basic_ostream<_CharT,
    _Traits>& std::basic_ostream<_CharT,
    _Traits>::eek:perator<<(std::basic_ostream<_CharT, _Traits>& (*)
    (std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits =
    std::char_traits<char>]


    line 32, is where i use "assert".
     
    arnuld, Aug 9, 2007
    #4
  5. arnuld

    arnuld Guest

    > On Aug 9, 10:21 am, arnuld <> wrote:
    > that raises an error:
    >
    > /home/arnuld/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra
    > ex_07-03.cpp
    > ex_07-03.cpp: In function 'int main()':
    > ex_07-03.cpp:32: error: no match for 'operator<<' in 'std::cout <<
    > ((raise_power(i, ((unsigned int)j)) != mult_result) ?
    > __assert_fail(((const char*)"mult_result == raise_power(i, j)"),
    > ((const char*)"ex_07-03.cpp"), 32u, ((const char*)(&
    > __PRETTY_FUNCTION__))) : 0)'
    > /usr/lib/gcc/i686-pc-linux-gnu/4.2.1/../../../../include/c++/4.2.1/
    > ostream:112: note: candidates are: std::basic_ostream<_CharT,
    > _Traits>& std::basic_ostream<_CharT,
    > _Traits>::eek:perator<<(std::basic_ostream<_CharT, _Traits>& (*)
    > (std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits =
    > std::char_traits<char>]
    >
    > line 32, is where i use "assert".


    sorry, forgot to post code:

    std::cout << assert(mult_result == raise_power(i, j))
    << std::endl;
     
    arnuld, Aug 9, 2007
    #5
  6. arnuld

    Ian Collins Guest

    arnuld wrote:
    >> On Aug 9, 10:21 am, arnuld <> wrote:
    >> that raises an error:
    >>
    >> /home/arnuld/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra
    >> ex_07-03.cpp
    >> ex_07-03.cpp: In function 'int main()':
    >> ex_07-03.cpp:32: error: no match for 'operator<<' in 'std::cout <<
    >> ((raise_power(i, ((unsigned int)j)) != mult_result) ?
    >> __assert_fail(((const char*)"mult_result == raise_power(i, j)"),
    >> ((const char*)"ex_07-03.cpp"), 32u, ((const char*)(&
    >> __PRETTY_FUNCTION__))) : 0)'
    >> /usr/lib/gcc/i686-pc-linux-gnu/4.2.1/../../../../include/c++/4.2.1/
    >> ostream:112: note: candidates are: std::basic_ostream<_CharT,
    >> _Traits>& std::basic_ostream<_CharT,
    >> _Traits>::eek:perator<<(std::basic_ostream<_CharT, _Traits>& (*)
    >> (std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits =
    >> std::char_traits<char>]
    >>
    >> line 32, is where i use "assert".

    >
    > sorry, forgot to post code:
    >
    > std::cout << assert(mult_result == raise_power(i, j))
    > << std::endl;
    >
    >

    No, just use what I wrote.

    --
    Ian Collins.
     
    Ian Collins, Aug 9, 2007
    #6
  7. arnuld

    arnuld Guest

    > On Aug 9, 11:12 am, Ian Collins <> wrote:

    > No, just use what I wrote.



    i did use that, now i just modified it to:

    std::cout << std::assert( raise_power(i, j) == mult_result )
    << std::endl;

    and the error is:

    ex_07-03.cpp:32: error: expected unqualified-id before '(' token


    what now ?
     
    arnuld, Aug 9, 2007
    #7
  8. arnuld

    Ian Collins Guest

    arnuld wrote:
    >> On Aug 9, 11:12 am, Ian Collins <> wrote:

    >
    >> No, just use what I wrote.

    >
    >
    > i did use that, now i just modified it to:
    >
    > std::cout << std::assert( raise_power(i, j) == mult_result )
    > << std::endl;
    >
    > and the error is:
    >
    > ex_07-03.cpp:32: error: expected unqualified-id before '(' token
    >

    Don't output it, just use the assert to validate your test. If the
    assertion fails, your program will abort with an error message.

    --
    Ian Collins.
     
    Ian Collins, Aug 9, 2007
    #8
  9. arnuld

    arnuld Guest

    On Aug 9, 11:59 am, arnuld <> wrote:

    > i did use that, now i just modified it to:
    >
    > std::cout << std::assert( raise_power(i, j) == mult_result )
    > << std::endl;
    >
    > and the error is:
    >
    > ex_07-03.cpp:32: error: expected unqualified-id before '(' token
    >
    > what now ?


    ok i got it. "assert" is a preprocessor-macro that does not return
    anything and hence causes conflict with the overloaded operator "<<"

    but then i have to write more-code:

    assert( raise_power(i, j) == mult_result );
    std::cout << ( raise_power(i, j) == mult_result ) << std::endl;

    but then, is writing more code a good idea ? (when i know that
    expression works fine)
     
    arnuld, Aug 9, 2007
    #9
  10. arnuld

    Ian Collins Guest

    arnuld wrote:
    > On Aug 9, 11:59 am, arnuld <> wrote:
    >
    >> i did use that, now i just modified it to:
    >>
    >> std::cout << std::assert( raise_power(i, j) == mult_result )
    >> << std::endl;
    >>
    >> and the error is:
    >>
    >> ex_07-03.cpp:32: error: expected unqualified-id before '(' token
    >>
    >> what now ?

    >
    > ok i got it. "assert" is a preprocessor-macro that does not return
    > anything and hence causes conflict with the overloaded operator "<<"
    >
    > but then i have to write more-code:
    >
    > assert( raise_power(i, j) == mult_result );
    > std::cout << ( raise_power(i, j) == mult_result ) << std::endl;
    >
    > but then, is writing more code a good idea ? (when i know that
    > expression works fine)
    >

    No, the point was if your test passes, the program exits cleanly, if it
    fails it aborts and you get an error message.

    you could just write

    assert( raise_power(i, j) == mult_result );
    std::cout << "OK" << std::endl;

    You can add more assertions,

    assert( raise_power(2, 2) == 4 );

    and so on.

    I had assumed your book would have covered assert!

    --
    Ian Collins.
     
    Ian Collins, Aug 9, 2007
    #10
  11. arnuld

    Ian Collins Guest

    arnuld wrote:
    > On Aug 9, 11:59 am, arnuld <> wrote:
    >
    >> i did use that, now i just modified it to:
    >>
    >> std::cout << std::assert( raise_power(i, j) == mult_result )
    >> << std::endl;
    >>
    >> and the error is:
    >>
    >> ex_07-03.cpp:32: error: expected unqualified-id before '(' token
    >>
    >> what now ?

    >
    > ok i got it. "assert" is a preprocessor-macro that does not return
    > anything and hence causes conflict with the overloaded operator "<<"
    >

    Sorry to have distracted you with this, the more interesting problem to
    solve and test is supporting negative numbers! Let's see how you get on
    with those...

    --
    Ian Collins.
     
    Ian Collins, Aug 9, 2007
    #11
  12. arnuld

    James Kanze Guest

    On Aug 9, 9:38 am, Ian Collins <> wrote:
    > arnuld wrote:
    > Sorry to have distracted you with this, the more interesting problem to
    > solve and test is supporting negative numbers!


    Especially for a function which returns an int. (But it
    probably shouldn't, because of the risk of overflow.)

    Note too that handling negatives intelligently is definitly
    non-trivial. You can't just do something like:

    double
    power( int i, int n )
    {
    double r = 1.0 ;
    double m = i ;
    bool invert = n < 0 ;
    if ( invert ) {
    n = -n ;
    }
    while ( n > 0 ) {
    r *= m ;
    m *= m ;
    n >>= 1 ;
    }
    return invert ? 1.0 / r : r ;
    }

    -n might still be negative. (Formally, you'd have undefined
    behavior in that case, but in practice, on the usual machines,
    it's a no-op.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Aug 9, 2007
    #12
  13. arnuld

    arnuld Guest

    > On Aug 9, 7:43 pm, James Kanze <> wrote:

    > Especially for a function which returns an int. (But it
    > probably shouldn't, because of the risk of overflow.)


    hmm.. that is why you used double return type.


    > Note too that handling negatives intelligently is definitly
    > non-trivial. You can't just do something like:
    >
    > double
    > power( int i, int n )
    > {
    > double r = 1.0 ;
    > double m = i ;
    > bool invert = n < 0 ;
    > if ( invert ) {
    > n = -n ;
    > }
    > while ( n > 0 ) {
    > r *= m ;
    > m *= m ;
    > n >>= 1 ;
    > }
    > return invert ? 1.0 / r : r ;
    > }


    your programme works fine both with +ve and -ve numbers but i do not
    understand the meaning of "n >>= 1". you are shifting bits one by one
    but what exactly will be the value of "n" after shifting one bit
    right ? i am not able to know the value of "n". yes, it works but what
    happens to the vale of "n" each time ?


    to understand that i used this code:


    #include <iostream>



    int main()
    {
    int x = 3;

    std::cout << "x: " << x << "\n\n";

    while(x > 0)
    {
    std::cout << "x: "
    << (x >>= 1)
    << std::endl;
    }

    return 0;
    }

    ====== OUTPUT ==========
    /home/arnuld/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra
    test.cpp
    /home/arnuld/programming/cpp $ ./a.out
    x: 3

    x: 1
    x: 0
    /home/arnuld/programming/cpp $

    comparing it with your solution i do not understand anything at all.

    > -n might still be negative. (Formally, you'd have undefined
    > behavior in that case, but in practice, on the usual machines,
    > it's a no-op.)
    >
    > --
    > James Kanze (GABI Software) email:
    > Conseils en informatique orientée objet/
    > Beratung in objektorientierter Datenverarbeitung
    > 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


    you said "n = -n" in if clause, so how can "n" still be a -ve number ?
     
    arnuld, Aug 10, 2007
    #13
  14. Hi!

    arnuld schrieb:
    > your programme works fine both with +ve and -ve numbers but i do not
    > understand the meaning of "n >>= 1". you are shifting bits one by one
    > but what exactly will be the value of "n" after shifting one bit
    > right ? i am not able to know the value of "n". yes, it works but what
    > happens to the vale of "n" each time ?


    What happens if you shift "82341" one digit right? It is "8234" which is
    a tenth of "82341". What happens if you shift a binary number one bit
    right? It gets divided by two.
    "n >>= 1" is equivalent to "n /= 2" but maybe faster
    (propably not faster because the optimizer will generate identical code
    for both).

    What I don't understand is why the dopped bit is not looked at in each
    iteration. I thought of:

    while(n>0)
    {
    if( n & 1 )
    r *= m;
    m *= m;
    n >>= 1;
    }

    > you said "n = -n" in if clause, so how can "n" still be a -ve number ?


    There is a negative int number "n" such that "-n" is too large to be
    represented. It happens to usually come out as n again. That is "n ==
    -n" is true (technically it is still undefined behaviour as James
    already said).

    Magic? No. Just take the least int number available:
    std::numeric_limits<int>::min(). This is usually 0x80000000. To invert
    it (according to 2-complements representation which is common) you need
    to first flip all bits and then add 1:
    0x80000000
    flip: 0x7fffffff
    add 1: 0x80000000
    Same as before. Notice that 0x7fffffff is
    std::numeric_limits<int>::max(). So adding 1 will overflow.

    In short: there are usually more negative than positive numbers in the
    domain of "int".

    Frank
     
    Frank Birbacher, Aug 10, 2007
    #14
  15. arnuld

    arnuld Guest

    > On Aug 9, 12:38 pm, Ian Collins <> wrote:

    > Sorry to have distracted you with this, the more interesting problem to
    > solve and test is supporting negative numbers! Let's see how you get on
    > with those...


    that is not distraction, it is a good exercise to. i used simple
    logic to come up with my new code:

    2 ^ 3 = 8
    2 ^ -3 == 1/8 == 0.125

    so i can use the same code and i only need to add something like "if
    (y < 0) then (1/mult)". here is it and it works. BTW, thanks for the
    simple but mind-bending exercise :)


    /* C++ Primer - 4/e
    *
    * exercise 7.3
    * STATEMENT
    * write a programme to take two int paramaters and generate the
    result of raising
    * the first to the power of the second. write a programme to call
    your function passing
    * it two ints. verify the result.
    *
    */

    #include <iostream>
    #include <cassert>

    /* raises "x" to the power "y" */
    double raise_power(int x, int y)
    {
    double mult = 1.0;
    double temp = y;
    /* if y < 0 then make it positive */
    if( y < 0)
    {
    y = -y;
    }

    while( y-- )
    {
    mult = mult * x;
    }

    return ( temp < 0 ) ? (1.0 / mult) : mult;
    }


    int main()
    {
    int i = 2;
    int j = -3;

    std::cout << ( raise_power( i, j ) ) << std::endl;

    return 0;
    }
     
    arnuld, Aug 10, 2007
    #15
  16. arnuld wrote:
    >> On Aug 9, 12:38 pm, Ian Collins <> wrote:

    >
    >> Sorry to have distracted you with this, the more interesting problem
    >> to solve and test is supporting negative numbers! Let's see how you
    >> get on with those...

    >
    > that is not distraction, it is a good exercise to. i used simple
    > logic to come up with my new code:
    >
    > 2 ^ 3 = 8
    > 2 ^ -3 == 1/8 == 0.125
    >
    > so i can use the same code and i only need to add something like "if
    > (y < 0) then (1/mult)". here is it and it works. BTW, thanks for the
    > simple but mind-bending exercise :)
    >
    >
    > /* C++ Primer - 4/e
    > *
    > * exercise 7.3
    > * STATEMENT
    > * write a programme to take two int paramaters and generate the
    > result of raising
    > * the first to the power of the second. write a programme to call
    > your function passing
    > * it two ints. verify the result.
    > *
    > */
    >
    > #include <iostream>
    > #include <cassert>
    >
    > /* raises "x" to the power "y" */
    > double raise_power(int x, int y)
    > {
    > double mult = 1.0;
    > double temp = y;


    'temp' doesn't need to be "double". An 'int' will do.

    > /* if y < 0 then make it positive */
    > if( y < 0)
    > {
    > y = -y;
    > }
    >
    > while( y-- )
    > {
    > mult = mult * x;
    > }
    >
    > return ( temp < 0 ) ? (1.0 / mult) : mult;
    > }
    >
    > [..]


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 10, 2007
    #16
  17. arnuld

    James Kanze Guest

    On Aug 10, 12:21 pm, arnuld <> wrote:
    > > On Aug 9, 7:43 pm, James Kanze <> wrote:
    > > Especially for a function which returns an int. (But it
    > > probably shouldn't, because of the risk of overflow.)


    > hmm.. that is why you used double return type.


    Partially. Also because it makes no sense to allow a negative
    power otherwise.

    > > Note too that handling negatives intelligently is definitly
    > > non-trivial. You can't just do something like:


    > > double
    > > power( int i, int n )
    > > {
    > > double r = 1.0 ;
    > > double m = i ;
    > > bool invert = n < 0 ;
    > > if ( invert ) {
    > > n = -n ;
    > > }
    > > while ( n > 0 ) {
    > > r *= m ;


    There's actually a serious error here. This line should be:

    if ( (n & 1) != 0 ) {
    r *= m ;
    }

    > > m *= m ;
    > > n >>= 1 ;
    > > }
    > > return invert ? 1.0 / r : r ;
    > > }


    > your programme works fine both with +ve and -ve numbers but i do not
    > understand the meaning of "n >>= 1".


    First, it fails for some specific negative values, at least on
    most machines. Did you try it with n == INT_MIN? Even with the
    above correction. (To make it work right, you have to use
    unsigned int.)

    > you are shifting bits one by one
    > but what exactly will be the value of "n" after shifting one bit
    > right ?


    I'm tempted to say: who cares:). The point is that you're not
    dealing with the numeric value of n, but rather with it's
    representation in binary bits. All that you're interested in is
    that what's left in n is the bits that you haven't yet
    processed. (It's slightly more subtle: you do have to process
    the bits in the correct order.)

    In fact, if n is positive, the numeric value is the same as if
    you'd divided by 2. I use the >>= 1 here, instead of /= 2,
    because I'm really thinking in terms of bits, and not numeric
    values.

    > i am not able to know the value of "n". yes, it works but what
    > happens to the vale of "n" each time ?


    > to understand that i used this code:


    > #include <iostream>
    >
    > int main()
    > {
    > int x = 3;
    >
    > std::cout << "x: " << x << "\n\n";
    >
    > while(x > 0)
    > {
    > std::cout << "x: "
    > << (x >>= 1)
    > << std::endl;
    > }
    > return 0;
    > }


    > ====== OUTPUT ==========
    > /home/arnuld/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra
    > test.cpp
    > /home/arnuld/programming/cpp $ ./a.out
    > x: 3
    > x: 1
    > x: 0
    > /home/arnuld/programming/cpp $


    > comparing it with your solution i do not understand anything at all.


    Think about how a binary representation works. What does each
    bit represent? Think about where the bit being

    > > -n might still be negative. (Formally, you'd have undefined
    > > behavior in that case, but in practice, on the usual machines,
    > > it's a no-op.)


    > you said "n = -n" in if clause, so how can "n" still be a -ve number ?


    As I said, you've encountered undefined behavior. You might
    want to try printing out the values of INT_MIN and INT_MAX.
    Then ask yourself what -INT_MIN should be, as an int.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Aug 11, 2007
    #17
  18. arnuld

    James Kanze Guest

    On Aug 10, 12:57 pm, Frank Birbacher <> wrote:

    [...]
    > What I don't understand is why the dopped bit is not looked at in each
    > iteration. I thought of:


    > while(n>0)
    > {
    > if( n & 1 )
    > r *= m;
    > m *= m;
    > n >>= 1;
    > }


    Because I typed the code in too fast, and only tested it with
    some simple values for which it happened to work.

    > > you said "n = -n" in if clause, so how can "n" still be a -ve number ?


    > There is a negative int number "n" such that "-n" is too large to be
    > represented. It happens to usually come out as n again. That is "n ==
    > -n" is true (technically it is still undefined behaviour as James
    > already said).


    And of course, it depends on the implementation. It's only a
    problem on machines using 2's complement---it works fine with
    1's complement and signed magnitude. Of course, off hand, I
    only know of one, relatively exotic mainframe that uses 1's
    complement today, and as far as I know, the last signed
    magnitude machine went out of production some years ago. So
    it's probably a safe bet that the machine he's programming on
    uses 2's complement.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Aug 11, 2007
    #18
  19. arnuld

    James Kanze Guest

    On Aug 10, 2:40 pm, arnuld <> wrote:

    [...]
    > /* raises "x" to the power "y" */
    > double raise_power(int x, int y)
    > {
    > double mult = 1.0;
    > double temp = y;
    > /* if y < 0 then make it positive */
    > if( y < 0)
    > {
    > y = -y;
    > }
    > while( y-- )
    > {
    > mult = mult * x;
    > }
    > return ( temp < 0 ) ? (1.0 / mult) : mult;
    > }


    I tried it with 5, -2147483648. The result was 1. Somehow, I
    don't think that's right.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Aug 11, 2007
    #19
  20. arnuld

    arnuld Guest

    > On Aug 11, 5:30 pm, James Kanze <> wrote:

    > I tried it with 5, -2147483648. The result was 1. Somehow, I
    > don't think that's right.


    i think, -2147483648 is too big to fit in a double.

    then you can also use these arguments:

    -343458234892348914789789,
    789234897023478901234689234232342347842342347894123478247812378

    but why i need such big values ? may be they are specific
    requirements for some specific domain like scientific
    programming, ....... why do i even need -2147483648 ?

    i think just making it work for values which fit in a double is ok.
     
    arnuld, Aug 11, 2007
    #20
    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. Richard

    SSL / authentication primer

    Richard, Jul 25, 2003, in forum: Java
    Replies:
    2
    Views:
    483
    Richard
    Jul 25, 2003
  2. Chris
    Replies:
    0
    Views:
    367
    Chris
    Apr 18, 2005
  3. Captain Dondo

    SVG primer

    Captain Dondo, Jan 21, 2006, in forum: HTML
    Replies:
    1
    Views:
    576
    Toby Inkster
    Jan 22, 2006
  4. Charles L

    'C++ Primer 2nd Ed' errata

    Charles L, Apr 11, 2004, in forum: C++
    Replies:
    2
    Views:
    365
  5. hugo
    Replies:
    1
    Views:
    360
    Ali Cehreli
    Aug 17, 2004
Loading...

Share This Page