Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: 1.75 -> 1-3/4
- X-seq: zsh-users 23442
- From: Matthew Martin <phy1729@xxxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Re: 1.75 -> 1-3/4
- Date: Tue, 5 Jun 2018 17:32:54 -0500
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:mail-followup-to:references :mime-version:content-disposition:in-reply-to:user-agent; bh=NDx7b6fvcCdOghmuNSzscjtU1dSC0ivWutsq42J2kXE=; b=Cx+j07X7M1I1iMXc6XUDUGSupT1hh7yQXWSeY69mN14HT/h/QqndbdERHEJSNRRxJv vqwmObtma9qDB7Op40H2pMMvTIJ/THeKZn29yQPI8v1ZZdquvI78C7KKoikvfOMhxxcU s+pmDVs7UOK2TtIsJARfH3eY1JC4b49krLdtq9hMGI0o9Qx3N/ygJCdDCS5/FF0QcHRW Q3I1LW4PLxW5xXoja+UM2UZ/GLj17cLeNsgE34fYxJJNqDe3wWdZYeFSkfvvJ9CEU8rL oEcRgG57zQKT13Fj9uuqkFi5R0qmp6LcFDOfNPLcGyaXGZMW/K5blVF0IFn0oC3Nq0wI vPIw==
- In-reply-to: <8636y25hm7.fsf@zoho.com>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- List-unsubscribe: <mailto:zsh-users-unsubscribe@zsh.org>
- Mail-followup-to: zsh-users@xxxxxxx
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <8636y25hm7.fsf@zoho.com>
On Tue, Jun 05, 2018 at 12:09:20AM +0200, Emanuel Berg wrote:
> Is this [1] the correct algorithm/a good
> implementation? It translates 1.75 into 1-3/4
> at least :P
What comes to mind first for me is the following.
- Matthew Martin
ths() {
local decmal
local -i denom num whole
if [[ $1 != *.* ]]; then
print -- $1
return
fi
whole=${1%.*}
decimal=${1#*.}
num=$decimal
denom=$(( 10 ** ${#decimal} ))
for d in 2 5; do
while (( num % d == 0 && denom % d == 0 )); do
(( num /= d ))
(( denom /= d ))
done
done
(( whole )) && print -n -- $whole
(( whole && num )) && print -n ' '
(( num )) && print -n -- $num/$denom
print
}
Messages sorted by:
Reverse Date,
Date,
Thread,
Author