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

Re: why do ceil/floor give the decimal dot?



Lawrence Velázquez <vq@xxxxxxxxx> writes:

>> I like the solution better to have the types all
>> floats (or whatever) and then use printf to make it
>> look the way you want.
>
> You should use %g then (with appropriate precision)
> because %d does an implicit conversion.
>
>     % printf '%d\n' $(( 1e20 ))
>     -9223372036854775808
>
>     % printf '%g\n' $(( 1e20 ))
>     1e+20
>
>     % printf '%.30g\n' $(( 1e20 ))
>     100000000000000000000

OK, case closed:

    prefix-len () {
        local hosts=$1
        local len=$(( 32 - ceil(log($hosts)/log(2)) ))
        printf "%g\n" $len
    }

    $ prefix-len 30
    27

Thanks everyone.

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



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