libs/libc/math: Fix calculation error of log function

Fix a problem where the log function would loop infinitely by
calculation error when a specific value was passed. For example,
in the following case, the log function loops infinitely and
never returns. So, this commit fixes to return the right value.

  double a = 7883961.5;
  double b = log(a);
This commit is contained in:
SPRESENSE 2021-07-10 21:14:17 +09:00 committed by Xiang Xiao
parent 5fe51b923a
commit 2e80d5e38e
2 changed files with 6 additions and 6 deletions

View file

@ -60,7 +60,7 @@ double log(double x)
{
double y;
double y_old;
double ney;
double ey;
double epsilon;
int relax_factor;
int iter;
@ -75,8 +75,8 @@ double log(double x)
while (y > y_old + epsilon || y < y_old - epsilon)
{
y_old = y;
ney = exp(-y);
y -= 1.0 - x * ney;
ey = exp(y);
y -= (ey - x) / ey;
if (y > 700.0)
{

View file

@ -58,7 +58,7 @@ float logf(float x)
{
float y;
float y_old;
float ney;
float ey;
float epsilon;
int relax_factor;
int iter;
@ -73,8 +73,8 @@ float logf(float x)
while (y > y_old + epsilon || y < y_old - epsilon)
{
y_old = y;
ney = expf(-y);
y -= 1.0F - x * ney;
ey = expf(y);
y -= (ey - x) / ey;
if (y > FLT_MAX_EXP_X)
{