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

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

1.75 -> 1-3/4


From: Emanuel Berg
Subject: 1.75 -> 1-3/4
Date: Tue, 05 Jun 2018 15:01:33 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)

Did anyone do the equivalent of this ("ths" zsh
below) in Elisp or any other Lisp? Just asking
before I do it myself. The algorithm should be
straightforward, but it uses recursion which
I plan to replace by a loop in the Elisp
version. But it looks good otherwise, right?

Before anyone tries to use it on a bicycle
tire, note that "[a] 26x1.5 tire and a 26x1-1/2
tire are different tire sizing systems" (JL on
rec.bicycles.tech). Read more here [1]

ths () {
    local value=$1
    local denom=${2:-16}
    local whole=$(( int(floor($value)) ))
    local rest=$(( $value - $whole ))
    local frac=$(( int(rint($rest * $denom)) ))
    if (( $frac > 0 && $(( $frac % 2 )) == 0 )); then
        local new_denom=$(( denom / 2 ))
        ths $value $new_denom
    else
        local frace
        (( $frac > 0 )) && frace=-${frac}/${denom}
        echo ${whole}${frace}
    fi
}
# $ ths 2.0
# 2
# $ ths 1.75
# 1-3/4

[1] http://www.sheldonbrown.com/tire-sizing.html

-- 
underground experts united
http://user.it.uu.se/~embe8573


reply via email to

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