emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Incorrect sum of times in table
@ 2013-07-01 17:38 Paul Stansell
  2013-07-01 20:49 ` Nick Dokos
  0 siblings, 1 reply; 4+ messages in thread
From: Paul Stansell @ 2013-07-01 17:38 UTC (permalink / raw)
  To: emacs-orgmode

Hello,

I noticed a case where the sum of two times in a table does not give
the correct answer.

To see this, create an org file with the following lines:

| 0:00:31 |
| 0:00:30 |
|---------|
|         |

Open it and type C-+ C-y in the empty cell of the table.  The answer
inserted is 0:01:00 instead of 0:01:01.

Regards,

Paul

PS. I'm using Emacs 24.2.1 and Org-mode release_8.0.3-331-gf7e6f1 and
I tested with the minimal org-mode set-up from
http://orgmode.org/manual/Feedback.html.

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

* Re: Incorrect sum of times in table
  2013-07-01 17:38 Incorrect sum of times in table Paul Stansell
@ 2013-07-01 20:49 ` Nick Dokos
  2013-07-01 21:22   ` Bastien
  0 siblings, 1 reply; 4+ messages in thread
From: Nick Dokos @ 2013-07-01 20:49 UTC (permalink / raw)
  To: emacs-orgmode

Paul Stansell <paulstansell@gmail.com> writes:

> Hello,
>
> I noticed a case where the sum of two times in a table does not give
> the correct answer.
>
> To see this, create an org file with the following lines:
>
> | 0:00:31 |
> | 0:00:30 |
> |---------|
> |         |
>
> Open it and type C-+ C-y in the empty cell of the table.  The answer
> inserted is 0:01:00 instead of 0:01:01.
>

I think you mean C-c +.

The problem is that these things are calculated as decimal hours, using
floating point arithmetic and you get truncation towards 0 when the
value is printed out as an integer. The format in org-table-sum is

    (format "%d:%02d:%02d" h m s)

but s is 0.9999...  and it gets formatted as 0.

It might be better to use

    (format "%.0f:%02.0f:%02.0f" h m s)

i.e. as floating point with no places after the decimal point
(in which case the decimal point does not seem to be output).
But there may be other problems that I have not thought of.
It might be even better to round the floating point number to
the nearest integer and use %d formats instead:

    (format "%d:%02d:%02d" (round h) (round m) (round s))

-- 
Nick

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

* Re: Incorrect sum of times in table
  2013-07-01 20:49 ` Nick Dokos
@ 2013-07-01 21:22   ` Bastien
  2013-07-03  8:18     ` Bastien
  0 siblings, 1 reply; 4+ messages in thread
From: Bastien @ 2013-07-01 21:22 UTC (permalink / raw)
  To: Nick Dokos; +Cc: emacs-orgmode

Hi Nick,

Nick Dokos <ndokos@gmail.com> writes:

> I think you mean C-c +.
>
> The problem is that these things are calculated as decimal hours, using
> floating point arithmetic and you get truncation towards 0 when the
> value is printed out as an integer. The format in org-table-sum is
>
>     (format "%d:%02d:%02d" h m s)
>
> but s is 0.9999...  and it gets formatted as 0.
>
> It might be better to use
>
>     (format "%.0f:%02.0f:%02.0f" h m s)
>
> i.e. as floating point with no places after the decimal point
> (in which case the decimal point does not seem to be output).
> But there may be other problems that I have not thought of.
> It might be even better to round the floating point number to
> the nearest integer and use %d formats instead:
>
>     (format "%d:%02d:%02d" (round h) (round m) (round s))

Both solutions work -- we don't need to fear other problems here,
`org-table-sum' is pretty isolated, so I'd say "please go ahead!
and fix this in maint.

Thanks,

-- 
 Bastien

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

* Re: Incorrect sum of times in table
  2013-07-01 21:22   ` Bastien
@ 2013-07-03  8:18     ` Bastien
  0 siblings, 0 replies; 4+ messages in thread
From: Bastien @ 2013-07-03  8:18 UTC (permalink / raw)
  To: Nick Dokos; +Cc: emacs-orgmode

Bastien <bzg@gnu.org> writes:

> Both solutions work -- we don't need to fear other problems here,
> `org-table-sum' is pretty isolated, so I'd say "please go ahead!
> and fix this in maint.

I just applied the first solution, thanks!

-- 
 Bastien

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

end of thread, other threads:[~2013-07-03  8:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-07-01 17:38 Incorrect sum of times in table Paul Stansell
2013-07-01 20:49 ` Nick Dokos
2013-07-01 21:22   ` Bastien
2013-07-03  8:18     ` Bastien

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