zsh-users
 help / color / mirror / code / Atom feed
* question about float variables
@ 2001-10-09 23:33 Bruno Bonfils
  2001-10-10  8:58 ` Borsenkow Andrej
  0 siblings, 1 reply; 6+ messages in thread
From: Bruno Bonfils @ 2001-10-09 23:33 UTC (permalink / raw)
  To: ZSH Users

[-- Attachment #1: Type: text/plain, Size: 449 bytes --]

hi all,

How to divide two numbers ?

I try this :

float var1=0.0
var1=$((2.4 + 1.4))
echo $var1

return 3.800000000e+00 good !!

but

float var1=0.0
var1=$((3 / 2)) 
echo $var1

return 1.000000000e+00 not good ! :(

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

Thank you very much

-- 
 Bruno Bonfils
 Admin Sys Linux

 http://www.darksnow.org  http://www.debian-fr.org
 http://www.zshfr.org

[-- Attachment #2: Type: application/pgp-signature, Size: 249 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: question about float variables
  2001-10-09 23:33 question about float variables Bruno Bonfils
@ 2001-10-10  8:58 ` Borsenkow Andrej
  2001-10-10  9:42   ` Phil Pennock
  2001-10-10 12:47   ` Bruno Bonfils
  0 siblings, 2 replies; 6+ messages in thread
From: Borsenkow Andrej @ 2001-10-10  8:58 UTC (permalink / raw)
  To: 'Bruno Bonfils', 'ZSH Users'

> float var1=0.0
> var1=$((3 / 2))
> echo $var1
> 
> return 1.000000000e+00 not good ! :(
>


bor@itsrm2% float var1
bor@itsrm2% var1=$((3.0/2))
bor@itsrm2% print $var1
1.500000000e+00

Integer divided by integer is integer.

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

Should I just say RTFM?

typeset description:

    -E
          Use an internal double-precision floating point
          representation.  On output the variable will be converted to
          scientific notation.  If N is nonzero it defines the number
          of significant figures to display; the default is ten.

    -F
          Use an internal double-precision floating point
          representation.  On output the variable will be converted to
          fixed-point decimal notation.  If N is nonzero it defines the
          number of digits to display after the decimal point; the
          default is ten.


-andrej


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: question about float variables
  2001-10-10  8:58 ` Borsenkow Andrej
@ 2001-10-10  9:42   ` Phil Pennock
  2001-10-10 10:10     ` Borsenkow Andrej
  2001-10-10 12:47   ` Bruno Bonfils
  1 sibling, 1 reply; 6+ messages in thread
From: Phil Pennock @ 2001-10-10  9:42 UTC (permalink / raw)
  To: Borsenkow Andrej; +Cc: 'Bruno Bonfils', 'ZSH Users'

[-- Attachment #1: Type: text/plain, Size: 1352 bytes --]

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 #2: Type: application/pgp-signature, Size: 187 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: question about float variables
  2001-10-10  9:42   ` Phil Pennock
@ 2001-10-10 10:10     ` Borsenkow Andrej
  2001-10-10 10:28       ` Phil Pennock
  0 siblings, 1 reply; 6+ messages in thread
From: Borsenkow Andrej @ 2001-10-10 10:10 UTC (permalink / raw)
  To: 'Phil Pennock'; +Cc: 'Bruno Bonfils', 'ZSH Users'

> 
> > 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.
>

Sigh ... zsh was meant to be used for arithmetic computations. Obvious
workarounds are

- multiply by 1.0: $((var1*1.0/var2))
- use temps
- use float() function: $((float(var1)/var2))

What is wrong with above?

BTW how is it different from C? 

#include <stdio.h>
main() {
float f;

f = 3/2;
printf ("f = %f\n", f);
}
bor@itsrm2% cc foo.c
bor@itsrm2% ./a.out
f = 1.000000

> I've not seen a way in zsh to modify this to be 'sensible' for humans,

If you can suggest how to 'sensibly' incorporate it into zsh grammar ...

> 
> Personally, I'd use the OS's printf(1) if it exists,


It is included into 4.1 branch. I do not know what people think about
adding it into 4.0.3.

-andrej


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: question about float variables
  2001-10-10 10:10     ` Borsenkow Andrej
@ 2001-10-10 10:28       ` Phil Pennock
  0 siblings, 0 replies; 6+ messages in thread
From: Phil Pennock @ 2001-10-10 10:28 UTC (permalink / raw)
  To: Borsenkow Andrej; +Cc: 'ZSH Users'

[-- Attachment #1: Type: text/plain, Size: 1326 bytes --]

On 2001-10-10 at 14:10 +0400, Borsenkow Andrej wrote:
> - multiply by 1.0: $((var1*1.0/var2))
> - use temps
> - use float() function: $((float(var1)/var2))
> 
> What is wrong with above?

Nothing -- if you take the time to _read_ the mail to which you're
replying, before launching into a tirade, you might notice that I
actually pointed to "zmodload math/compfunc" and then using the float()
function.  I wasn't saying that zsh is not useful.

> BTW how is it different from C? 

What has this got to do with it?  Is C an exemplar of good design, now?

> > I've not seen a way in zsh to modify this to be 'sensible' for humans,
> 
> If you can suggest how to 'sensibly' incorporate it into zsh grammar ...

Yet Another typeset flag.  There are already flags handling how
parameters are displayed, alignment, etc etc.  One to say "display this
param as though using %g".

> > Personally, I'd use the OS's printf(1) if it exists,

> It is included into 4.1 branch.

Cool. :^)
-- 
Across all scientific fields, skeptical analysis of technical claims made by
others, and the presentation of detailed evidence to support such analysis, is
the heart of the scientific method. To outlaw such analysis is to outlaw the
scientific method itself. -- Ed Felten; declaration in Felten v RIAA 2001-08-13

[-- Attachment #2: Type: application/pgp-signature, Size: 187 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: question about float variables
  2001-10-10  8:58 ` Borsenkow Andrej
  2001-10-10  9:42   ` Phil Pennock
@ 2001-10-10 12:47   ` Bruno Bonfils
  1 sibling, 0 replies; 6+ messages in thread
From: Bruno Bonfils @ 2001-10-10 12:47 UTC (permalink / raw)
  To: 'ZSH Users'

[-- Attachment #1: Type: text/plain, Size: 1028 bytes --]

> bor@itsrm2% float var1
> bor@itsrm2% var1=$((3.0/2))
> bor@itsrm2% print $var1
> 1.500000000e+00

> Should I just say RTFM?
(sorry, i was tired)
> 
> typeset description:
> 
>     -E
>           Use an internal double-precision floating point
>           representation.  On output the variable will be converted to
>           scientific notation.  If N is nonzero it defines the number
>           of significant figures to display; the default is ten.
> 
>     -F
>           Use an internal double-precision floating point
>           representation.  On output the variable will be converted to
>           fixed-point decimal notation.  If N is nonzero it defines the
>           number of digits to display after the decimal point; the
>           default is ten.
> 
> 
> -andrej

Thank you very much 

-- 
 Bruno Bonfils
 Admin Sys Linux

 http://www.darksnow.org  http://www.debian-fr.org

Il faut s'entr'aider, c'est la loi de nature.

	-- Jean de La Fontaine, L'Ane et le Chien

[-- Attachment #2: Type: application/pgp-signature, Size: 249 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2001-10-12  4:40 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-10-09 23:33 question about float variables Bruno Bonfils
2001-10-10  8:58 ` Borsenkow Andrej
2001-10-10  9:42   ` Phil Pennock
2001-10-10 10:10     ` Borsenkow Andrej
2001-10-10 10:28       ` Phil Pennock
2001-10-10 12:47   ` Bruno Bonfils

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).