Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

1.75 -> 1-3/4



Is this [1] the correct algorithm/a good
implementation? It translates 1.75 into 1-3/4
at least :P

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 % 2 )) == 0 )); then
        local new_denom=$(( denom / 2 ))
        ths $value $new_denom
    else
        echo $whole-${frac}/${denom}
    fi
}
# $ ths 1.75
# 1-3/4

[1] http://user.it.uu.se/~embe8573/conf/.zsh/math

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



Messages sorted by: Reverse Date, Date, Thread, Author