emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* rounding (up)
@ 2017-06-22  7:28 Uwe Brauer
  2017-06-22 12:02 ` John Kitchin
  2017-06-22 12:34 ` Marco Wahl
  0 siblings, 2 replies; 5+ messages in thread
From: Uwe Brauer @ 2017-06-22  7:28 UTC (permalink / raw)
  To: emacs-orgmode


Hi

It seems that org-table (and the underlying calc implementation) round
down not up.


Please consider

| 3.25 | 0.4875 |
|  6.5 |  0.975 |
#+TBLFM: $2=$1*0.15;


| 3.25 | 0.49 |
|  6.5 | 0.97 |
#+TBLFM: $2=$1*0.15;%.2f

Is there a way to obtain

| 3.25 | 0.49 |
|  6.5 | 0.98 |
#+TBLFM: $2=$1*0.15;%.2f

That is to replace rounding down to rounding up?

Thanks

Uwe Brauer 

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

* Re: rounding (up)
  2017-06-22  7:28 rounding (up) Uwe Brauer
@ 2017-06-22 12:02 ` John Kitchin
  2017-06-22 16:54   ` Uwe Brauer
  2017-06-22 12:34 ` Marco Wahl
  1 sibling, 1 reply; 5+ messages in thread
From: John Kitchin @ 2017-06-22 12:02 UTC (permalink / raw)
  To: Uwe Brauer; +Cc: emacs-orgmode

It appears Emacs uses Banker's rounding (http://wiki.c2.com/?BankersRounding):

#+BEGIN_SRC emacs-lisp
(list (round 4.5) (round 5.5))
#+END_SRC

#+RESULTS:
| 4 | 6 |

Here is some lightly tested code to get different styles of rounding:

#+BEGIN_SRC emacs-lisp
(defun custom-round (number &optional N direction)
  "Round NUMBER to N decimal places.
DIRECTION is a symbol of how to round.
`round' does Banker's rounding.
`ceiling' rounds up.
`floor' rounds down.
`truncate' rounds towards zero.
https://en.wikipedia.org/wiki/Rounding"
  (setq N (or N 0)
	direction (or direction 'ceiling))
  (let ((m (float (expt 10 (* -1 N)))))
    (* (funcall direction (/ number m)) m)))

(list (custom-round 0.4875 2 'ceiling) (custom-round 0.975 2 'ceiling))
#+END_SRC

#+RESULTS:
| 0.49 | 0.98 |

#+BEGIN_SRC emacs-lisp
(list (custom-round 0.4875 2 'floor) (custom-round 0.975 2 'floor))
#+END_SRC

#+RESULTS:
| 0.48 | 0.97 |


Uwe Brauer writes:

> Hi
>
> It seems that org-table (and the underlying calc implementation) round
> down not up.
>
>
> Please consider
>
> | 3.25 | 0.4875 |
> |  6.5 |  0.975 |
> #+TBLFM: $2=$1*0.15;
>
>
> | 3.25 | 0.49 |
> |  6.5 | 0.97 |
> #+TBLFM: $2=$1*0.15;%.2f
>
> Is there a way to obtain
>
> | 3.25 | 0.49 |
> |  6.5 | 0.98 |
> #+TBLFM: $2=$1*0.15;%.2f
>
> That is to replace rounding down to rounding up?
>
> Thanks
>
> Uwe Brauer


--
Professor John Kitchin
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu

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

* Re: rounding (up)
  2017-06-22  7:28 rounding (up) Uwe Brauer
  2017-06-22 12:02 ` John Kitchin
@ 2017-06-22 12:34 ` Marco Wahl
  2017-06-23  9:50   ` Uwe Brauer
  1 sibling, 1 reply; 5+ messages in thread
From: Marco Wahl @ 2017-06-22 12:34 UTC (permalink / raw)
  To: emacs-orgmode

Uwe Brauer <oub@mat.ucm.es> writes:

> Hi
>
> It seems that org-table (and the underlying calc implementation) round
> down not up.
>
>
> Please consider
>
> | 3.25 | 0.4875 |
> |  6.5 |  0.975 |
>
> #+TBLFM: $2=$1*0.15;
>
>
> | 3.25 | 0.49 |
> |  6.5 | 0.97 |
>
> #+TBLFM: $2=$1*0.15;%.2f
>
> Is there a way to obtain
>
> | 3.25 | 0.49 |
> |  6.5 | 0.98 |
>
> #+TBLFM: $2=$1*0.15;%.2f
>
> That is to replace rounding down to rounding up?


(info "(calc) Integer Truncation") has

#v+
The ‘R’ (‘calc-round’) [‘round’ or ‘fround’] command rounds to the
nearest integer.  When the fractional part is .5 exactly, this command
rounds away from zero.  (All other rounding in the Calculator uses this
convention as well.)  Thus ‘3.5 R’ produces 4 but ‘3.4 R’ produces 3;
‘_3.5 R’ produces -4.
#v-

and

#v+
Each of these functions, when written in algebraic formulas, allows a
second argument which specifies the number of digits after the decimal
point to keep.  For example, ‘round(123.4567, 2)’ will produce the
answer 123.46, and ‘round(123.4567, -1)’ will produce 120 (i.e., the
cutoff is one digit to the _left_ of the decimal point).  A second
argument of zero is equivalent to no second argument at all.
#v-

So I suggest the following:

| 3.25 | |
|  6.5 | |
#+TBLFM: $2=round($1*0.15,2);%.2f


Ciao
    Marco
    

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

* Re: rounding (up)
  2017-06-22 12:02 ` John Kitchin
@ 2017-06-22 16:54   ` Uwe Brauer
  0 siblings, 0 replies; 5+ messages in thread
From: Uwe Brauer @ 2017-06-22 16:54 UTC (permalink / raw)
  To: emacs-orgmode

>>> "John" == John Kitchin <jkitchin@andrew.cmu.edu> writes:

   > #+BEGIN_SRC emacs-lisp
   > (list (custom-round 0.4875 2 'floor) (custom-round 0.975 2 'floor))
   > #+END_SRC

Thanks I will have a look into that code.

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

* Re: rounding (up)
  2017-06-22 12:34 ` Marco Wahl
@ 2017-06-23  9:50   ` Uwe Brauer
  0 siblings, 0 replies; 5+ messages in thread
From: Uwe Brauer @ 2017-06-23  9:50 UTC (permalink / raw)
  To: emacs-orgmode


   > Uwe Brauer <oub@mat.ucm.es> writes:


   > (info "(calc) Integer Truncation") has


   > So I suggest the following:

   > | 3.25 | |
   > |  6.5 | |

   > #+TBLFM: $2=round($1*0.15,2);%.2f

That is cool as well

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

end of thread, other threads:[~2017-06-23  9:55 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-22  7:28 rounding (up) Uwe Brauer
2017-06-22 12:02 ` John Kitchin
2017-06-22 16:54   ` Uwe Brauer
2017-06-22 12:34 ` Marco Wahl
2017-06-23  9:50   ` Uwe Brauer

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