Explorar el Código

core: implement sin(), cos() and abs() for reals and fix a crash in the

addition and subtraction operators occurring when exponents were too
different.
legacy
Sam Hocevar sam hace 13 años
padre
commit
593aa3af70
Se han modificado 2 ficheros con 52 adiciones y 0 borrados
  1. +47
    -0
      src/real.cpp
  2. +5
    -0
      src/real.h

+ 47
- 0
src/real.cpp Ver fichero

@@ -129,6 +129,9 @@ real real::operator +(real const &x) const
int bigoff = (e1 - e2) / (sizeof(uint16_t) * 8);
int off = e1 - e2 - bigoff * (sizeof(uint16_t) * 8);

if (bigoff > BIGITS)
return *this;

ret.m_signexp = m_signexp;

uint32_t carry = 0;
@@ -188,6 +191,9 @@ real real::operator -(real const &x) const
int bigoff = (e1 - e2) / (sizeof(uint16_t) * 8);
int off = e1 - e2 - bigoff * (sizeof(uint16_t) * 8);

if (bigoff > BIGITS)
return *this;

ret.m_signexp = m_signexp;

int32_t carry = 0;
@@ -501,6 +507,13 @@ real sqrt(real const &x)
return ret * x;
}

real fabs(real const &x)
{
real ret = x;
ret.m_signexp &= 0x7fffffffu;
return ret;
}

real exp(real const &x)
{
int square = 0;
@@ -529,6 +542,40 @@ real exp(real const &x)
return ret;
}

real sin(real const &x)
{
real ret = 0.0, fact = 1.0, xn = x, x2 = x * x;

for (int i = 1; ; i += 2)
{
real newret = ret + xn / fact;
if (ret == newret)
break;
ret = newret;
xn *= x2;
fact *= (real)(-(i + 1) * (i + 2));
}

return ret;
}

real cos(real const &x)
{
real ret = 0.0, fact = 1.0, xn = 1.0, x2 = x * x;

for (int i = 1; ; i += 2)
{
real newret = ret + xn / fact;
if (ret == newret)
break;
ret = newret;
xn *= x2;
fact *= (real)(-i * (i + 1));
}

return ret;
}

void real::print(int ndigits) const
{
real const r1 = 1, r10 = 10;


+ 5
- 0
src/real.h Ver fichero

@@ -53,10 +53,15 @@ public:
bool operator <=(real const &x) const;
bool operator >=(real const &x) const;

friend real fabs(real const &x);

friend real fres(real const &x);
friend real sqrt(real const &x);
friend real exp(real const &x);

friend real sin(real const &x);
friend real cos(real const &x);

void print(int ndigits = 150) const;

private:


Cargando…
Cancelar
Guardar