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

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

percent-change


From: Emanuel Berg
Subject: percent-change
Date: Thu, 11 May 2023 15:35:36 +0200
User-agent: Gnus/5.13 (Gnus v5.13)

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




reply via email to

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