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

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



On 2015-05-14 18:14:53 -0400, Lawrence Velázquez wrote:
> On May 14, 2015, at 5:38 PM, Emanuel Berg <embe8573@xxxxxxxxxxxxx> wrote:
> 
> > 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

To output integers, it may be better to use %.0f because %g may output
the result with an exponent, or use a very large precision for %g so
that it wouldn't depend on the number to output, such as %.9999g (but
%.0f is shorter and simpler to understand). Now, if the user restricts
to the range where all integers are representable as floating point,
i.e. [-2^53,2^53], then %.16g is sufficient.

-- 
Vincent Lefèvre <vincent@xxxxxxxxxx> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)



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