Changeset 1116


Ignore:
Timestamp:
Dec 23, 2011, 3:43:32 AM (9 years ago)
Author:
sam
Message:

core: fix the sign of a negative real number raised to an even power, and
add the corresponding unit test.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/real.cpp

    r1115 r1116  
    693693    else /* x < 0 */
    694694    {
     695        /* Odd integer exponent */
     696        if (y == (round(y >> 1) << 1))
     697            return exp(y * log(-x));
     698
     699        /* Even integer exponent */
    695700        if (y == round(y))
    696701            return -exp(y * log(-x));
     702
    697703        /* FIXME: negative nth root */
    698704        return real::R_0;
  • trunk/test/unit/real.cpp

    r1115 r1116  
    327327        }
    328328    }
     329
     330    LOLUNIT_TEST(Pow)
     331    {
     332        double a1 = pow(-real::R_2, real::R_2);
     333        double b1 = 4.0;
     334        LOLUNIT_ASSERT_DOUBLES_EQUAL(a1, b1, 1.0e-13);
     335
     336        double a2 = pow(-real::R_2, real::R_3);
     337        double b2 = -8.0;
     338        LOLUNIT_ASSERT_DOUBLES_EQUAL(a2, b2, 1.0e-13);
     339    }
    329340};
    330341
Note: See TracChangeset for help on using the changeset viewer.