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

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



On May 13, 2015, at 11:29 PM, Emanuel Berg <embe8573@xxxxxxxxxxxxx> wrote:

> Take a look at this [1] zsh:
> 
>    prefix-len () {
>        local hosts=$1
>        echo $(( 32 - int(ceil(log($hosts)/log(2))) ))
>    }
> 
> without the integer conversion, the output for
> 
>    prefix-len 30
> 
> isn't 27, but "27."

Yes, this is expected and correct. From "ARITHMETIC EVALUATION" in the
zshmisc(1) man page:

    Promotion of integer to floating point values is performed where
    necessary.

So:

- "ceil(log(30)/log(2))" returns floating-point "5."
- integer "32" is promoted to floating point "32."
- floating point "32." - floating point "5." = floating-point "27."

vq



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