Skip to content

Commit

Permalink
Mark all the methods calling low level mpdecimal functions as @trusted.
Browse files Browse the repository at this point in the history
  • Loading branch information
pdenapo committed Feb 21, 2024
1 parent d49eb29 commit 8e922f3
Showing 1 changed file with 42 additions and 42 deletions.
84 changes: 42 additions & 42 deletions source/d_mpdecimal/decimal.d
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ SOFTWARE.

mpd_context_t decimal_ctx;

void init_decimal(int prec)
@trusted void init_decimal(int prec)
{
printf("Using libmpdec version %s \n", mpd_version);
mpd_init(&decimal_ctx, prec);
}

void init_ieee_decimal(int bits)
@trusted void init_ieee_decimal(int bits)
{
printf("Using libmpdec version %s \n", mpd_version);
mpd_ieee_context(&decimal_ctx, bits);
Expand All @@ -61,20 +61,20 @@ struct Decimal
value = v;
}

this(Decimal original)
@trusted this(Decimal original)
{
//debug writeln("Call copy constructor");
value = mpd_qncopy(original.value);
}

this(this)
@trusted this(this)
{
//debug writeln("Calling this(this)");
if (value)
value = mpd_qncopy(value);
}

this(string s)
@trusted this(string s)
{
value = mpd_new(&decimal_ctx);
immutable(char)* c_string = toStringz(s);
Expand All @@ -85,19 +85,19 @@ struct Decimal
GC.clrAttr(cast(void*) c_string, GC.BlkAttr.NO_MOVE);
}

this(int x)
@trusted this(int x)
{
value = mpd_new(&decimal_ctx);
mpd_set_i32(value, x, &decimal_ctx);
}

this(long x)
@trusted this(long x)
{
value = mpd_new(&decimal_ctx);
mpd_set_i64(value, x, &decimal_ctx);
}

~this()
@trusted ~this()
{
//debug writeln("Calling mpd_del value=",value);
if (value)
Expand All @@ -106,7 +106,7 @@ struct Decimal
}

// a string representation, used by write.
string toString() const
@trusted string toString() const
{
if (value)
{
Expand All @@ -118,7 +118,7 @@ struct Decimal
return "null";
}

string format(string fmt) const
@trusted string format(string fmt) const
{
if (value)
{
Expand All @@ -130,7 +130,7 @@ struct Decimal
return "null";
}

bool opEquals(Decimal rhs) const
@trusted bool opEquals(Decimal rhs) const
{
if (!value || !rhs.value)
return false;
Expand All @@ -139,7 +139,7 @@ struct Decimal

// unary operator overloading

Decimal opUnary(string op)()
@trusted Decimal opUnary(string op)()
{
mpd_t* result;
if (!value)
Expand Down Expand Up @@ -172,7 +172,7 @@ struct Decimal

// binary operator overloading

Decimal opBinary(string op)(Decimal rhs)
@trusted Decimal opBinary(string op)(Decimal rhs)
{
if (!value || !rhs.value)
return this;
Expand Down Expand Up @@ -209,7 +209,7 @@ struct Decimal

// assign operator overloading

Decimal opOpAssign(string op)(Decimal rhs)
@trusted Decimal opOpAssign(string op)(Decimal rhs)
{
if (!value || !rhs.value)
return this;
Expand Down Expand Up @@ -242,100 +242,100 @@ struct Decimal

// comparison operator overloading

int opCmp(const Decimal rhs) const
@trusted int opCmp(const Decimal rhs) const
{
if (!value || !rhs.value)
return false;
return mpd_cmp(value, rhs.value, &decimal_ctx);
}

bool isfinite() const
@trusted bool isfinite() const
{
if (!value)
return false;
return cast(bool) mpd_isfinite(value);
}

bool isinfinite() const
@trusted bool isinfinite() const
{
if (!value)
return false;
return cast(bool) mpd_isinfinite(value);
}

bool isnan() const
@trusted bool isnan() const
{
if (!value)
return false;
return cast(bool) mpd_isnan(value);
}

bool isnegative() const
@trusted bool isnegative() const
{
if (!value)
return false;
return cast(bool) mpd_isnegative(value);
}

bool ispositive() const
@trusted bool ispositive() const
{
if (!value)
return false;
return cast(bool) mpd_ispositive(value);
}

bool isqnan() const
@trusted bool isqnan() const
{
if (!value)
return false;
return cast(bool) mpd_isqnan(value);
}

bool issigned() const
@trusted bool issigned() const
{
if (!value)
return false;
return cast(bool) mpd_issigned(value);
}

bool issnan() const
@trusted bool issnan() const
{
if (!value)
return false;
return cast(bool) mpd_issnan(value);
}

bool isspecial() const
@trusted bool isspecial() const
{
if (!value)
return false;
return cast(bool) mpd_isspecial(value);
}

bool iszero() const
@trusted bool iszero() const
{
if (!value)
return false;
return cast(bool) mpd_iszero(value);
}

bool is_nonnegative() const
@trusted bool is_nonnegative() const
{
return iszero() || ispositive();
}

bool is_nonpositive() const
@trusted bool is_nonpositive() const
{
return iszero() || isnegative();
}

bool isinteger() const
@trusted bool isinteger() const
{
return cast(bool) mpd_isinteger(value);
}
}

Decimal decimal_abs(const Decimal x)
@trusted Decimal decimal_abs(const Decimal x)
{
mpd_t* result;
result = mpd_new(&decimal_ctx);
Expand All @@ -344,71 +344,71 @@ Decimal decimal_abs(const Decimal x)
}

// round-to-integral-exact
Decimal decimal_round_to_intx(const Decimal x)
@trusted Decimal decimal_round_to_intx(const Decimal x)
{
mpd_t* result;
result = mpd_new(&decimal_ctx);
mpd_round_to_intx(result, x.value, &decimal_ctx);
return Decimal(result);
}

Decimal decimal_round_to_int(const Decimal x)
@trusted Decimal decimal_round_to_int(const Decimal x)
{
mpd_t* result;
result = mpd_new(&decimal_ctx);
mpd_round_to_int(result, x.value, &decimal_ctx);
return Decimal(result);
}

Decimal decimal_floor(const Decimal x)
@trusted Decimal decimal_floor(const Decimal x)
{
mpd_t* result;
result = mpd_new(&decimal_ctx);
mpd_floor(result, x.value, &decimal_ctx);
return Decimal(result);
}

Decimal decimal_ceil(const Decimal x)
@trusted Decimal decimal_ceil(const Decimal x)
{
mpd_t* result;
result = mpd_new(&decimal_ctx);
mpd_ceil(result, x.value, &decimal_ctx);
return Decimal(result);
}

Decimal decimal_trunc(const Decimal x)
@trusted Decimal decimal_trunc(const Decimal x)
{
mpd_t* result;
result = mpd_new(&decimal_ctx);
mpd_trunc(result, x.value, &decimal_ctx);
return Decimal(result);
}

Decimal decimal_exp(const Decimal x)
@trusted Decimal decimal_exp(const Decimal x)
{
mpd_t* result;
result = mpd_new(&decimal_ctx);
mpd_exp(result, x.value, &decimal_ctx);
return Decimal(result);
}

Decimal decimal_ln(const Decimal x)
@trusted Decimal decimal_ln(const Decimal x)
{
mpd_t* result;
result = mpd_new(&decimal_ctx);
mpd_ln(result, x.value, &decimal_ctx);
return Decimal(result);
}

Decimal decimal_log10(const Decimal x)
@trusted Decimal decimal_log10(const Decimal x)
{
mpd_t* result;
result = mpd_new(&decimal_ctx);
mpd_log10(result, x.value, &decimal_ctx);
return Decimal(result);
}

Decimal decimal_sqrt(const Decimal x)
@trusted Decimal decimal_sqrt(const Decimal x)
{
mpd_t* result;
result = mpd_new(&decimal_ctx);
Expand All @@ -417,26 +417,26 @@ Decimal decimal_sqrt(const Decimal x)
}

// inverse-square-root
Decimal decimal_invroot(const Decimal x)
@trusted Decimal decimal_invroot(const Decimal x)
{
mpd_t* result;
result = mpd_new(&decimal_ctx);
mpd_invroot(result, x.value, &decimal_ctx);
return Decimal(result);
}

void clear_status()
@trusted void clear_status()
{
mpd_qsetstatus(&decimal_ctx, 0);
}

uint32_t get_status()
@trusted uint32_t get_status()
{
return mpd_getstatus(&decimal_ctx);
}

/// Prints and return the status flags
uint32_t print_status()
@trusted uint32_t print_status()
{
uint32_t status = get_status();
string binary_status = format("%b", status);
Expand Down

0 comments on commit 8e922f3

Please sign in to comment.