ob-calc.el: Fix assigning floating point numbers

* lisp/ob-calc.el (org-babel-execute:calc): Use internal calc format
when assigning variable values that are numbers.
* testing/lisp/test-ob-calc.el (ob-calc/float-var): New test.

Reported-by: Visuwesh <visuweshm@gmail.com>
Link: https://orgmode.org/list/87edbu4kdh.fsf@gmail.com
This commit is contained in:
Ihor Radchenko 2024-05-02 15:24:15 +03:00
parent a2e5685e49
commit dd12e9c763
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
2 changed files with 23 additions and 11 deletions

View File

@ -66,17 +66,21 @@
(lambda (pair)
(let ((val (cdr pair)))
(calc-push-list
;; For a vector, Calc follows the format (vec 1 2 3 ...) so
;; a matrix becomes (vec (vec 1 2 3) (vec 4 5 6) ...). See
;; the comments in "Arithmetic routines." section of
;; calc.el.
(list (if (listp val)
(cons 'vec
(if (null (cdr val))
(car val)
(mapcar (lambda (x) (if (listp x) (cons 'vec x) x))
val)))
val))))
(list
(cond
;; For a vector, Calc follows the format (vec 1 2 3 ...) so
;; a matrix becomes (vec (vec 1 2 3) (vec 4 5 6) ...). See
;; the comments in "Arithmetic routines." section of
;; calc.el.
((listp val)
(cons 'vec
(if (null (cdr val))
(car val)
(mapcar (lambda (x) (if (listp x) (cons 'vec x) x))
val))))
((numberp val)
(math-read-number (number-to-string val)))
(t val)))))
(calc-store-into (car pair)))
vars)
(mapc

View File

@ -39,6 +39,14 @@
#+END_SRC"
(should (equal "27" (org-babel-execute-src-block)))))
(ert-deftest ob-calc/float-var ()
"Test of floating variable."
(org-test-with-temp-text "\
#+BEGIN_SRC calc :results silent :var x=2.0
1/x
#+END_SRC"
(should (equal "0.5" (org-babel-execute-src-block)))))
(ert-deftest ob-calc/simple-program-symbolic ()
"Test of simple symbolic algebra."
(org-test-with-temp-text "\