Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
9il committed May 26, 2022
1 parent 818322a commit 8690f39
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
28 changes: 16 additions & 12 deletions source/mir/bignum/fp.d
Original file line number Diff line number Diff line change
Expand Up @@ -547,33 +547,37 @@ struct Fp(uint size)
return ret;
}

ret = Fp!newSize(this.coefficient, true);
ret.sign = this.sign;
static if (newSize < size)
UInt!size coefficient = this.coefficient;
int shift;
// subnormal

if (this.exponent == this.exponent.min)
{
// underflow
if (this.exponent == this.exponent.min && !ret.coefficient)
{
ret.exponent = 0;
return ret;
}
shift = cast(int)coefficient.ctlz;
coefficient <<= shift;
}

ret = Fp!newSize(coefficient, true);
ret.exponent -= shift;
ret.sign = this.sign;

import mir.checkedint: adds;
/// overflow
bool overflow;
ret.exponent = adds(ret.exponent, this.exponent, overflow);
if (_expect(overflow, false))
{
// overflow
static if (newSize < size)
if (this.exponent > 0)
{
assert(this.exponent > 0);
ret.exponent = ret.exponent.max;
ret.coefficient = 0u;
}
// underflow
else
{
assert(this.exponent < 0);
ret.coefficient >>= ret.exponent - exponent.min;
ret.exponent = ret.coefficient ? ret.exponent.min : 0;
}
}
return ret;
Expand Down
3 changes: 1 addition & 2 deletions source/mir/math/numeric.d
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,7 @@ unittest
static assert(is(typeof(factorial(33)) == Fp!128));
static assert(is(typeof(factorial!256(33)) == Fp!256));
static immutable double f33 = 8.68331761881188649551819440128e+36;
import mir.format: text;
static assert(cast(double) factorial(33) == f33, (cast(double) factorial(33)).text);
static assert(approxEqual(cast(double) factorial(33), f33));

assert(cast(double) factorial(0) == 1);
assert(cast(double) factorial(0, 100) == 1);
Expand Down

0 comments on commit 8690f39

Please sign in to comment.