emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Calc: Multiply time (hours) with a float
@ 2016-09-08  8:33 Karl Voit
  2016-09-11 11:45 ` Karl Voit
  0 siblings, 1 reply; 16+ messages in thread
From: Karl Voit @ 2016-09-08  8:33 UTC (permalink / raw)
  To: emacs-orgmode

Hi!

I do have following situation: I'd like to multiply a float with hours
which results in an error, obviously. However, I was not able to find
out how to do it.

Minimal example with expected product:

| time [h:m:s] |   value |   product |
|--------------+---------+-----------|
|     09:15:00 | 2.54321 | 23.524693 |
#+TBLFM: @2$3=$2*$1




In case you have time and energy to go deeper in my rabbit hole:


My real world example which is a bit more complex in case you want to
find a totally different way (workaround?) of calculating the stuff I
need:

First table determines the reference hours per day of my lawn mowing
robot with the current program:

#+NAME: robot-program
| Status As Of     | Start |   End | Start |   End | Hours/Day |
|------------------+-------+-------+-------+-------+-----------|
| <2016-06-30 Thu> | 11:30 | 19:30 | 20:30 | 21:45 |  09:15:00 |
#+TBLFM: $6=($3-$2)+($5-$4);T

Second table holds the logged number of hours of work for certain time
stamps (first three columns). The thing I want to calculate is the
number of hours the robot was *supposed* to work. Unfortunately, the
robot gets stuck from time to time and therefore the logged working
hours differ from the theoretical working hours (reference).

For a better understanding, I split the calculation of $6 (my desired
comparison value) and added $4 (the daily hour reference value) and $5
(difference between the current row and the previous one in time).

Current situation with the ERROR:

| Date                   | Hours | Difference [h] | Daily Reference | Difference [Days] | Reference Comparison [h] |
|------------------------+-------+----------------+-----------------+-------------------+--------------------------|
| <2016-08-13 Sat>       |   100 |            100 |                 |                   |                          |
| <2016-08-20 Sat 14:40> |   217 |            117 |        09:15:00 |          7.611111 | #ERROR                   |
| <2016-08-27 Sat 19:08> |   245 |             28 |        09:15:00 |          7.186111 | #ERROR                   |
#+TBLFM: $3=$2-subscr(@<$2..@>$2, @# - 1);N :: @3$4..@>$4=remote(robot-program,@>$6);T :: @3$5..@>$5=($1 - subscr(@<$1..@>$1, @# - 1)) :: @3$6..@>$6= $5 * $4

Desired output:

| Date                   | Hours | Difference [h] | Daily Reference | Difference [Days] | Reference Comparison [h] |
|------------------------+-------+----------------+-----------------+-------------------+--------------------------|
| <2016-08-13 Sat>       |   100 |            100 |                 |                   |                          |
| <2016-08-20 Sat 14:40> |   217 |            117 |        09:15:00 |          7.611111 |                70.402777 |
| <2016-08-27 Sat 19:08> |   245 |             28 |        09:15:00 |          7.186111 |                66.471527 |

Nitpicking: yes, comparing reference*hours (of whole days) to log
values which were logged in-between working hours is not exact even
with zero percent stuck time. I'd like to have just a rough
comparison.

So if my desired output can be derived without multiplying hours to
floats: I'm open ;-)

-- 
mail|git|SVN|photos|postings|SMS|phonecalls|RSS|CSV|XML to Org-mode:
       > get Memacs from https://github.com/novoid/Memacs <

https://github.com/novoid/extract_pdf_annotations_to_orgmode + more on github

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

* Re: Calc: Multiply time (hours) with a float
  2016-09-08  8:33 Calc: Multiply time (hours) with a float Karl Voit
@ 2016-09-11 11:45 ` Karl Voit
  2016-09-11 12:12   ` Michael Welle
  0 siblings, 1 reply; 16+ messages in thread
From: Karl Voit @ 2016-09-11 11:45 UTC (permalink / raw)
  To: emacs-orgmode

* Karl Voit <devnull@Karl-Voit.at> wrote:
> Hi!
>
> I do have following situation: I'd like to multiply a float with hours
> which results in an error, obviously. However, I was not able to find
> out how to do it.
>
> Minimal example with expected product:
> 
> | time [h:m:s] |   value | h * value |
> |--------------+---------+-----------|
> |     09:15:00 | 2.54321 | 23.524693 |
>  #+TBLFM: @2$3=$2*$1

I guess the last resort would be to do this would be with an elisp
function.

Is there no other way?

-- 
mail|git|SVN|photos|postings|SMS|phonecalls|RSS|CSV|XML to Org-mode:
       > get Memacs from https://github.com/novoid/Memacs <

https://github.com/novoid/extract_pdf_annotations_to_orgmode + more on github

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

* Re: Calc: Multiply time (hours) with a float
  2016-09-11 11:45 ` Karl Voit
@ 2016-09-11 12:12   ` Michael Welle
  2016-09-18 16:36     ` Karl Voit
  0 siblings, 1 reply; 16+ messages in thread
From: Michael Welle @ 2016-09-11 12:12 UTC (permalink / raw)
  To: emacs-orgmode

Hello,

Karl Voit <devnull@Karl-Voit.at> writes:

> * Karl Voit <devnull@Karl-Voit.at> wrote:
>> Hi!
>>
>> I do have following situation: I'd like to multiply a float with hours
>> which results in an error, obviously. However, I was not able to find
>> out how to do it.
>>
>> Minimal example with expected product:
>> 
>> | time [h:m:s] |   value | h * value |
>> |--------------+---------+-----------|
>> |     09:15:00 | 2.54321 | 23.524693 |
>>  #+TBLFM: @2$3=$2*$1
>
> I guess the last resort would be to do this would be with an elisp
> function.
>
> Is there no other way?
do you really need the result to be in decimal notation? If not, 
#+TBLFM:@2$3=$2*$1;T would work. If you need decimal notation you have
to provide your own function, I think.

Regards
hmw

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

* Re: Calc: Multiply time (hours) with a float
  2016-09-11 12:12   ` Michael Welle
@ 2016-09-18 16:36     ` Karl Voit
  2016-09-19  6:49       ` Michael Welle
  0 siblings, 1 reply; 16+ messages in thread
From: Karl Voit @ 2016-09-18 16:36 UTC (permalink / raw)
  To: emacs-orgmode

* Michael Welle <mwe012008@gmx.net> wrote:
> Hello,

Hello Michael,

> Karl Voit <devnull@Karl-Voit.at> writes:
>
>> * Karl Voit <devnull@Karl-Voit.at> wrote:
>>> Hi!
>>>
>>> I do have following situation: I'd like to multiply a float with hours
>>> which results in an error, obviously. However, I was not able to find
>>> out how to do it.
>>>
>>> Minimal example with expected product:
>>> 
>>> | time [h:m:s] |   value | h * value |
>>> |--------------+---------+-----------|
>>> |     09:15:00 | 2.54321 | 23.524693 |
>>>  #+TBLFM: @2$3=$2*$1
>>
> do you really need the result to be in decimal notation? 

If I want to implement my original idea (see my original posting):
yes.

> If not, 
> #+TBLFM:@2$3=$2*$1;T would work. If you need decimal notation you have
> to provide your own function, I think.

So I wrote some Elisp code:

  (defun org-time-string-to-seconds (s)
    "Convert a string HH:MM:SS to a number of seconds.
     Omitted third element will be interpreted as MM:SS with missing hours."
    ;; test with:
    ;; (message (concat "result is: " (number-to-string (org-time-string-to-seconds "57:45:03"))))
    ;; (message (concat "result is: " (number-to-string (org-time-string-to-seconds "57:45"))))
    (cond
     ((and (stringp s)
       (string-match "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)" s))
      (let ((hour (string-to-number (match-string 1 s)))
        (min (string-to-number (match-string 2 s)))
        (sec (string-to-number (match-string 3 s))))
    (+ (* hour 3600) (* min 60) sec)))
     ((and (stringp s)
       (string-match "\\([0-9]+\\):\\([0-9]+\\)" s))
      (let ((min (string-to-number (match-string 1 s)))
        (sec (string-to-number (match-string 2 s))))
    (+ (* min 60) sec)))
     ;;((stringp s) (string-to-number s))
     ;;(t s)
     )
    )

  (defun org-time-string-to-hours (s)
    "Convert a string HH:MM:SS to hours (float).
     When only two values given, they will be interpreted as MM:SS with missing hours."
    ;; test via:
    ;; (message (concat "result is: " (number-to-string (org-time-string-to-hours "57:45:03"))))
    ;; (message (concat "result is: " (number-to-string (org-time-string-to-hours "57:45"))))
    (/ (org-time-string-to-seconds s) 3600.0)
    )

... which is working:

| time [h:m:s] |   value | hours [float] |   product |
|--------------+---------+---------------+-----------|
|     09:15:00 | 2.54321 |          9.25 | 23.524693 |
#+TBLFM: $4=$2*$3::@2$3='(org-time-string-to-hours $1)

However, when I try to calculate the product in one step, I get an error:

| time [h:m:s] |   value | product |
|--------------+---------+---------|
|     09:15:00 | 2.54321 | #ERROR  |
#+TBLFM: @2$3='(* $2 (org-time-string-to-hours $1))

When I replace "$2" in TBLFM with an integer value, it works.

How can I calculate the product in /one/ step?

Thanks again!

-- 
mail|git|SVN|photos|postings|SMS|phonecalls|RSS|CSV|XML to Org-mode:
       > get Memacs from https://github.com/novoid/Memacs <

https://github.com/novoid/extract_pdf_annotations_to_orgmode + more on github

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

* Re: Calc: Multiply time (hours) with a float
  2016-09-18 16:36     ` Karl Voit
@ 2016-09-19  6:49       ` Michael Welle
  2016-09-19  6:55         ` Michael Welle
  0 siblings, 1 reply; 16+ messages in thread
From: Michael Welle @ 2016-09-19  6:49 UTC (permalink / raw)
  To: emacs-orgmode

Hello,

Karl Voit <devnull@Karl-Voit.at> writes:

> * Michael Welle <mwe012008@gmx.net> wrote:
[...]
> ... which is working:
>
> | time [h:m:s] |   value | hours [float] |   product |
> |--------------+---------+---------------+-----------|
> |     09:15:00 | 2.54321 |          9.25 | 23.524693 |
>
> #+TBLFM: $4=$2*$3::@2$3='(org-time-string-to-hours $1)
>
> However, when I try to calculate the product in one step, I get an error:
>
> | time [h:m:s] |   value | product |
> |--------------+---------+---------|
> |     09:15:00 | 2.54321 | #ERROR  |
>
> #+TBLFM: @2$3='(* $2 (org-time-string-to-hours $1))
>
> When I replace "$2" in TBLFM with an integer value, it works.
>
> How can I calculate the product in /one/ step?
chapter 3.5.3 of Org's info manual might be of interest here. I haven't
fully 'inhaled' it, but like this should work:

#+TBLFM: @2$3='(* $2 (org-time-string-to-hours "$1"));L

Regards
hmw

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

* Re: Calc: Multiply time (hours) with a float
  2016-09-19  6:49       ` Michael Welle
@ 2016-09-19  6:55         ` Michael Welle
  2016-09-19 11:43           ` Karl Voit
  0 siblings, 1 reply; 16+ messages in thread
From: Michael Welle @ 2016-09-19  6:55 UTC (permalink / raw)
  To: emacs-orgmode

Hello,

Michael Welle <mwe012008@gmx.net> writes:
[...]
> #+TBLFM: @2$3='(* $2 (org-time-string-to-hours "$1"));L
or converting $2 to a number before the calculation should work also,
but is more to write ;).

Regards
hmw

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

* Re: Calc: Multiply time (hours) with a float
  2016-09-19  6:55         ` Michael Welle
@ 2016-09-19 11:43           ` Karl Voit
  2016-09-19 12:41             ` Michael Welle
  0 siblings, 1 reply; 16+ messages in thread
From: Karl Voit @ 2016-09-19 11:43 UTC (permalink / raw)
  To: emacs-orgmode

Hallo Michael,

thanks for the pointer to http://orgmode.org/org.html#Formula-syntax-for-Lisp -
it was very helpful for my understanding.

* Michael Welle <mwe012008@gmx.net> wrote:
> Hello,
>
> Michael Welle <mwe012008@gmx.net> writes:
> [...]
>> #+TBLFM: @2$3='(* $2 (org-time-string-to-hours "$1"));L
> or converting $2 to a number before the calculation should work also,
> but is more to write ;).

Hm. I still got #ERROR. I reduced my issue to calculate the float (for hours)
without multiplying it with "value":

| time [h:m:s] |   value | product |
|--------------+---------+---------|
|     09:15:00 | 2.54321 | #ERROR  |
#+TBLFM: @2$3='(org-time-string-to-hours $1)
#+TBLFM: @2$3='(org-time-string-to-hours $1);L
#+TBLFM: @2$3='(org-time-string-to-hours "$1");N

... each of the TBLFM results in the same error.

At this stage, $2 is not my problem any more.

Do you have any further idea?

-- 
mail|git|SVN|photos|postings|SMS|phonecalls|RSS|CSV|XML to Org-mode:
       > get Memacs from https://github.com/novoid/Memacs <

https://github.com/novoid/extract_pdf_annotations_to_orgmode + more on github

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

* Re: Calc: Multiply time (hours) with a float
  2016-09-19 11:43           ` Karl Voit
@ 2016-09-19 12:41             ` Michael Welle
  2016-09-19 13:18               ` Karl Voit
  0 siblings, 1 reply; 16+ messages in thread
From: Michael Welle @ 2016-09-19 12:41 UTC (permalink / raw)
  To: emacs-orgmode

Hello,

Karl Voit <devnull@Karl-Voit.at> writes:

> Hallo Michael,
>
> thanks for the pointer to http://orgmode.org/org.html#Formula-syntax-for-Lisp -
> it was very helpful for my understanding.
>
> * Michael Welle <mwe012008@gmx.net> wrote:
>> Hello,
>>
>> Michael Welle <mwe012008@gmx.net> writes:
>> [...]
>>> #+TBLFM: @2$3='(* $2 (org-time-string-to-hours "$1"));L
>> or converting $2 to a number before the calculation should work also,
>> but is more to write ;).
>
> Hm. I still got #ERROR. I reduced my issue to calculate the float (for hours)
> without multiplying it with "value":
strange. I use the current Org version from git and Emacs 25.1.


> | time [h:m:s] |   value | product |
> |--------------+---------+---------|
> |     09:15:00 | 2.54321 | #ERROR  |
>
> #+TBLFM: @2$3='(org-time-string-to-hours $1)
That works for me. The default interpretation of $1 is used and
therefore the value is fed into o-t-s-t-h as string.


> #+TBLFM: @2$3='(org-time-string-to-hours $1);L
That doesn't work. I'm not sure, but I guess it is because you asked for 
literal interpretation and it is not clear what that would mean for
09:15:00.


> #+TBLFM: @2$3='(org-time-string-to-hours "$1");N
Here you ask for interpretation as numbers. So the o-t-s-t-h function
sees a stringified 9 as it's parameter value. Near, but still no t-shirt ;). 


> ... each of the TBLFM results in the same error.
Well, none of them looks like the one I wrote:

#+TBLFM: @2$3='(* $2 (org-time-string-to-hours "$1"));L

You said, that gives an error, too. I have no idea, why. You can use
(message...) and (type-of ...) to look at the types and values that you
feed into your functions. Maybe that gives some insight?

The variant with converting $2 to a number is:
#+TBLFM: @2$3='(* (string-to-number $2) (org-time-string-to-hours $1))

Since the default interpretation is to use strings, the multiplication
sees $2 as a number and $1 is fed to your function as a string, as
required. 

Regards
hmw

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

* Re: Calc: Multiply time (hours) with a float
  2016-09-19 12:41             ` Michael Welle
@ 2016-09-19 13:18               ` Karl Voit
  2016-09-19 13:49                 ` Michael Welle
                                   ` (2 more replies)
  0 siblings, 3 replies; 16+ messages in thread
From: Karl Voit @ 2016-09-19 13:18 UTC (permalink / raw)
  To: emacs-orgmode

* Michael Welle <mwe012008@gmx.net> wrote:
>
> Karl Voit <devnull@Karl-Voit.at> writes:
>
>>> [...]
>>>> #+TBLFM: @2$3='(* $2 (org-time-string-to-hours "$1"));L
>>> or converting $2 to a number before the calculation should work also,
>>> but is more to write ;).
>>
>> Hm. I still got #ERROR. I reduced my issue to calculate the float (for hours)
>> without multiplying it with "value":
> strange. I use the current Org version from git and Emacs 25.1.
>
>> | time [h:m:s] |   value | product |
>> |--------------+---------+---------|
>> |     09:15:00 | 2.54321 | #ERROR  |
>>
>> #+TBLFM: @2$3='(org-time-string-to-hours $1)
> That works for me. The default interpretation of $1 is used and
> therefore the value is fed into o-t-s-t-h as string.

OK, this is my current issue then.

I'm using Org from git/maint version 8.3.4
(release_8.3.4-33-gd522fc) and GNU Emacs 24.5.1 (i686-pc-mingw32
Windows) as well as Emacs 24.4.1 on GNU/Linux.

Can somebody confirm my #ERROR behavior?

>> #+TBLFM: @2$3='(org-time-string-to-hours $1);L
> That doesn't work. I'm not sure, but I guess it is because you asked for 
> literal interpretation and it is not clear what that would mean for
> 09:15:00.

I'd say the same.

>> #+TBLFM: @2$3='(org-time-string-to-hours "$1");N
> Here you ask for interpretation as numbers. So the o-t-s-t-h function
> sees a stringified 9 as it's parameter value. Near, but still no t-shirt ;). 

I was wondering whether or not the stringification is done before or
after the number-conversion - as a test. It's an #ERROR anyway.

>> ... each of the TBLFM results in the same error.
> Well, none of them looks like the one I wrote:
>
> #+TBLFM: @2$3='(* $2 (org-time-string-to-hours "$1"));L

... which I tried first, got an #ERROR and analyzed the issue.
Narrowing down to '(org-time-string-to-hours $1) is my issue since
I now understand the handling of $2 which is according to the
documentation.

> You said, that gives an error, too. I have no idea, why. You can use
> (message...) and (type-of ...) to look at the types and values that you
> feed into your functions. Maybe that gives some insight?

(type-of ...) -> very helpful

| time [h:m:s] |   value | product |
|--------------+---------+---------|
|     09:15:00 | 2.54321 | #ERROR  |
#+TBLFM: @2$3='(message (type-of (org-time-string-to-hours $1)))
#+TBLFM: @2$3='(type-of (org-time-string-to-hours $1))

... both formulas lead to #ERROR in @2$3 with no message in the
*Message* buffer.

From my understanding, the first one should have caused an output of
the type in the *Message* buffer and the second one should have
placed something like "integer" into @2$3. Am I correct?

> The variant with converting $2 to a number is:
> #+TBLFM: @2$3='(* (string-to-number $2) (org-time-string-to-hours $1))

Since the issue is with $1, the (string-to-number $2) (which is
working) doesn't fix the #ERROR issue of $1 :-(


Thank you *very* much for your explanations! I (and hopefully many
others as well) am learning a lot here.

-- 
mail|git|SVN|photos|postings|SMS|phonecalls|RSS|CSV|XML to Org-mode:
       > get Memacs from https://github.com/novoid/Memacs <

https://github.com/novoid/extract_pdf_annotations_to_orgmode + more on github

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

* Re: Calc: Multiply time (hours) with a float
  2016-09-19 13:18               ` Karl Voit
@ 2016-09-19 13:49                 ` Michael Welle
  2016-09-19 15:33                   ` Nick Dokos
  2016-09-19 15:33                 ` Bernhard Pröll
  2016-09-20 14:23                 ` Calc: Multiply time (hours) with a float - solved by updating Org Karl Voit
  2 siblings, 1 reply; 16+ messages in thread
From: Michael Welle @ 2016-09-19 13:49 UTC (permalink / raw)
  To: emacs-orgmode

Hello,

Karl Voit <devnull@Karl-Voit.at> writes:

> * Michael Welle <mwe012008@gmx.net> wrote:
>>
>> Karl Voit <devnull@Karl-Voit.at> writes:
>>
>>>> [...]
>>>>> #+TBLFM: @2$3='(* $2 (org-time-string-to-hours "$1"));L
>>>> or converting $2 to a number before the calculation should work also,
>>>> but is more to write ;).
>>>
>>> Hm. I still got #ERROR. I reduced my issue to calculate the float (for hours)
>>> without multiplying it with "value":
>> strange. I use the current Org version from git and Emacs 25.1.
>>
>>> | time [h:m:s] |   value | product |
>>> |--------------+---------+---------|
>>> |     09:15:00 | 2.54321 | #ERROR  |
>>>
>>> #+TBLFM: @2$3='(org-time-string-to-hours $1)
>> That works for me. The default interpretation of $1 is used and
>> therefore the value is fed into o-t-s-t-h as string.
>
> OK, this is my current issue then.
The o-t-s-t-s function expect it to be a string, doesn't it? You test it
with (stringp s) there.


[...]
>>> #+TBLFM: @2$3='(org-time-string-to-hours "$1");N
>> Here you ask for interpretation as numbers. So the o-t-s-t-h function
>> sees a stringified 9 as it's parameter value. Near, but still no t-shirt ;). 
>
> I was wondering whether or not the stringification is done before or
> after the number-conversion - as a test. It's an #ERROR anyway.
The double quotes around $1 make it a string and your function parameter
named s is of type string. As I said, your function sees a "9" in this
variant and isn't prepared to work with that as input. That surely
results in an error.


>>> ... each of the TBLFM results in the same error.
>> Well, none of them looks like the one I wrote:
>>
>> #+TBLFM: @2$3='(* $2 (org-time-string-to-hours "$1"));L
>
> ... which I tried first, got an #ERROR and analyzed the issue.
> Narrowing down to '(org-time-string-to-hours $1) is my issue since
> I now understand the handling of $2 which is according to the
> documentation.
>
>> You said, that gives an error, too. I have no idea, why. You can use
>> (message...) and (type-of ...) to look at the types and values that you
>> feed into your functions. Maybe that gives some insight?
>
> (type-of ...) -> very helpful
>
> | time [h:m:s] |   value | product |
> |--------------+---------+---------|
> |     09:15:00 | 2.54321 | #ERROR  |
>
> #+TBLFM: @2$3='(message (type-of (org-time-string-to-hours $1)))
> #+TBLFM: @2$3='(type-of (org-time-string-to-hours $1))
Try (message "type: %s" (type-of (org-time-string-to-hours $1)))


> ... both formulas lead to #ERROR in @2$3 with no message in the
> *Message* buffer.
>
> From my understanding, the first one should have caused an output of
> the type in the *Message* buffer and the second one should have
> placed something like "integer" into @2$3. Am I correct?
In the table above both formulas place the value of the evaluated form
in the product cell of the table. If you change the message example as
suggested above, the product cell will contain something like "type:
float", the second formula will insert just the type name: "float" in
that case. 


>> The variant with converting $2 to a number is:
>> #+TBLFM: @2$3='(* (string-to-number $2) (org-time-string-to-hours $1))
>
> Since the issue is with $1, the (string-to-number $2) (which is
> working) doesn't fix the #ERROR issue of $1 :-(
You can put

(message "type: '%s' value '%s'" (type-of s) s)

into your o-t-s-t-seconds function, right before the cond form. That
should print out a type of string and a value that can be processed by
the logic of that function, i.e. just "9" doesn't work, because of the
regexp you use as conditions.


> Thank you *very* much for your explanations! I (and hopefully many
> others as well) am learning a lot here.
What I find a bit strange is that things that work for me, doesn't work
in your environment. At first glance your function doesn't look so
special, IMO.

Regards
hmw

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

* Re: Calc: Multiply time (hours) with a float
  2016-09-19 13:18               ` Karl Voit
  2016-09-19 13:49                 ` Michael Welle
@ 2016-09-19 15:33                 ` Bernhard Pröll
  2016-09-20 14:23                 ` Calc: Multiply time (hours) with a float - solved by updating Org Karl Voit
  2 siblings, 0 replies; 16+ messages in thread
From: Bernhard Pröll @ 2016-09-19 15:33 UTC (permalink / raw)
  To: Karl Voit; +Cc: emacs-orgmode

Hi,

FWIW, there is a org-table-toggle-formula-debugger command.


On Mon, Sep 19 2016, Karl Voit wrote:

> * Michael Welle <mwe012008@gmx.net> wrote:
>>
>> Karl Voit <devnull@Karl-Voit.at> writes:
>>
>>>> [...]
>>>>> #+TBLFM: @2$3='(* $2 (org-time-string-to-hours "$1"));L
>>>> or converting $2 to a number before the calculation should work also,
>>>> but is more to write ;).
>>>
>>> Hm. I still got #ERROR. I reduced my issue to calculate the float (for hours)
>>> without multiplying it with "value":
>> strange. I use the current Org version from git and Emacs 25.1.
>>
>>> | time [h:m:s] |   value | product |
>>> |--------------+---------+---------|
>>> |     09:15:00 | 2.54321 | #ERROR  |
>>>
>>> #+TBLFM: @2$3='(org-time-string-to-hours $1)
>> That works for me. The default interpretation of $1 is used and
>> therefore the value is fed into o-t-s-t-h as string.
>
> OK, this is my current issue then.
>
> I'm using Org from git/maint version 8.3.4
> (release_8.3.4-33-gd522fc) and GNU Emacs 24.5.1 (i686-pc-mingw32
> Windows) as well as Emacs 24.4.1 on GNU/Linux.
>
> Can somebody confirm my #ERROR behavior?
>
>>> #+TBLFM: @2$3='(org-time-string-to-hours $1);L
>> That doesn't work. I'm not sure, but I guess it is because you asked for 
>> literal interpretation and it is not clear what that would mean for
>> 09:15:00.
>
> I'd say the same.
>
>>> #+TBLFM: @2$3='(org-time-string-to-hours "$1");N
>> Here you ask for interpretation as numbers. So the o-t-s-t-h function
>> sees a stringified 9 as it's parameter value. Near, but still no t-shirt ;). 
>
> I was wondering whether or not the stringification is done before or
> after the number-conversion - as a test. It's an #ERROR anyway.
>
>>> ... each of the TBLFM results in the same error.
>> Well, none of them looks like the one I wrote:
>>
>> #+TBLFM: @2$3='(* $2 (org-time-string-to-hours "$1"));L
>
> ... which I tried first, got an #ERROR and analyzed the issue.
> Narrowing down to '(org-time-string-to-hours $1) is my issue since
> I now understand the handling of $2 which is according to the
> documentation.
>
>> You said, that gives an error, too. I have no idea, why. You can use
>> (message...) and (type-of ...) to look at the types and values that you
>> feed into your functions. Maybe that gives some insight?
>
> (type-of ...) -> very helpful
>
> | time [h:m:s] |   value | product |
> |--------------+---------+---------|
> |     09:15:00 | 2.54321 | #ERROR  |
>
> #+TBLFM: @2$3='(message (type-of (org-time-string-to-hours $1)))
> #+TBLFM: @2$3='(type-of (org-time-string-to-hours $1))
>
> ... both formulas lead to #ERROR in @2$3 with no message in the
> *Message* buffer.
>
> From my understanding, the first one should have caused an output of
> the type in the *Message* buffer and the second one should have
> placed something like "integer" into @2$3. Am I correct?
>
>> The variant with converting $2 to a number is:
>> #+TBLFM: @2$3='(* (string-to-number $2) (org-time-string-to-hours $1))
>
> Since the issue is with $1, the (string-to-number $2) (which is
> working) doesn't fix the #ERROR issue of $1 :-(
>
>
> Thank you *very* much for your explanations! I (and hopefully many
> others as well) am learning a lot here.

-- 
Bernhard

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

* Re: Calc: Multiply time (hours) with a float
  2016-09-19 13:49                 ` Michael Welle
@ 2016-09-19 15:33                   ` Nick Dokos
  2016-09-19 17:45                     ` Michael Welle
  0 siblings, 1 reply; 16+ messages in thread
From: Nick Dokos @ 2016-09-19 15:33 UTC (permalink / raw)
  To: emacs-orgmode

Michael Welle <mwe012008@gmx.net> writes:

> Hello,
>
> Karl Voit <devnull@Karl-Voit.at> writes:
>
>> * Michael Welle <mwe012008@gmx.net> wrote:
>>>
>>> Karl Voit <devnull@Karl-Voit.at> writes:
>>>
>>>>> [...]
>>>>>> #+TBLFM: @2$3='(* $2 (org-time-string-to-hours "$1"));L
>>>>> or converting $2 to a number before the calculation should work also,
>>>>> but is more to write ;).
>>>>
>>>> Hm. I still got #ERROR. I reduced my issue to calculate the float (for hours)
>>>> without multiplying it with "value":
>>> strange. I use the current Org version from git and Emacs 25.1.
>>>
>>>> | time [h:m:s] |   value | product |
>>>> |--------------+---------+---------|
>>>> |     09:15:00 | 2.54321 | #ERROR  |
>>>>
>>>> #+TBLFM: @2$3='(org-time-string-to-hours $1)
>>> That works for me. The default interpretation of $1 is used and
>>> therefore the value is fed into o-t-s-t-h as string.
>>
>> OK, this is my current issue then.

FWIW, I used the above table with the definitions in an org file, C-C C-c'd the code,
and C-c C-c'd the table formula - I get no error:

--8<---------------cut here---------------start------------->8---
* table error
strange. I use the current Org version from git and Emacs 25.1.

| time [h:m:s] |   value | product |
|--------------+---------+---------|
|     09:15:00 | 2.54321 |    9.25 |
#+TBLFM: @2$3='(org-time-string-to-hours $1)

* code

#+BEGIN_SRC elisp
  (defun org-time-string-to-seconds (s)
    "Convert a string HH:MM:SS to a number of seconds.
     Omitted third element will be interpreted as MM:SS with missing hours."
    ;; test with:
    ;; (message (concat "result is: " (number-to-string (org-time-string-to-seconds "57:45:03"))))
    ;; (message (concat "result is: " (number-to-string (org-time-string-to-seconds "57:45"))))
    (cond
     ((and (stringp s)
       (string-match "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)" s))
      (let ((hour (string-to-number (match-string 1 s)))
        (min (string-to-number (match-string 2 s)))
        (sec (string-to-number (match-string 3 s))))
    (+ (* hour 3600) (* min 60) sec)))
     ((and (stringp s)
       (string-match "\\([0-9]+\\):\\([0-9]+\\)" s))
      (let ((min (string-to-number (match-string 1 s)))
        (sec (string-to-number (match-string 2 s))))
    (+ (* min 60) sec)))
     ;;((stringp s) (string-to-number s))
     ;;(t s)
     )
    )

  (defun org-time-string-to-hours (s)
    "Convert a string HH:MM:SS to hours (float).
     When only two values given, they will be interpreted as MM:SS with missing hours."
    ;; test via:
    ;; (message (concat "result is: " (number-to-string (org-time-string-to-hours "57:45:03"))))
    ;; (message (concat "result is: " (number-to-string (org-time-string-to-hours "57:45"))))
    (/ (org-time-string-to-seconds s) 3600.0)
    )

#+END_SRC

#+RESULTS:
: org-time-string-to-hours

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

Org-mode version 8.3.6 (release_8.3.6-1135-g0ba465
GNU Emacs 25.1.50.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.20.9) of 2016-08-31

-- 
Nick

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

* Re: Calc: Multiply time (hours) with a float
  2016-09-19 15:33                   ` Nick Dokos
@ 2016-09-19 17:45                     ` Michael Welle
  2016-09-20 14:17                       ` Karl Voit
  0 siblings, 1 reply; 16+ messages in thread
From: Michael Welle @ 2016-09-19 17:45 UTC (permalink / raw)
  To: emacs-orgmode

Hello,

Nick Dokos <ndokos@gmail.com> writes:

> Michael Welle <mwe012008@gmx.net> writes:
>
>> Hello,
>>
>> Karl Voit <devnull@Karl-Voit.at> writes:
>>
>>> * Michael Welle <mwe012008@gmx.net> wrote:
>>>>
>>>> Karl Voit <devnull@Karl-Voit.at> writes:
>>>>
>>>>>> [...]
>>>>>>> #+TBLFM: @2$3='(* $2 (org-time-string-to-hours "$1"));L
>>>>>> or converting $2 to a number before the calculation should work also,
>>>>>> but is more to write ;).
>>>>>
>>>>> Hm. I still got #ERROR. I reduced my issue to calculate the float
>>>>> (for hours)
>>>>> without multiplying it with "value":
>>>> strange. I use the current Org version from git and Emacs 25.1.
>>>>
>>>>> | time [h:m:s] |   value | product |
>>>>> |--------------+---------+---------|
>>>>> |     09:15:00 | 2.54321 | #ERROR  |
>>>>>
>>>>> #+TBLFM: @2$3='(org-time-string-to-hours $1)
I'm just back from a 20km hike and in the woods my mind came up with a
silly question ;). This empty line, that we see between the table above
and the formula, does that exist in your real Org file? If so, and if
the point is on the formula when you press C-cC-c, the table doesn't
change, i.e., the error mark doesn't go away even if the formula is
correct. 

Regards
hmw

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

* Re: Calc: Multiply time (hours) with a float
  2016-09-19 17:45                     ` Michael Welle
@ 2016-09-20 14:17                       ` Karl Voit
  0 siblings, 0 replies; 16+ messages in thread
From: Karl Voit @ 2016-09-20 14:17 UTC (permalink / raw)
  To: emacs-orgmode

* Michael Welle <mwe012008@gmx.net> wrote:
> Hello,
>
>> | time [h:m:s] |   value | product |
>> |--------------+---------+---------|
>> |     09:15:00 | 2.54321 | #ERROR  |
>>
>> #+TBLFM: @2$3='(org-time-string-to-hours $1)

> I'm just back from a 20km hike and in the woods my mind came up with a
> silly question ;). This empty line, that we see between the table above
> and the formula, does that exist in your real Org file?

Good point but this is not the case in my Org-mode ;-)

To prevent any update issue, I also manually deleted "#ERROR" before
I reported any result here in the ML.

-- 
mail|git|SVN|photos|postings|SMS|phonecalls|RSS|CSV|XML to Org-mode:
       > get Memacs from https://github.com/novoid/Memacs <

https://github.com/novoid/extract_pdf_annotations_to_orgmode + more on github

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

* Re: Calc: Multiply time (hours) with a float - solved by updating Org
  2016-09-19 13:18               ` Karl Voit
  2016-09-19 13:49                 ` Michael Welle
  2016-09-19 15:33                 ` Bernhard Pröll
@ 2016-09-20 14:23                 ` Karl Voit
  2016-09-20 14:58                   ` Michael Welle
  2 siblings, 1 reply; 16+ messages in thread
From: Karl Voit @ 2016-09-20 14:23 UTC (permalink / raw)
  To: emacs-orgmode

* Karl Voit <devnull@Karl-Voit.at> wrote:
>
> | time [h:m:s] |   value | product |
> |--------------+---------+---------|
> |     09:15:00 | 2.54321 | #ERROR  |
> #+TBLFM: @2$3='(org-time-string-to-hours $1)
>
> OK, this is my current issue then.
>
> I'm using Org from git/maint version 8.3.4
> (release_8.3.4-33-gd522fc) and GNU Emacs 24.5.1 (i686-pc-mingw32
> Windows) as well as Emacs 24.4.1 on GNU/Linux.

I updated to Org-mode version 8.3.4 (release_8.3.4-115-gf46b92):

| time [h:m:s] |   value | product |
|--------------+---------+---------|
|     09:15:00 | 2.54321 |    9.25 |
#+TBLFM: @2$3='(org-time-string-to-hours $1)

... now works after Org update.

| time [h:m:s] |   value |    product |
|--------------+---------+------------|
|     09:15:00 | 2.54321 | 23.5246925 |
#+TBLFM: @2$3='(* $2 (org-time-string-to-hours "$1"));L

... now works as well.

I am so sorry that this took you so much time just because of my
old code. However, I learned a lot from your comments - Thanks for
that! :-)

-- 
mail|git|SVN|photos|postings|SMS|phonecalls|RSS|CSV|XML to Org-mode:
       > get Memacs from https://github.com/novoid/Memacs <

https://github.com/novoid/extract_pdf_annotations_to_orgmode + more on github

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

* Re: Calc: Multiply time (hours) with a float - solved by updating Org
  2016-09-20 14:23                 ` Calc: Multiply time (hours) with a float - solved by updating Org Karl Voit
@ 2016-09-20 14:58                   ` Michael Welle
  0 siblings, 0 replies; 16+ messages in thread
From: Michael Welle @ 2016-09-20 14:58 UTC (permalink / raw)
  To: emacs-orgmode

Hello,

Karl Voit <devnull@Karl-Voit.at> writes:

> * Karl Voit <devnull@Karl-Voit.at> wrote:
>>
>> | time [h:m:s] |   value | product |
>> |--------------+---------+---------|
>> |     09:15:00 | 2.54321 | #ERROR  |
>> #+TBLFM: @2$3='(org-time-string-to-hours $1)
>>
>> OK, this is my current issue then.
>>
>> I'm using Org from git/maint version 8.3.4
>> (release_8.3.4-33-gd522fc) and GNU Emacs 24.5.1 (i686-pc-mingw32
>> Windows) as well as Emacs 24.4.1 on GNU/Linux.
>
> I updated to Org-mode version 8.3.4 (release_8.3.4-115-gf46b92):
>
> | time [h:m:s] |   value | product |
> |--------------+---------+---------|
> |     09:15:00 | 2.54321 |    9.25 |
>
> #+TBLFM: @2$3='(org-time-string-to-hours $1)
>
> ... now works after Org update.
that's great news.


[...]
> I am so sorry that this took you so much time just because of my
> old code. However, I learned a lot from your comments - Thanks for
> that! :-)
No worries, have fun ;).

Regards
hmw

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

end of thread, other threads:[~2016-09-20 14:58 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-08  8:33 Calc: Multiply time (hours) with a float Karl Voit
2016-09-11 11:45 ` Karl Voit
2016-09-11 12:12   ` Michael Welle
2016-09-18 16:36     ` Karl Voit
2016-09-19  6:49       ` Michael Welle
2016-09-19  6:55         ` Michael Welle
2016-09-19 11:43           ` Karl Voit
2016-09-19 12:41             ` Michael Welle
2016-09-19 13:18               ` Karl Voit
2016-09-19 13:49                 ` Michael Welle
2016-09-19 15:33                   ` Nick Dokos
2016-09-19 17:45                     ` Michael Welle
2016-09-20 14:17                       ` Karl Voit
2016-09-19 15:33                 ` Bernhard Pröll
2016-09-20 14:23                 ` Calc: Multiply time (hours) with a float - solved by updating Org Karl Voit
2016-09-20 14:58                   ` Michael Welle

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