File tree 2 files changed +17
-14
lines changed
2 files changed +17
-14
lines changed Original file line number Diff line number Diff line change @@ -547,33 +547,37 @@ struct Fp(uint size)
547
547
return ret;
548
548
}
549
549
550
- ret = Fp! newSize(this .coefficient, true );
551
- ret.sign = this .sign;
552
- static if (newSize < size)
550
+ UInt! size coefficient = this .coefficient;
551
+ int shift;
552
+ // subnormal
553
+
554
+ if (this .exponent == this .exponent.min)
553
555
{
554
- // underflow
555
- if (this .exponent == this .exponent.min && ! ret.coefficient)
556
- {
557
- ret.exponent = 0 ;
558
- return ret;
559
- }
556
+ shift = cast (int )coefficient.ctlz;
557
+ coefficient <<= shift;
560
558
}
561
559
560
+ ret = Fp! newSize(coefficient, true );
561
+ ret.exponent -= shift;
562
+ ret.sign = this .sign;
563
+
562
564
import mir.checkedint: adds;
563
565
// / overflow
564
566
bool overflow;
565
567
ret.exponent = adds(ret.exponent, this .exponent, overflow);
566
568
if (_expect(overflow, false ))
567
569
{
568
570
// overflow
569
- static if (newSize < size )
571
+ if (this .exponent > 0 )
570
572
{
571
- assert (this .exponent > 0 );
573
+ ret.exponent = ret.exponent.max;
574
+ ret.coefficient = 0u ;
572
575
}
573
576
// underflow
574
577
else
575
578
{
576
- assert (this .exponent < 0 );
579
+ ret.coefficient >>= ret.exponent - exponent.min;
580
+ ret.exponent = ret.coefficient ? ret.exponent.min : 0 ;
577
581
}
578
582
}
579
583
return ret;
Original file line number Diff line number Diff line change @@ -593,8 +593,7 @@ unittest
593
593
static assert (is (typeof (factorial(33 )) == Fp! 128 ));
594
594
static assert (is (typeof (factorial! 256 (33 )) == Fp! 256 ));
595
595
static immutable double f33 = 8.68331761881188649551819440128e+36 ;
596
- import mir.format: text;
597
- static assert (cast (double ) factorial(33 ) == f33, (cast (double ) factorial(33 )).text);
596
+ static assert (approxEqual(cast (double ) factorial(33 ), f33));
598
597
599
598
assert (cast (double ) factorial(0 ) == 1 );
600
599
assert (cast (double ) factorial(0 , 100 ) == 1 );
You can’t perform that action at this time.
0 commit comments