emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [babel] calc var floating point number error
@ 2012-07-20 13:55 Giovanni Ridolfi
  2012-07-20 14:34 ` Eric Schulte
  0 siblings, 1 reply; 3+ messages in thread
From: Giovanni Ridolfi @ 2012-07-20 13:55 UTC (permalink / raw)
  To: Orgmode

Hello everyone,

GNU Emacs 24.1.1 (i386-mingw-nt6.1.7601) of 2012-06-10 on MARVIN
Org-mode version 7.8.11 (eed478ffa @ 


I have a problem with babel and calc.

I am not able to pass (as variable) floating point numbers. 

this example works:

#+BEGIN_SRC calc :var thi=20 :var tha=90 :var a=10 :var rho=7854 :var cp=434 :var r=5
 tha -  (tha - thi) * exp((-3.0*a)/(rho*cp*r))
#+END_SRC

#+RESULTS:
: 20.0001232163


However var "r" should be 0.05, but if I use such floating point number I got an error:


#+NAME sphere
#+BEGIN_SRC calc :var thi=20 :var tha=90 :var a=10 :var rho=7854 :var cp=434 :var r=0.05
 tha -  (tha - thi) * exp((-3.0*a)/(rho*cp*r))
#+END_SRC

#+RESULTS:
| 19 | Expected a number |

This behaviour has been reported also in SO

http://stackoverflow.com/questions/9559221/simple-math-results-in-an-emacs-org-mode-file-with-babel


cheers,
Giovanni


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [babel] calc var floating point number error
  2012-07-20 13:55 [babel] calc var floating point number error Giovanni Ridolfi
@ 2012-07-20 14:34 ` Eric Schulte
  2012-07-25 15:32   ` Eric Schulte
  0 siblings, 1 reply; 3+ messages in thread
From: Eric Schulte @ 2012-07-20 14:34 UTC (permalink / raw)
  To: Giovanni Ridolfi; +Cc: Orgmode

[-- Attachment #1: Type: text/plain, Size: 1666 bytes --]

Giovanni Ridolfi <giovanni.ridolfi@yahoo.it> writes:

> Hello everyone,
>
> GNU Emacs 24.1.1 (i386-mingw-nt6.1.7601) of 2012-06-10 on MARVIN
> Org-mode version 7.8.11 (eed478ffa @ 
>
>
> I have a problem with babel and calc.
>
> I am not able to pass (as variable) floating point numbers. 
>
> this example works:
>
> #+BEGIN_SRC calc :var thi=20 :var tha=90 :var a=10 :var rho=7854 :var cp=434 :var r=5
> ?tha -? (tha - thi) * exp((-3.0*a)/(rho*cp*r))
> #+END_SRC
>
> #+RESULTS:
> : 20.0001232163
>
>
> However var "r" should be 0.05, but if I use such floating point number I got an error:
>
>
> #+NAME sphere
> #+BEGIN_SRC calc :var thi=20 :var tha=90 :var a=10 :var rho=7854 :var cp=434 :var r=0.05
> ?tha -? (tha - thi) * exp((-3.0*a)/(rho*cp*r))
> #+END_SRC
>
> #+RESULTS:
> | 19 | Expected a number |
>
> This behaviour has been reported also in SO
>
> http://stackoverflow.com/questions/9559221/simple-math-results-in-an-emacs-org-mode-file-with-babel
>

The calc internals are truly perplexing.  In fact, passing floating
point numbers does work as long as division is preformed (see below).
When division and floating point numbers are both present, calc inserts
a ' into its internal representation.  Striping this quote out manually
allows the calculation to proceed.  The attached patch does this
stripping, resulting the in the change of behavior shown below.

I suspect that the ' is inserted for some valid reason, so I wouldn't
recommend actually applying this patch to Org-mode unless/until someone
who is familiar with calc can review it.

Here are some calc blocks before and after the patch.


[-- Attachment #2: babel-calc.org --]
[-- Type: text/x-org, Size: 550 bytes --]

#+Title: some calc examples

#+BEGIN_SRC calc :var rho=7854 :var cp=434 :var r=0.05
  (rho*cp*r)
#+END_SRC

#+RESULTS:
: 170431.8

#+BEGIN_SRC calc :var r=0.10
  1/r
#+END_SRC

↑ before patch
----------------------------------------------------------------------
↓ after patch

#+BEGIN_SRC calc :var r=0.10
  1/r
#+END_SRC

#+RESULTS:
: 10.

#+BEGIN_SRC calc :var thi=20 :var tha=90 :var a=10 :var rho=7854 :var cp=434 :var r=0.05
tha - (tha - thi) * exp((-3.0*a)/(rho*cp*r))
#+END_SRC

#+RESULTS:
: 20.0123205598


[-- Attachment #3: Type: text/plain, Size: 8 bytes --]


Best,


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #4: 0001-strip-quotes-from-calc-internal-representations.patch --]
[-- Type: text/x-patch, Size: 866 bytes --]

From d68c69482013e408c20d0d940abc6bf41b71519c Mon Sep 17 00:00:00 2001
From: Eric Schulte <eric.schulte@gmx.com>
Date: Fri, 20 Jul 2012 08:33:19 -0600
Subject: [PATCH] strip quotes from calc internal representations

This allows more calculations to be performed, but at what cost?

* lisp/ob-calc.el (org-babel-execute:calc): Strip single quotes from
  calc internal representations.
---
 lisp/ob-calc.el | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lisp/ob-calc.el b/lisp/ob-calc.el
index f8ad7e3..8f94bcd 100644
--- a/lisp/ob-calc.el
+++ b/lisp/ob-calc.el
@@ -74,7 +74,7 @@
 		      ((listp res) (error "calc error \"%s\" on input \"%s\""
 					  (cadr res) line))
 		      (t (replace-regexp-in-string
-			  "'\\[" "["
+			  "'" ""
 			  (calc-eval
 			   (math-evaluate-expr
 			    ;; resolve user variables, calc built in
-- 
1.7.11.2


[-- Attachment #5: Type: text/plain, Size: 46 bytes --]


-- 
Eric Schulte
http://cs.unm.edu/~eschulte

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [babel] calc var floating point number error
  2012-07-20 14:34 ` Eric Schulte
@ 2012-07-25 15:32   ` Eric Schulte
  0 siblings, 0 replies; 3+ messages in thread
From: Eric Schulte @ 2012-07-25 15:32 UTC (permalink / raw)
  To: Giovanni Ridolfi; +Cc: Orgmode

Eric Schulte <eric.schulte@gmx.com> writes:

> Giovanni Ridolfi <giovanni.ridolfi@yahoo.it> writes:
>
>> Hello everyone,
>>
>> GNU Emacs 24.1.1 (i386-mingw-nt6.1.7601) of 2012-06-10 on MARVIN
>> Org-mode version 7.8.11 (eed478ffa @ 
>>
>>
>> I have a problem with babel and calc.
>>
>> I am not able to pass (as variable) floating point numbers. 
>>
>> this example works:
>>
>> #+BEGIN_SRC calc :var thi=20 :var tha=90 :var a=10 :var rho=7854 :var cp=434 :var r=5
>> ?tha -? (tha - thi) * exp((-3.0*a)/(rho*cp*r))
>> #+END_SRC
>>
>> #+RESULTS:
>> : 20.0001232163
>>
>>
>> However var "r" should be 0.05, but if I use such floating point number I got an error:
>>
>>
>> #+NAME sphere
>> #+BEGIN_SRC calc :var thi=20 :var tha=90 :var a=10 :var rho=7854 :var cp=434 :var r=0.05
>> ?tha -? (tha - thi) * exp((-3.0*a)/(rho*cp*r))
>> #+END_SRC
>>
>> #+RESULTS:
>> | 19 | Expected a number |
>>
>> This behaviour has been reported also in SO
>>
>> http://stackoverflow.com/questions/9559221/simple-math-results-in-an-emacs-org-mode-file-with-babel
>>
>
> The calc internals are truly perplexing.  In fact, passing floating
> point numbers does work as long as division is preformed (see below).
> When division and floating point numbers are both present, calc inserts
> a ' into its internal representation.  Striping this quote out manually
> allows the calculation to proceed.  The attached patch does this
> stripping, resulting the in the change of behavior shown below.
>
> I suspect that the ' is inserted for some valid reason, so I wouldn't
> recommend actually applying this patch to Org-mode unless/until someone
> who is familiar with calc can review it.
>

Looking more closely at the previous behavior (which does remove the '
in some cases), as compared to the patched behavior (which removes the '
in all cases) I have applied this patch after all.

It is simpler than the previous behavior, and there is at least one case
where it is known to work and the previous behavior is not known to
work.

Best,

-- 
Eric Schulte
http://cs.unm.edu/~eschulte

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-07-25 15:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-07-20 13:55 [babel] calc var floating point number error Giovanni Ridolfi
2012-07-20 14:34 ` Eric Schulte
2012-07-25 15:32   ` Eric Schulte

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).