emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* ob-calc duplicate stack-element issue
@ 2020-03-15 10:46 Heiko Schmidt
  2020-03-15 12:06 ` Eric S Fraga
  2020-03-18 14:47 ` Marco Wahl
  0 siblings, 2 replies; 9+ messages in thread
From: Heiko Schmidt @ 2020-03-15 10:46 UTC (permalink / raw)
  To: emacs-orgmode

Hi there,

the following org snippet is describing my issue.

Thanks in advance,
Heiko

* TODO post problem report about ob-calc duplicate stack-element issue
     :LOGBOOK:
     - State "TODO"       from              [2020-03-15 Sun 09:19]
     :END:

- I want to use ob-calc to do some "literate calculation" with the
   purpose to document what I thought at the time I wrote it.

- problem: When evaluating the calc snippets the top of stack element
   is dropped. Because every "begin/end_src calc" block drops the top
   of stack, one can't reuse the result in following blocks.

- although without expecting any effect here is some version
   information:

     - emacs version: GNU Emacs 24.4.1 (x86_64-pc-linux-gnu, GTK+ Version
     3.14.5) of 2017-09-12 on hullmann, modified by Debian

     - org-version:   9.2

** what I'd like to be able

Number of cars (PKW) in germany:

#+begin_src calc :exports both
45e6
#+end_src

#+RESULTS:
: 45000000.

Yearly mileage in [km/y]

#+begin_src calc :exports both
15000
#+end_src

#+RESULTS:
: 15000

Calculate amount of complete km per year

#+begin_src calc :exports both
'*
#+end_src

#+RESULTS:
: 675000000000.

** the problem I'm facing and the nearly working work-around

- problem: babel removes the resulting top stack element from the
   stack

- tried solution: duplicate the last stack-element on evaluation with
   "' " (emulate <space> press to duplicate the top element of the
   stack in calc)

- side effect: I normally delete trailing whitespace in my files - so
   entering "' " is a dangerous workaround

- I tried to local-set-key the § key to calc-enter which I thought
   should enable a command "'§" to duplicate the top of stack. On
   evaluation this resulted in an error "byte-code: Wrong type
   argument: number-or-marker-p, nil"


Number of cars (PKW) in germany:

#+begin_src calc :exports both
45e6
'<space>
#+end_src

#+RESULTS:
: 45000000.

Yearly mileage in [km/y]

#+begin_src calc :exports both
15000
'<space>
#+end_src

#+RESULTS:
: 15000

Calculate amount of complete km per year

#+begin_src calc :exports both
'*
'<space>
#+end_src

#+RESULTS:
: 675000000000.

** hope for a solution or work around from the community

- preferred: Is there a way to leave the top of stack from one snippet
   to the next (which I don't know)?
- alternative: Is there a way to duplicate the top of stack element
   between begin/end_src calc blocks?
- any advice is appreciated.

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

* Re: ob-calc duplicate stack-element issue
  2020-03-15 10:46 ob-calc duplicate stack-element issue Heiko Schmidt
@ 2020-03-15 12:06 ` Eric S Fraga
  2020-03-15 15:08   ` Heiko Schmidt
  2020-03-18 14:47 ` Marco Wahl
  1 sibling, 1 reply; 9+ messages in thread
From: Eric S Fraga @ 2020-03-15 12:06 UTC (permalink / raw)
  To: Heiko Schmidt; +Cc: emacs-orgmode

On Sunday, 15 Mar 2020 at 11:46, Heiko Schmidt wrote:
> - I want to use ob-calc to do some "literate calculation" with the
>    purpose to document what I thought at the time I wrote it.

I have never managed to get ob-calc to do what I want either so I cannot
help you directly with your query.  Instead, I use embedded calc mode,
along these lines:

--8<---------------cut here---------------start------------->8---
Number of cars (PKW) in germany:

ncars := 45000000.

Yearly mileage in [km/y]

mileage := 15000 km/y

Calculate amount of complete km per year

mileage ncars => 675000000000. km / y

--8<---------------cut here---------------end--------------->8---


You have to activate embedded mode.  By default, calc will look for
expressions surrounded by empty lines.  The := operator assigns values
to variables and the => operator says to show the result here instead of
replacing the expression with the result.

As an aside, calc understands units which is quite nice...

HTH,
eric

-- 
: Eric S Fraga via Emacs 28.0.50, Org release_9.3.6-412-ge18415

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

* Re: ob-calc duplicate stack-element issue
  2020-03-15 12:06 ` Eric S Fraga
@ 2020-03-15 15:08   ` Heiko Schmidt
  0 siblings, 0 replies; 9+ messages in thread
From: Heiko Schmidt @ 2020-03-15 15:08 UTC (permalink / raw)
  To: emacs-orgmode

Several times I made exactly similar experiences with ob-calc as you 
mentioned (units, do what I want), nevertheless I decided to be 
dependent of orgmode and I'd really appreciate to use ob-calc for 
"literate calculation".

"Embedded calc" seems interesting but I refuse using more tools or modes 
because they always tend to create open loops which I'm having enough.

I think ob-calc is underestimated in the org-babel world - there is only 
a few documentation on it. It could be of more value to document 
calculations.

Thanks,
Heiko
On 15.03.20 13:06, Eric S Fraga wrote:
> On Sunday, 15 Mar 2020 at 11:46, Heiko Schmidt wrote:
>> - I want to use ob-calc to do some "literate calculation" with the
>>     purpose to document what I thought at the time I wrote it.
> I have never managed to get ob-calc to do what I want either so I cannot
> help you directly with your query.  Instead, I use embedded calc mode,
> along these lines:
>
> --8<---------------cut here---------------start------------->8---
> Number of cars (PKW) in germany:
>
> ncars := 45000000.
>
> Yearly mileage in [km/y]
>
> mileage := 15000 km/y
>
> Calculate amount of complete km per year
>
> mileage ncars => 675000000000. km / y
>
> --8<---------------cut here---------------end--------------->8---
>
>
> You have to activate embedded mode.  By default, calc will look for
> expressions surrounded by empty lines.  The := operator assigns values
> to variables and the => operator says to show the result here instead of
> replacing the expression with the result.
>
> As an aside, calc understands units which is quite nice...
>
> HTH,
> eric
>

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

* Re: ob-calc duplicate stack-element issue
  2020-03-15 10:46 ob-calc duplicate stack-element issue Heiko Schmidt
  2020-03-15 12:06 ` Eric S Fraga
@ 2020-03-18 14:47 ` Marco Wahl
  2020-03-18 15:50   ` Eric S Fraga
  2020-04-06  4:16   ` Heiko Schmidt
  1 sibling, 2 replies; 9+ messages in thread
From: Marco Wahl @ 2020-03-18 14:47 UTC (permalink / raw)
  To: Heiko Schmidt; +Cc: emacs-orgmode

Heiko Schmidt <Heiko.Schmidt@webbedtables.de> writes:

> - problem: When evaluating the calc snippets the top of stack element
>    is dropped. Because every "begin/end_src calc" block drops the top
>    of stack, one can't reuse the result in following blocks.

> Number of cars (PKW) in germany:
>
> #+begin_src calc :exports both
> 45e6
> #+end_src
>
>
> #+RESULTS:
> : 45000000.
>
> Yearly mileage in [km/y]
>
> #+begin_src calc :exports both
> 15000
> #+end_src
>
>
> #+RESULTS:
> : 15000
>
> Calculate amount of complete km per year
>
> #+begin_src calc :exports both
> '*
> #+end_src
>
>
> #+RESULTS:
> : 675000000000.

> - problem: babel removes the resulting top stack element from the
>    stack
>
> - tried solution: duplicate the last stack-element on evaluation with
>    "' " (emulate <space> press to duplicate the top element of the
>    stack in calc)

> ** hope for a solution or work around from the community
>
> - preferred: Is there a way to leave the top of stack from one snippet
>    to the next (which I don't know)?
> - alternative: Is there a way to duplicate the top of stack element
>    between begin/end_src calc blocks?
> - any advice is appreciated.

Okay.  I take here the "any advice is appreciated" part.

AFAICS at the org babel calc evaluation the last value of the calc stack
gets dropped.

So your workaround is okay, I think.  You can just write any dummy
element at the bottom of each block e.g. just 0.  No need of
duplication.  Looks a bit hackish to me but so what?

Another approach could be "noweb".  Example (you would just evaluate the
block at the bottom):

--8<---------------cut here---------------start------------->8---
Number of cars (PKW) in germany:

#+name: numcars
#+begin_src calc :exports both
45e6
#+end_src

Yearly mileage in [km/y]

#+name: mileage
#+begin_src calc :exports both
15000
#+end_src

Calculate amount of complete km per year

#+begin_src calc :noweb yes
<<numcars>>
<<mileage>>
'*
#+end_src
--8<---------------cut here---------------end--------------->8---


HTH,
-- Marco

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

* Re: ob-calc duplicate stack-element issue
  2020-03-18 14:47 ` Marco Wahl
@ 2020-03-18 15:50   ` Eric S Fraga
  2020-03-18 16:23     ` Marco Wahl
  2020-04-06  4:39     ` Heiko Schmidt
  2020-04-06  4:16   ` Heiko Schmidt
  1 sibling, 2 replies; 9+ messages in thread
From: Eric S Fraga @ 2020-03-18 15:50 UTC (permalink / raw)
  To: Marco Wahl; +Cc: Heiko Schmidt, emacs-orgmode

On Wednesday, 18 Mar 2020 at 15:47, Marco Wahl wrote:
> AFAICS at the org babel calc evaluation the last value of the calc stack
> gets dropped.
>
> So your workaround is okay, I think.  You can just write any dummy
> element at the bottom of each block e.g. just 0.  No need of
> duplication.  Looks a bit hackish to me but so what?

Indeed hackish.  But it does beg the question: why does ob-calc pop the
stack?  I cannot see any use case given that the stack is essentially
infinite and can be safely ignored (in most if not all cases).

Could the solution be to simply remove the =(calc-pop 1)= line at the
end of =org-babel-execute:calc= function?

Just a thought.
-- 
: Eric S Fraga via Emacs 28.0.50, Org release_9.3.6-432-g73bd24

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

* Re: ob-calc duplicate stack-element issue
  2020-03-18 15:50   ` Eric S Fraga
@ 2020-03-18 16:23     ` Marco Wahl
  2020-03-18 16:41       ` Eric S Fraga
  2020-04-06  4:39     ` Heiko Schmidt
  1 sibling, 1 reply; 9+ messages in thread
From: Marco Wahl @ 2020-03-18 16:23 UTC (permalink / raw)
  To: e.fraga; +Cc: Heiko Schmidt, emacs-orgmode

Eric S Fraga <e.fraga@ucl.ac.uk> writes:

> On Wednesday, 18 Mar 2020 at 15:47, Marco Wahl wrote:
>> AFAICS at the org babel calc evaluation the last value of the calc stack
>> gets dropped.
>>
>> So your workaround is okay, I think.  You can just write any dummy
>> element at the bottom of each block e.g. just 0.  No need of
>> duplication.  Looks a bit hackish to me but so what?
>
> Indeed hackish.  But it does beg the question: why does ob-calc pop the
> stack?  I cannot see any use case given that the stack is essentially
> infinite and can be safely ignored (in most if not all cases).
>
> Could the solution be to simply remove the =(calc-pop 1)= line at the
> end of =org-babel-execute:calc= function?
>
> Just a thought.

Possibly the originators thought about the typical use of calc blocks as
units which reduce to the top element of the stack.  In this case the
calc-pop would be the right step to clean the stack.

Just another thought.


Ciao,
-- Marco

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

* Re: ob-calc duplicate stack-element issue
  2020-03-18 16:23     ` Marco Wahl
@ 2020-03-18 16:41       ` Eric S Fraga
  0 siblings, 0 replies; 9+ messages in thread
From: Eric S Fraga @ 2020-03-18 16:41 UTC (permalink / raw)
  To: Marco Wahl; +Cc: Heiko Schmidt, emacs-orgmode

On Wednesday, 18 Mar 2020 at 17:23, Marco Wahl wrote:
> Possibly the originators thought about the typical use of calc blocks as
> units which reduce to the top element of the stack.  In this case the
> calc-pop would be the right step to clean the stack.

Could be.  Indeed, at the moment, if you pop up calc directly, it does
look the same as however you left it the last time you used it and not
popping the stack would ruin that feature.

So, changing ob-calc to not pop the result off might be undesirable
after all.

-- 
: Eric S Fraga via Emacs 28.0.50, Org release_9.3.6-432-g73bd24

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

* Re: ob-calc duplicate stack-element issue
  2020-03-18 14:47 ` Marco Wahl
  2020-03-18 15:50   ` Eric S Fraga
@ 2020-04-06  4:16   ` Heiko Schmidt
  1 sibling, 0 replies; 9+ messages in thread
From: Heiko Schmidt @ 2020-04-06  4:16 UTC (permalink / raw)
  To: emacs-orgmode

Hi Marco,


On 18.03.20 15:47, Marco Wahl wrote:
> Okay. I take here the "any advice is appreciated" part.
> AFAICS at the org babel calc evaluation the last value of the calc stack
> gets dropped.
>
> So your workaround is okay, I think.  You can just write any dummy
> element at the bottom of each block e.g. just 0.  No need of
> duplication.  Looks a bit hackish to me but so what?
Oh no. You're completely right. This simplifying clarification is 
already quite helpful. Just putting 0 on top of stack is enough. 
Terrifying simple.
> Another approach could be "noweb".  Example (you would just evaluate the
> block at the bottom):
>
> --8<---------------cut here---------------start------------->8---
> Number of cars (PKW) in germany:
>
> #+name: numcars
> #+begin_src calc :exports both
> 45e6
> #+end_src
>
> Yearly mileage in [km/y]
>
> #+name: mileage
> #+begin_src calc :exports both
> 15000
> #+end_src
>
> Calculate amount of complete km per year
>
> #+begin_src calc :noweb yes
> <<numcars>>
> <<mileage>>
> '*
> #+end_src
> --8<---------------cut here---------------end--------------->8---
>
>
> HTH,
> -- Marco
>
 From my point of view "variablizing" distracts me to much. I'd prefer 
more to comment via "literate" description of the numbers.


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

* Re: ob-calc duplicate stack-element issue
  2020-03-18 15:50   ` Eric S Fraga
  2020-03-18 16:23     ` Marco Wahl
@ 2020-04-06  4:39     ` Heiko Schmidt
  1 sibling, 0 replies; 9+ messages in thread
From: Heiko Schmidt @ 2020-04-06  4:39 UTC (permalink / raw)
  To: emacs-orgmode

On 18.03.20 16:50, Eric S Fraga wrote:
>
> Indeed hackish.  But it does beg the question: why does ob-calc pop the
> stack?  I cannot see any use case given that the stack is essentially
> infinite and can be safely ignored (in most if not all cases).
>
> Could the solution be to simply remove the =(calc-pop 1)= line at the
> end of =org-babel-execute:calc= function?
>
> Just a thought.

After removing

(calc-pop 1)

  from =org-babel-execute:calc= the problem is perfectly solved.

Thanks a lot.




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

end of thread, other threads:[~2020-04-06  4:40 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-03-15 10:46 ob-calc duplicate stack-element issue Heiko Schmidt
2020-03-15 12:06 ` Eric S Fraga
2020-03-15 15:08   ` Heiko Schmidt
2020-03-18 14:47 ` Marco Wahl
2020-03-18 15:50   ` Eric S Fraga
2020-03-18 16:23     ` Marco Wahl
2020-03-18 16:41       ` Eric S Fraga
2020-04-06  4:39     ` Heiko Schmidt
2020-04-06  4:16   ` Heiko Schmidt

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).