zsh-users
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <stephane.chazelas@gmail.com>
To: zsh-users@zsh.org
Cc: embe8573@student.uu.se
Subject: force floating point arithmetics
Date: Mon, 4 Mar 2013 07:29:02 +0000	[thread overview]
Message-ID: <20130304072902.GA9639@chaz.gmail.com> (raw)

Hiya,

this is a follow up on
http://unix.stackexchange.com/a/66755/22565

I was trying to find a way for $((...)) to always do floating
point arithmetics as in $((1/3)) not returning 0, and the best I
could come up with was appending . to any sequence of decimal
digits that is not otherwise part of a hex number (or number in
another base) or of a variable name or 12e-20 type numbers like:

setopt extendedglob
calc() printf '%.6g\n' $((${*//(#bm)(([0-9.]##[eE][-+][0-9]##|[[:alnum:]_#]#[.#_[:alpha:]][[:alnum:]_#]#)|([0-9]##))/$MATCH${match[3]:+.}}))
alias 'calc=noglob calc'

calc 1/3

...

Like the OP there, I'm surprised there's no easier way to do it,
but maybe I've not looked hard enough.

So, is there a way to force floating point arithmetics there
even when entering integer constants?

Thanks,
Stephane


             reply	other threads:[~2013-03-04  7:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-04  7:29 Stephane Chazelas [this message]
2013-03-05 19:51 ` Peter Stephenson
2013-03-05 20:02   ` Peter Stephenson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130304072902.GA9639@chaz.gmail.com \
    --to=stephane.chazelas@gmail.com \
    --cc=embe8573@student.uu.se \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).