* all-substance.el
@ 2024-07-10 1:54 Emanuel Berg
2024-07-11 17:59 ` all-substance.el Emanuel Berg
0 siblings, 1 reply; 2+ messages in thread
From: Emanuel Berg @ 2024-07-10 1:54 UTC (permalink / raw)
To: help-gnu-emacs
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
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: all-substance.el
2024-07-10 1:54 all-substance.el Emanuel Berg
@ 2024-07-11 17:59 ` Emanuel Berg
0 siblings, 0 replies; 2+ messages in thread
From: Emanuel Berg @ 2024-07-11 17:59 UTC (permalink / raw)
To: help-gnu-emacs
> Here is another interesting program, and very substantial
Added `wash-out', so now we have wash on but also wash of.
See results in particular with half time 6 hours in the below
examples for unexpected things, to most.
Also added docstrings and a bunch of stuff.
;;; -*- 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 &optional verbose day-q)
"Q is the quantity, for example 10. This is a number; do not use a unit.
D is the number of days, for example 20.
HT is the half-time in hours, for example 6.
Set VERBOSE to return a string.
Set DAY-Q if Q instead is submitted the daily quantity, for example 0.5."
(cl-loop
with daily = (if day-q q (/ 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
(if verbose
(message "daily %.2f, days %d, total %.2f" daily d total)
total) ))
;; (all-substance 300 365 6) ; 0.94
;; (all-substance 0.5 30 6 nil t) ; 0.57
;; (all-substance 365 365 6 t) ; daily 1.00, days 365, total 1.14
(defun wash-out (q ht &optional days)
(or days (setq days 5))
(cl-loop
with day-drop = (half-time-to-day-drop ht)
for d from 1 to days
collect (* q (expt day-drop d)) into res
finally return res) )
;; (wash-out 1.14 6) ; 0.1 1 day w/o resupply
;; 0.02 2 ..
;; 0.002 3 ..
;; 0.0003 4 ..
;; 0.00003 5 ..
(provide 'all-substance)
--
underground experts united
https://dataswamp.org/~incal
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-07-11 17:59 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-10 1:54 all-substance.el Emanuel Berg
2024-07-11 17:59 ` all-substance.el Emanuel Berg
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).