@@ -623,9 +623,14 @@ public RubyFixnum num_bits_set(final ThreadContext context) {
623
623
public IRubyObject prime_p (IRubyObject [] args ) {
624
624
final Ruby runtime = getRuntime ();
625
625
int argc = Arity .checkArgumentCount (runtime , args , 0 , 1 );
626
+
627
+ // negative numbers are always considered non-prime
628
+ if (this .value .signum () < 0 ) return runtime .getFalse ();
629
+
630
+ int certainty = argc == 0 ? DEFAULT_CERTAINTY : RubyNumeric .fix2int (args [0 ]);
631
+
626
632
// BigInteger#isProbablePrime will actually limit checks to a maximum of 50,
627
633
// depending on bit count.
628
- int certainty = argc == 0 ? DEFAULT_CERTAINTY : RubyNumeric .fix2int (args [0 ]);
629
634
return runtime .newBoolean (this .value .isProbablePrime (certainty ));
630
635
}
631
636
@@ -635,9 +640,14 @@ public IRubyObject prime_p(IRubyObject[] args) {
635
640
public IRubyObject prime_fasttest_p (IRubyObject [] args ) {
636
641
final Ruby runtime = getRuntime ();
637
642
int argc = Arity .checkArgumentCount (runtime , args , 0 , 2 );
643
+
644
+ // negative numbers are always considered non-prime
645
+ if (this .value .signum () < 0 ) return runtime .getFalse ();
646
+
647
+ int certainty = argc == 0 ? DEFAULT_CERTAINTY : RubyNumeric .fix2int (args [0 ]);
648
+
638
649
// BigInteger#isProbablePrime will actually limit checks to a maximum of 50,
639
650
// depending on bit count.
640
- int certainty = argc == 0 ? DEFAULT_CERTAINTY : RubyNumeric .fix2int (args [0 ]);
641
651
return runtime .newBoolean (this .value .isProbablePrime (certainty ));
642
652
}
643
653
0 commit comments