[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
all-substance.el
From: |
Emanuel Berg |
Subject: |
all-substance.el |
Date: |
Wed, 10 Jul 2024 03:54:52 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) |
Here is another interesting program, and very substantial - or
have a look. Maybe the initial function can be improved?
The purpose is to compute the total amount of the substance
after some days and half-time for that substance.
The assumption is consumption is evenly distributed, i.e.
the same every day.
;;; -*- lexical-binding: t -*-
;;
;; this file:
;; https://dataswamp.org/~incal/emacs-init/all-substance.el
(require 'cl-lib)
(defun half-time-to-day-drop (ht)
(if (< ht 24)
(/ ht 24 2.0)
(- 1 (/ 24 ht 2.0)) ))
;; (half-time-to-day-drop 12) ; 0.25
;; (half-time-to-day-drop 48) ; 0.75
(defun all-substance (q d ht)
"Q is quantity, D is days, HT is half-time."
(cl-loop
with daily = (/ q d 1.0)
with day-drop = (half-time-to-day-drop ht)
for i downfrom (1- d) to 0
sum (* daily (expt day-drop i)) into total
finally return (message "daily %.2f, days %d, total %.2f" daily d total) ))
;; (all-substance 5 10 6) ; daily 0.50, days 10, total 0.57
;; (all-substance 50 100 6) ; daily 0.50, days 100, total 0.57
;; (all-substance 50 50 6000) ; daily 1.00, days 50, total 47.63
(provide 'all-substance)
--
underground experts united
https://dataswamp.org/~incal
- all-substance.el,
Emanuel Berg <=