From: Oliver Kiddle <okiddle@yahoo.co.uk>
To: Stephane CHAZELAS <Stephane_CHAZELAS@yahoo.fr>
Cc: Zsh hackers list <zsh-workers@sunsite.dk>
Subject: Re: LC_NUMERIC=fr_FR and floating point arithmetics
Date: Mon, 10 Mar 2003 09:58:20 +0100 [thread overview]
Message-ID: <1556.1047286700@finches.logica.co.uk> (raw)
In-Reply-To: <20030224184502.A9922@pcchazelas.free.fr>
On 24 Feb, Stephane CHAZELAS wrote:
> $ LC_NUMERIC=fr_FR ksh93 -c 'float a=1; echo $(( a / 3 ))'
> 0,333333333333
> $ LC_NUMERIC=fr_FR zsh -c 'float a=1; echo $(( a / 3 ))'
> 0,33333333333333331.
>
> zsh seems to assume that "." is the decimal separator which is
> not correct in a french locale.
>
> I agree this is confusing. A ksh93 script such as
> echo $(( 1. / 3 ))
>
> won't work under french locale.
> (must be echo $(( 1, / 3 )) )
Which is worse in my opinion. $(( 1.1 )) really ought to be interpreted
as a 1 point 1 regardless of locale - it is otherwise impossible to use
decimals in portable shell scripts.
If we didn't have backward and ksh compatibility to consider, I would
be inclined to say that $a for floats and $(( ... )) should always use
the C locale and anyone wanting locale specific output could use printf
(see below) (implementing this is easy; just save the LC_NUMERIC locale
in convfloat()).
But keeping locale handling in output means that scalar parameters
can't be reused in a calculation. And making math evaluation accept
either `.' or whatever the locale dictates is not easy when you
consider that a comma in math evaluation is used as a separator:
(( a=1, 3.4 ))
Also, because we allow this:
% a='3 + 4'
% echo $(( a ))
7
you can't just interpret the comma in a parameter expansion. So I can't
think of any solution except limiting math evaluation to the C locale.
So what can we do?
I did a grep for mon_decimal_point in /usr/share/i18n/locales to see
if any other characters are used as decimal separators. It indicates
that Portugal use `$' as their decimal separator. Seems weird. Apart
from that it is , or . for the rest of the world except Burkina Faso
who apparently use something outside the normal ASCII range.
Currently, a bug prevents locale handling for printf (on at least some
platforms). The bug causing this is in math.c where it does:
prev_locale = setlocale(LC_NUMERIC, NULL);
setlocale(LC_NUMERIC, "POSIX");
The second setlocale call clobbers prev_locale - we have to use
dupstring(). The patch fixes this.
Oliver
diff -ur zsh-latest/Src/math.c localeprob2/Src/math.c
--- zsh-latest/Src/math.c 2002-12-19 10:58:19.000000000 +0000
+++ localeprob2/Src/math.c 2003-03-09 01:13:18.000000000 +0000
@@ -399,12 +399,12 @@
/* it's a float */
yyval.type = MN_FLOAT;
#ifdef USE_LOCALE
- prev_locale = setlocale(LC_NUMERIC, NULL);
+ prev_locale = dupstring(setlocale(LC_NUMERIC, NULL));
setlocale(LC_NUMERIC, "POSIX");
#endif
yyval.u.d = strtod(ptr, &nptr);
#ifdef USE_LOCALE
- setlocale(LC_NUMERIC, prev_locale);
+ if (prev_locale) setlocale(LC_NUMERIC, prev_locale);
#endif
if (ptr == nptr || *nptr == '.') {
zerr("bad floating point constant", NULL, 0);
next prev parent reply other threads:[~2003-03-10 8:55 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-02-24 17:45 Stephane CHAZELAS
2003-03-10 8:58 ` Oliver Kiddle [this message]
2003-03-10 19:25 ` Zefram
2003-03-11 10:35 ` Oliver Kiddle
2003-03-12 3:32 ` Philippe Troin
2003-03-12 8:30 ` Oliver Kiddle
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=1556.1047286700@finches.logica.co.uk \
--to=okiddle@yahoo.co.uk \
--cc=Stephane_CHAZELAS@yahoo.fr \
--cc=zsh-workers@sunsite.dk \
/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).