5
$\begingroup$

Suppose a want a pattern to match:

-1
-1.
-1.`42 (or any other value for the precision)

with a pattern. What would be the best pattern to use? I guess one could use

_?(N@#==-1.&)

but this seems like it shouldn't be the best approach. Any thoughts?

$\endgroup$
  • 3
    $\begingroup$ I don't think there are significantly better ways to do it, tbh. Is there any reason why you're not satisfied with this pattern? It depends on the application, I guess. If you want to find -1 values in an array, there are vectorised ways to do it. $\endgroup$ – Sjoerd Smit Jul 18 '18 at 12:52
  • $\begingroup$ Cases[{-1, -1., -1.` 42}, _?(# == -1 &)] works for me $\endgroup$ – Bob Hanlon Jul 18 '18 at 17:59
  • $\begingroup$ _?(PossibleZeroQ[#+1]&) perhaps. $\endgroup$ – Daniel Lichtblau Jul 18 '18 at 18:17

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.