From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Ruijie Yu via Users list for the GNU Emacs text editor Newsgroups: gmane.emacs.help Subject: Re: percent-change Date: Sat, 13 May 2023 21:37:12 +0800 Message-ID: References: <87r0rnxa6v.fsf@dataswamp.org> Reply-To: Ruijie Yu Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="18317"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.11.3; emacs 30.0.50 Cc: help-gnu-emacs@gnu.org To: Emanuel Berg Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Sat May 13 15:38:30 2023 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pxpSA-0004ai-MJ for geh-help-gnu-emacs@m.gmane-mx.org; Sat, 13 May 2023 15:38:30 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pxpRV-0002xF-55; Sat, 13 May 2023 09:37:49 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pxpRQ-0002wj-U0 for help-gnu-emacs@gnu.org; Sat, 13 May 2023 09:37:45 -0400 Original-Received: from netyu.xyz ([152.44.41.246] helo=mail.netyu.xyz) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pxpRK-0002JG-BM for help-gnu-emacs@gnu.org; Sat, 13 May 2023 09:37:44 -0400 Original-Received: from fw.net.yu.netyu.xyz ( [222.248.4.98]) by netyu.xyz (OpenSMTPD) with ESMTPSA id 05348873 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Sat, 13 May 2023 13:37:31 +0000 (UTC) In-reply-to: <87r0rnxa6v.fsf@dataswamp.org> Received-SPF: pass client-ip=152.44.41.246; envelope-from=ruijie@netyu.xyz; helo=mail.netyu.xyz X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01, T_SPF_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.help:143588 Archived-At: Emanuel Berg 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