help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: percent-change


From: Emanuel Berg
Subject: Re: percent-change
Date: Sat, 13 May 2023 16:00:07 +0200
User-agent: Gnus/5.13 (Gnus v5.13)

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]