all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Emanuel Berg <incal@dataswamp.org>
To: help-gnu-emacs@gnu.org
Subject: Re: percent-change
Date: Sat, 13 May 2023 16:00:07 +0200	[thread overview]
Message-ID: <878rdsxrfc.fsf@dataswamp.org> (raw)
In-Reply-To: sdvmt28z71z.fsf@netyu.xyz

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	other threads:[~2023-05-13 14:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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   ` Emanuel Berg [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=878rdsxrfc.fsf@dataswamp.org \
    --to=incal@dataswamp.org \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.