Re: Determining validity of mathematical expression before evaluation

Heime <[email protected]>
Newsgroups gmane.emacs.help
Message-ID <wquMQKEzQ64WiEbcZB6_7eu_CSJDWRzdvUwaWm5M_MxrjxzEMuDlYFAxNtm_xYjUpSpM7dkD1rN-utKtudxrqkNzRg3M-rsb7oRgHsbC4zw=@protonmail.com>
On Friday, March 27th, 2026 at 12:42 PM, Heime <[email protected]> wrote:

> Is it possible to determine whether an elisp mathematical expression
> is valid before evaluating it?

Have tried this, passing a string expression and evaluating it.
And if it fails, I call bc.  But have not been capable of using
condition-case properly.

(defun calc (expr)
  "Evaluate arithmetic expression interactively.
Enter operation and numbers as a single string like: '10 + 5 * 2'."
  (interactive "sExpression (10 + 5 * 2): ")

  (condition-case err
      (let ((result (eval (read expr))))
	(message "Result (eval): %s" result))

    (progn 
      (string-to-number 
       (shell-command-to-string 
	(format "echo '%s' | bc -l" expr)))
      (message "Result (bc): %s" result))))
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.