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

Re: question about float variables



On 2001-10-10 at 12:58 +0400, Borsenkow Andrej wrote:
> > float var1=0.0
> > var1=$((3 / 2))
> > echo $var1
> > 
> > return 1.000000000e+00 not good ! :(

> Integer divided by integer is integer.

Not so useful if you're manipulating parameters and you don't know what
their value is in advance.

% zmodload zsh/mathfunc
% float var1
% var1=$((float(3) / 2))
% echo $var1
1.500000000e+00

(Math-context float() is not the zsh command "float")

> > and how to format the output of $var1 ?
> > (i want a number like 95.3)

> typeset description:

They just change the storage type, so that:
% typeset -E var2
% typeset -F var3
% var2=$((3.0 / 2 )) ; var3=$((3.0 / 2 ))
% print $var2
1.500000000e+00
% print $var3
1.5000000000

I've not seen a way in zsh to modify this to be 'sensible' for humans,
as the original poster wants, unless you're happy to _just_ remove
trailing zeroes, in which case use the -F form and then:
% print ${var3%%0##}
1.5

Personally, I'd use the OS's printf(1) if it exists, and use the G
format specification; on OpenBSD:
 gG          The argument is printed in style f or in style e (E) whichev-
             er gives full precision in minimum space.

% printf '%g\n' $var2
1.5
% printf '%g\n' $var3
1.5

-- 
I don't like your use of the word "interesting" in this context.

Attachment: pgpd3HGXfpGlb.pgp
Description: PGP signature



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