瀏覽代碼

core: fix the sign of a negative real number raised to an even power, and

add the corresponding unit test.
legacy
Sam Hocevar sam 14 年之前
父節點
當前提交
f56c72c53d
共有 2 個檔案被更改,包括 17 行新增0 行删除
  1. +6
    -0
      src/real.cpp
  2. +11
    -0
      test/unit/real.cpp

+ 6
- 0
src/real.cpp 查看文件

@@ -692,8 +692,14 @@ real pow(real const &x, real const &y)
return exp(y * log(x)); return exp(y * log(x));
else /* x < 0 */ else /* x < 0 */
{ {
/* Odd integer exponent */
if (y == (round(y >> 1) << 1))
return exp(y * log(-x));

/* Even integer exponent */
if (y == round(y)) if (y == round(y))
return -exp(y * log(-x)); return -exp(y * log(-x));

/* FIXME: negative nth root */ /* FIXME: negative nth root */
return real::R_0; return real::R_0;
} }


+ 11
- 0
test/unit/real.cpp 查看文件

@@ -326,6 +326,17 @@ LOLUNIT_FIXTURE(RealTest)
LOLUNIT_ASSERT_EQUAL(b2, a2); LOLUNIT_ASSERT_EQUAL(b2, a2);
} }
} }

LOLUNIT_TEST(Pow)
{
double a1 = pow(-real::R_2, real::R_2);
double b1 = 4.0;
LOLUNIT_ASSERT_DOUBLES_EQUAL(a1, b1, 1.0e-13);

double a2 = pow(-real::R_2, real::R_3);
double b2 = -8.0;
LOLUNIT_ASSERT_DOUBLES_EQUAL(a2, b2, 1.0e-13);
}
}; };


} /* namespace lol */ } /* namespace lol */


Loading…
取消
儲存