Recently I stumbled upon a weird bug when I used a package that sets the NumericQ result of symbols you are feeding into a certain function to true.

Here is a minimal working example of what I mean:

NumericQ[a] ^= True;
NumericQ[b] ^= True;
a + a (-1.`) - 2 b + 2.` 0 b + 2 b

The output of the above expression should be 0 but it gives

0. - 2 b

Exchanging any of the floats with integers gives the correct result. Could someone explain to me what is happening here? Is this a bug I should report?

Another hint could be that when I decrease the factor in front of the first b by one, the result is decreased by 2:

a + a (-1.`) - 3 b + 2.` 0 b + 2 b


0. - 4 b

Any ideas are welcome.

  • 3
    $\begingroup$ On the one hand, this is probably unexpected behavior. On the other hand: What is the point of setting NumericQ[a] to True if a is a symbol? This breaks the whole purpose of NumericQ. $\endgroup$ – Henrik Schumacher Mar 15 at 10:49
  • $\begingroup$ It's used in this package: nrgljubljana.ijs.si/sneg. You can define real constants using snegrealconstants, so that for example functions like scalarproduct can distinguish between operators and scalars. $\endgroup$ – tantum quod Mar 15 at 11:06
  • 4
    $\begingroup$ Well, that does not convince me that what they do were good practice. $\endgroup$ – Henrik Schumacher Mar 15 at 11:12
  • 4
    $\begingroup$ If N[x] does not return a number then NumericQ[x] should not be True. This is simply a misuse of NumericQ. $\endgroup$ – Szabolcs Mar 15 at 12:57
  • 1
    $\begingroup$ Here is a reasonable use for adding definitions to NumericQ: mathematica.stackexchange.com/q/128444/12 Using it to indicate what is a scalar and what is an operator is plainly wrong, and will lead to lots of broken things down the road. $\endgroup$ – Szabolcs Mar 15 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.