unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* percent-change
@ 2023-05-11 13:35 Emanuel Berg
  2023-05-13 13:37 ` percent-change Ruijie Yu via Users list for the GNU Emacs text editor
  2023-05-13 15:16 ` percent-change Yuri Khan
  0 siblings, 2 replies; 9+ messages in thread
From: Emanuel Berg @ 2023-05-11 13:35 UTC (permalink / raw)
  To: help-gnu-emacs

Take a look at this, see examples for intended bahvior.
This was more difficult than I imagined, maybe there is some
simpler way to do it?

Otherwise it just shows once again that computing something
and writing a program to compute it are not the same ...

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/math.el

(defun percent-change (from to)
  (let ((dist (abs (- to from))))
    (if (zerop dist)
        0
      (let ((change (abs (/ dist from 0.01))))
        (if (< from to)
            change
          (* -1 change) )))))

;; (percent-change   1  2) ;  100
;; (percent-change   1  1) ;    0
;; (percent-change   1  0) ; -100
;; (percent-change   1 -1) ; -200

;; (percent-change   0  1) ;  1.0e+INF
;; (percent-change   0  0) ;  0
;; (percent-change   0 -1) ; -1.0e+INF

;; (percent-change  -1  1) ;  200
;; (percent-change  -1  0) ;  100
;; (percent-change  -1 -1) ;    0
;; (percent-change  -1 -2) ; -100

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: percent-change
  2023-05-11 13:35 percent-change Emanuel Berg
@ 2023-05-13 13:37 ` Ruijie Yu via Users list for the GNU Emacs text editor
  2023-05-13 14:00   ` percent-change Emanuel Berg
  2023-05-13 14:06   ` percent-change Emanuel Berg
  2023-05-13 15:16 ` percent-change Yuri Khan
  1 sibling, 2 replies; 9+ messages in thread
From: Ruijie Yu via Users list for the GNU Emacs text editor @ 2023-05-13 13:37 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs


Emanuel Berg <incal@dataswamp.org> writes:

> Take a look at this, see examples for intended bahvior.
> This was more difficult than I imagined, maybe there is some
> simpler way to do it?
>
> Otherwise it just shows once again that computing something
> and writing a program to compute it are not the same ...
>
> ;;; -*- lexical-binding: t -*-
> ;;
> ;; this file:
> ;;   https://dataswamp.org/~incal/emacs-init/math.el
>
> (defun percent-change (from to)
>   (let ((dist (abs (- to from))))
>     (if (zerop dist)
>         0
>       (let ((change (abs (/ dist from 0.01))))
>         (if (< from to)
>             change
>           (* -1 change) )))))

This is my version.  All test cases seem to pass.

```emacs-lisp
(defun cfg-percent-change (from to)
  (cond
   ((< from 0) (- (cfg-percent-change (- from) (- to))))
   ((= from to) 0)
   ((/ (- to from) from 0.01))))
```

I think your version had an unnecessary double-abs, and tightening up
the logic resulted in my version.

And yes, computation is much different from coming up all the edge
casese for writing a program that does the computation for you. :)

> ;; (percent-change   1  2) ;  100
> ;; (percent-change   1  1) ;    0
> ;; (percent-change   1  0) ; -100
> ;; (percent-change   1 -1) ; -200
>
> ;; (percent-change   0  1) ;  1.0e+INF
> ;; (percent-change   0  0) ;  0
> ;; (percent-change   0 -1) ; -1.0e+INF
>
> ;; (percent-change  -1  1) ;  200
> ;; (percent-change  -1  0) ;  100
> ;; (percent-change  -1 -1) ;    0
> ;; (percent-change  -1 -2) ; -100


-- 
Best,


RY



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

* Re: percent-change
  2023-05-13 13:37 ` percent-change Ruijie Yu via Users list for the GNU Emacs text editor
@ 2023-05-13 14:00   ` Emanuel Berg
  2023-05-13 14:06   ` percent-change Emanuel Berg
  1 sibling, 0 replies; 9+ messages in thread
From: Emanuel Berg @ 2023-05-13 14:00 UTC (permalink / raw)
  To: help-gnu-emacs

Ruijie Yu via Users list for the GNU Emacs text editor wrote:

> This is my version.  All test cases seem to pass.
>
> (defun cfg-percent-change (from to)
>   (cond
>    ((< from 0) (- (cfg-percent-change (- from) (- to))))
>    ((= from to) 0)
>    ((/ (- to from) from 0.01))))
>
> I think your version had an unnecessary double-abs, and
> tightening up the logic resulted in my version.

Non-recursive version, but reading it actually one could just
use `let' for 0.01 and then hardcode with `cond', that would
be even better since one could also drop `cl-lib' (not that
there is anything wrong with it).

Also there is nothing wrong with recursion, and especially not
in this case which is non-iterative in nature, still, there is
no need to do it and if one is allowed to dream one can
imagine simple math functions like these to be applied to huge
data sets that would imply an almost endless stream of
invocations ...

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/math.el

(require 'cl-lib)

(defun percent-change-2 (from to)
  (if (= from to)
      0
    (cl-labels ((change (ff tt) (/ (- tt ff) ff 0.01)))
      (if (< from 0)
          (- (change (- from) (- to)))
        (change from to) ))))

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: percent-change
  2023-05-13 13:37 ` percent-change Ruijie Yu via Users list for the GNU Emacs text editor
  2023-05-13 14:00   ` percent-change Emanuel Berg
@ 2023-05-13 14:06   ` Emanuel Berg
  1 sibling, 0 replies; 9+ messages in thread
From: Emanuel Berg @ 2023-05-13 14:06 UTC (permalink / raw)
  To: help-gnu-emacs

(defun percent-change-3 (from to)
  (let ((denom 0.01))
    (cond
      ((< from 0) (- (/ (- (- to) (- from)) (- from) denom)))
      ((= from to) 0)
      ((/ (- to from) from denom)) )))

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: percent-change
  2023-05-11 13:35 percent-change Emanuel Berg
  2023-05-13 13:37 ` percent-change Ruijie Yu via Users list for the GNU Emacs text editor
@ 2023-05-13 15:16 ` Yuri Khan
  2023-05-13 15:58   ` percent-change Emanuel Berg
  2023-05-13 16:04   ` percent-change Emanuel Berg
  1 sibling, 2 replies; 9+ messages in thread
From: Yuri Khan @ 2023-05-13 15:16 UTC (permalink / raw)
  To: help-gnu-emacs

On Sat, 13 May 2023 at 19:41, Emanuel Berg <incal@dataswamp.org> wrote:
>
> Take a look at this, see examples for intended bahvior.
> This was more difficult than I imagined, maybe there is some
> simpler way to do it?

    (defun yk-percent-change (from to)
      (if (= from to)
          0
        (/ (- to from) (abs from) 0.01)))



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

* Re: percent-change
  2023-05-13 15:16 ` percent-change Yuri Khan
@ 2023-05-13 15:58   ` Emanuel Berg
  2023-05-13 16:04   ` percent-change Emanuel Berg
  1 sibling, 0 replies; 9+ messages in thread
From: Emanuel Berg @ 2023-05-13 15:58 UTC (permalink / raw)
  To: help-gnu-emacs

Yuri Khan wrote:

>> Take a look at this, see examples for intended bahvior.
>> This was more difficult than I imagined, maybe there is
>> some simpler way to do it?
>
>     (defun yk-percent-change (from to)
>       (if (= from to)
>           0
>         (/ (- to from) (abs from) 0.01)))

Digital!

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: percent-change
  2023-05-13 15:16 ` percent-change Yuri Khan
  2023-05-13 15:58   ` percent-change Emanuel Berg
@ 2023-05-13 16:04   ` Emanuel Berg
  2023-05-14 14:29     ` percent-change Yuri Khan
  1 sibling, 1 reply; 9+ messages in thread
From: Emanuel Berg @ 2023-05-13 16:04 UTC (permalink / raw)
  To: help-gnu-emacs

Yuri Khan wrote:

>> Take a look at this, see examples for intended bahvior.
>> This was more difficult than I imagined, maybe there is
>> some simpler way to do it?
>
>     (defun yk-percent-change (from to)
>       (if (= from to)
>           0
>         (/ (- to from) (abs from) 0.01)))

But how/why does that work if 'from' is 0?

(/ 1 0 0.01) ; 1.0e+INF

What does that mean?

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: percent-change
  2023-05-13 16:04   ` percent-change Emanuel Berg
@ 2023-05-14 14:29     ` Yuri Khan
  2023-05-15  2:54       ` percent-change Emanuel Berg
  0 siblings, 1 reply; 9+ messages in thread
From: Yuri Khan @ 2023-05-14 14:29 UTC (permalink / raw)
  To: help-gnu-emacs

On Sun, 14 May 2023 at 19:20, Emanuel Berg <incal@dataswamp.org> wrote:

> But how/why does that work if 'from' is 0?
>
> (/ 1 0 0.01) ; 1.0e+INF
>
> What does that mean?

In floating point, dividing by zero is allowed and yields +infinity,
-infinity or (quiet) not-a-number depending on the sign of the
numerator. ‘/’ knows to perform the division in floating when at least
one operand is a float, which 0.01 nicely provides. “1.0e+INF” is just
the printed representation for positive infinity.



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

* Re: percent-change
  2023-05-14 14:29     ` percent-change Yuri Khan
@ 2023-05-15  2:54       ` Emanuel Berg
  0 siblings, 0 replies; 9+ messages in thread
From: Emanuel Berg @ 2023-05-15  2:54 UTC (permalink / raw)
  To: help-gnu-emacs

Yuri Khan wrote:

>> But how/why does that work if 'from' is 0?
>>
>> (/ 1 0 0.01) ; 1.0e+INF
>>
>> What does that mean?
>
> In floating point, dividing by zero is allowed and yields
> +infinity, -infinity or (quiet) not-a-number depending on
> the sign of the numerator. ‘/’ knows to perform the division
> in floating when at least one operand is a float, which 0.01
> nicely provides. 1.0e+INF is just the printed representation
> for positive infinity.

A quiet NaN is not signalled. [1]

(/  1 0.0) ;  1.0e+INF
(/  0 0.0) ; -0.0e+NaN
(/ -1 0.0) ; -1.0e+INF

Yes, it is as you say, but why?

Maybe the answer is TLDR:
  https://en.wikipedia.org/wiki/Division_by_zero

[1] https://stackoverflow.com/questions/18118408/what-is-the-difference-between-quiet-nan-and-signaling-nan

-- 
underground experts united
https://dataswamp.org/~incal




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

end of thread, other threads:[~2023-05-15  2:54 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-11 13:35 percent-change Emanuel Berg
2023-05-13 13:37 ` percent-change Ruijie Yu via Users list for the GNU Emacs text editor
2023-05-13 14:00   ` percent-change Emanuel Berg
2023-05-13 14:06   ` percent-change Emanuel Berg
2023-05-13 15:16 ` percent-change Yuri Khan
2023-05-13 15:58   ` percent-change Emanuel Berg
2023-05-13 16:04   ` percent-change Emanuel Berg
2023-05-14 14:29     ` percent-change Yuri Khan
2023-05-15  2:54       ` percent-change Emanuel Berg

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