bugGNU Octave - Bugs: bug #43041, isprime(5i) returns true

 
 

bug #43041: isprime(5i) returns true

Submitted by:  Colin Macdonald <cbm>
Submitted on:  Thu 21 Aug 2014 04:23:21 PM UTC  
 
Category: Octave FunctionSeverity: 3 - Normal
Priority: 5 - NormalItem Group: Incorrect Result
Status: FixedAssigned to: None
Originator Name: Open/Closed: Closed
Release: devOperating System: GNU/Linux

Add a New Comment(Rich Markup)
   

You are not logged in

Please log in, so followups can be emailed to you.

 

Sun 24 Aug 2014 02:45:38 PM UTC, comment #5:

It is convenient for many purposes to define a prime number as a non-unit number that divides a factor whenever it divides a product (a unit is a number that divides 1). This is the definition that generalises to any integral domain. Following this definition, both Maxima's and GAP's isprime() and IsPrimeInt() respectively consider negative prime numbers to be prime. Interestingly, Pari/GP, which is actually specialised for number theory, excludes negative prime numbers from its isprime() function.

Since I consider "Matlab compatibility" to allow not erroring out when Matlab errors out, and since Matlab errors out on negative integers, I think we can pick whichever definition works out. I prefer to consider negative primes as primes, but if you feel strongly otherwise, feel free to make the change to exclude them or to further document my implementation.

Jordi Gutiérrez Hermoso <jordigh>
Project Administrator
Sun 24 Aug 2014 12:27:49 AM UTC, comment #4:

Is it correct to return

when x is real and less than 0? Every web source defines a real-valued prime on the positive integer range [1, Inf]. As an example, http://mathworld.wolfram.com/PrimeNumber.html.

It seems to be more appropriate to issue an error in this case. Or, if the calculation should proceed then to issue NaN since it is not definitionally a prime number, even if it meets the test for being a prime.

In this case, -5, looks like a duck, quacks like a duck, but is excluded by definition from actually being a duck.

Rik <rik5>
Project Administrator
Sat 23 Aug 2014 10:31:37 PM UTC, comment #3:

I have pushed the following:

http://hg.savannah.gnu.org/hgweb/octave/rev/89e275a4f6f6

Jordi Gutiérrez Hermoso <jordigh>
Project Administrator
Sat 23 Aug 2014 07:37:08 PM UTC, comment #2:

The bug is not that isprime(7i) is true, because it is, but that isprime(5i) is true (which it's not, since 5i = (2 + i)(1+2i).

I'll patch this. There's no reason why this function should be restricted to positive integers.

Jordi Gutiérrez Hermoso <jordigh>
Project Administrator
Thu 21 Aug 2014 11:12:17 PM UTC, comment #1:

isprime now checks whether the input is a real number (http://hg.savannah.gnu.org/hgweb/octave/rev/0976f9fccbbd). Closing report.

Rik <rik5>
Project Administrator
Thu 21 Aug 2014 04:23:21 PM UTC, original submission:

isprime(4i) is false which I can live with :)

isprime(7i) gives true: it should either give false or raise an exception.

Matlab gives an error "All entries of X must be nonnegative integers."

---------------------

Our error message for isprime(-1) needs a tweak too:
"error: isprime: needs positive integers".

Should say "nonnegative integers"

---------------------

I might around to patching this but submitting the bug in case I don't!

Colin Macdonald <cbm>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach File(s):
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -unavailable- added by jordigh (Posted a comment)
  • -unavailable- added by rik5 (Posted a comment)
  • -unavailable- added by cbm (Submitted the item)
  •  

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

     

     

    Follow 7 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Sat 23 Aug 2014 10:31:37 PM UTCjordighStatusConfirmed=>Fixed
      Open/ClosedOpen=>Closed
    Sat 23 Aug 2014 07:37:55 PM UTCjordighOpen/ClosedClosed=>Open
    Sat 23 Aug 2014 07:37:08 PM UTCjordighStatusFixed=>Confirmed
      Summaryisprime(7i) returns true=>isprime(5i) returns true
    Thu 21 Aug 2014 11:12:17 PM UTCrik5StatusNone=>Fixed
      Open/ClosedOpen=>Closed

    Back to the top


    Powered by Savane 3.1-cleanup1