Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

I wish you humans would leave me alone.


devel / comp.lang.awk / Re: Rounding question (gawk)

SubjectAuthor
* Rounding question (gawk)Kenny McCormack
+* Re: Rounding question (gawk)Janis Papanagnou
|+- Re: Rounding question (gawk)Janis Papanagnou
|+* Re: Rounding question (gawk)Kaz Kylheku
||`- Re: Rounding question (gawk)Janis Papanagnou
|`- Re: Rounding question (gawk)tTh
+- Re: Rounding question (gawk)Ben Bacarisse
`* Re: Rounding question (gawk)Kaz Kylheku
 `- Re: Rounding question (gawk)Kenny McCormack

1
Rounding question (gawk)

<tv1iej$16kf7$1@news.xmission.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1403&group=comp.lang.awk#1403

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.lang.awk
Subject: Rounding question (gawk)
Date: Fri, 17 Mar 2023 11:20:51 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <tv1iej$16kf7$1@news.xmission.com>
Injection-Date: Fri, 17 Mar 2023 11:20:51 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="1266151"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Fri, 17 Mar 2023 11:20 UTC

Note: Both of these questions are in the "I think it is right, but am
asking to make sure" category.

I have a GAWK program that contains the following construct:

strftime("%T",($14+$15)/100,1)

which is printed out. Now, $14 and $15 are (non-negative) integer numbers
of hundredths of a second. So, you add them together and divide by 100 to
get a number of seconds, which is then printed out via strftime. I've
noticed, though, that this value is always truncated - e.g., if the sum is
299, what I get is just 2 seconds (i.e., 00:00:02). I think it would be
better if this value were rounded instead of truncated.

So, my first question is: Is there any kind of rounding function in GAWK?
(I think there is not; the only references to "round" I could find in the
man page seem to do with MPFR stuff, which I'd rather not use for this
purpose).

Second, is it enough to just add 50 before doing the division? I.e., change
it to:

strftime("%T",($14+$15+50)/100,1)

Would it be that easy?

--

"If God wanted us to believe in him, he'd exist."

(Linda Smith on "10 Funniest Londoners", TimeOut, 23rd June, 2005.)

Re: Rounding question (gawk)

<tv20m6$20vm6$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1406&group=comp.lang.awk#1406

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.awk
Subject: Re: Rounding question (gawk)
Date: Fri, 17 Mar 2023 16:23:50 +0100
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <tv20m6$20vm6$1@dont-email.me>
References: <tv1iej$16kf7$1@news.xmission.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 17 Mar 2023 15:23:50 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="1dfab266009a1d3620c01eb02fd7a582";
logging-data="2129606"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX195+2XL5lKKovLCu1Uf5CyI"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:VH361Nd084Gftr3KCnTOgiIo0Ns=
In-Reply-To: <tv1iej$16kf7$1@news.xmission.com>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Fri, 17 Mar 2023 15:23 UTC

On 17.03.2023 12:20, Kenny McCormack wrote:
> Note: Both of these questions are in the "I think it is right, but am
> asking to make sure" category.
>
> I have a GAWK program that contains the following construct:
>
> strftime("%T",($14+$15)/100,1)
>
> which is printed out. Now, $14 and $15 are (non-negative) integer numbers
> of hundredths of a second. So, you add them together and divide by 100 to
> get a number of seconds, which is then printed out via strftime. I've
> noticed, though, that this value is always truncated - e.g., if the sum is
> 299, what I get is just 2 seconds (i.e., 00:00:02). I think it would be
> better if this value were rounded instead of truncated.
>
> So, my first question is: Is there any kind of rounding function in GAWK?

Not that I know of, but I seem to recall that such an awk-function
can be found in the GNU Awk manual.

> (I think there is not; the only references to "round" I could find in the
> man page seem to do with MPFR stuff, which I'd rather not use for this
> purpose).
>
> Second, is it enough to just add 50 before doing the division? I.e., change
> it to:
>
> strftime("%T",($14+$15+50)/100,1)
>
> Would it be that easy?

That depends on your (rounding-)requirements. Your approach is the
simple "always upwards rounding at .5", other applications may want
to round up or down depending on the preceding digit. (In the GNU
Awk manual there's a chapter "Rounding Numbers" that explains it a
bit, and I think in the know (old) paper "What scientists should
know about FP numbers" (or a similar sounding title) there's yet
more details about rounding strategies.)

Janis

Re: Rounding question (gawk)

<87edpns7f0.fsf@bsb.me.uk>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1407&group=comp.lang.awk#1407

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ben.lists@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.awk
Subject: Re: Rounding question (gawk)
Date: Fri, 17 Mar 2023 15:51:15 +0000
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <87edpns7f0.fsf@bsb.me.uk>
References: <tv1iej$16kf7$1@news.xmission.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="84aee67ef11d8ea67d565ab3cd6c291d";
logging-data="2138033"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1//wc3I3lA+48xJNCfuHuywsOSUHxFVmDs="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:l0zhC8vnwm9qfLYXjfF6eYT8hfg=
sha1:2YHQ0rBQEZaybpEAivjT0VSb23Q=
X-BSB-Auth: 1.b93fb589732452f74647.20230317155115GMT.87edpns7f0.fsf@bsb.me.uk
 by: Ben Bacarisse - Fri, 17 Mar 2023 15:51 UTC

gazelle@shell.xmission.com (Kenny McCormack) writes:

> Second, is it enough to just add 50 before doing the division? I.e., change
> it to:
>
> strftime("%T",($14+$15+50)/100,1)
>
> Would it be that easy?

My only worry would be that you rely on something not explicitly
documented -- that strftime will truncate the value it receives. If
GAWK's strftime "shim" decides to be "helpful" and round the floating
value, your results will be off again. Seems very unlikely, but since
your code made me go and check if that was guaranteed, I would want to
assure future readers (such as myself) by making it explicit:

strftime("%T", int(($14+$15+50)/100), 1)

--
Ben.

Re: Rounding question (gawk)

<tv39t9$2asek$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1409&group=comp.lang.awk#1409

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.awk
Subject: Re: Rounding question (gawk)
Date: Sat, 18 Mar 2023 04:07:20 +0100
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <tv39t9$2asek$1@dont-email.me>
References: <tv1iej$16kf7$1@news.xmission.com> <tv20m6$20vm6$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 18 Mar 2023 03:07:21 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="1304a8e9e6cb483b4c642a5889513c8e";
logging-data="2453972"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19hpz0BUGaLwtfrDIFmwcsK"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:W772WAXStsShJosW6ZNhbDw76H4=
In-Reply-To: <tv20m6$20vm6$1@dont-email.me>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Sat, 18 Mar 2023 03:07 UTC

On 17.03.2023 16:23, Janis Papanagnou wrote:
>
> [...] and I think in the known (old) paper "What scientists should
> know about FP numbers" (or a similar sounding title) there's yet
> more details about rounding strategies.)

Exact title: "What Every Computer Scientist Should Know About
Floating-Point Arithmetic". There are several PDF formats, e.g.
https://www.itu.dk/~sestoft/bachelor/IEEE754_article.pdf
https://docs.oracle.com/cd/E19957-01/800-7895/800-7895.pdf

Janis

Re: Rounding question (gawk)

<20230317210107.144@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1411&group=comp.lang.awk#1411

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.awk
Subject: Re: Rounding question (gawk)
Date: Sat, 18 Mar 2023 04:03:52 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 37
Message-ID: <20230317210107.144@kylheku.com>
References: <tv1iej$16kf7$1@news.xmission.com>
Injection-Date: Sat, 18 Mar 2023 04:03:52 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="7cc6107a8e199e12c5fddd9c1c894f01";
logging-data="2468221"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18J8Cow2a1yUejUfO1j0k2Ks73uCoszJRM="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:wwa/uwW2fDGBuckUWk5h3TADa/E=
 by: Kaz Kylheku - Sat, 18 Mar 2023 04:03 UTC

On 2023-03-17, Kenny McCormack <gazelle@shell.xmission.com> wrote:
> Note: Both of these questions are in the "I think it is right, but am
> asking to make sure" category.
>
> I have a GAWK program that contains the following construct:
>
> strftime("%T",($14+$15)/100,1)
>
> which is printed out. Now, $14 and $15 are (non-negative) integer numbers
> of hundredths of a second. So, you add them together and divide by 100 to
> get a number of seconds, which is then printed out via strftime. I've
> noticed, though, that this value is always truncated - e.g., if the sum is
> 299, what I get is just 2 seconds (i.e., 00:00:02). I think it would be
> better if this value were rounded instead of truncated.

You could call strftime to get 00:00:02 and then stick on that value %
100 as a two-digit hundredths of a second:

00:00:02.99

I mean, if hundredths of a second, and their rounding, matter in the
program, maybe the requirements can be stretched to allow them to be
retained in the output.

> Second, is it enough to just add 50 before doing the division? I.e., change
> it to:
>
> strftime("%T",($14+$15+50)/100,1)
>
> Would it be that easy?

Yes?

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Rounding question (gawk)

<20230317210431.683@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1412&group=comp.lang.awk#1412

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.awk
Subject: Re: Rounding question (gawk)
Date: Sat, 18 Mar 2023 04:14:22 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <20230317210431.683@kylheku.com>
References: <tv1iej$16kf7$1@news.xmission.com> <tv20m6$20vm6$1@dont-email.me>
Injection-Date: Sat, 18 Mar 2023 04:14:22 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="7cc6107a8e199e12c5fddd9c1c894f01";
logging-data="2473100"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+IVClYRlsPVjyFG8TXR7N+JNb5nSnoeME="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:vls10uuoxkIyotC/atjCZk2QHZs=
 by: Kaz Kylheku - Sat, 18 Mar 2023 04:14 UTC

On 2023-03-17, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
> On 17.03.2023 12:20, Kenny McCormack wrote:
>> Second, is it enough to just add 50 before doing the division? I.e., change
>> it to:
>>
>> strftime("%T",($14+$15+50)/100,1)
>>
>> Would it be that easy?
>
> That depends on your (rounding-)requirements. Your approach is the
> simple "always upwards rounding at .5", other applications may want
> to round up or down depending on the preceding digit. (In the GNU

I think, fancy decimal rounding rules are mainly for money?

> Awk manual there's a chapter "Rounding Numbers" that explains it a
> bit, and I think in the know (old) paper "What scientists should
> know about FP numbers" (or a similar sounding title) there's yet
> more details about rounding strategies.)

That's for the floating point operations themselves: how to determine
the last bit of the mantissa. In IEEE 754, a bunch of rounding
strategies are defined. C99 has a function to set that up, fesetround,
whose argument values are in this domain:

0 Rounding is toward 0.

1 Rounding is toward nearest number.

2 Rounding is toward positive infinity.

3 Rounding is toward negative infinity.

This is not directly related to decimal rounding concepts like 5 goes up or
down to whichever number is event ("banker's rounding").

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Rounding question (gawk)

<tv3e7j$17jet$2@news.xmission.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1414&group=comp.lang.awk#1414

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.lang.awk
Subject: Re: Rounding question (gawk)
Date: Sat, 18 Mar 2023 04:21:07 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <tv3e7j$17jet$2@news.xmission.com>
References: <tv1iej$16kf7$1@news.xmission.com> <20230317210107.144@kylheku.com>
Injection-Date: Sat, 18 Mar 2023 04:21:07 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="1297885"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Sat, 18 Mar 2023 04:21 UTC

In article <20230317210107.144@kylheku.com>,
Kaz Kylheku <864-117-4973@kylheku.com> wrote:
....
>You could call strftime to get 00:00:02 and then stick on that value %
>100 as a two-digit hundredths of a second:
>
> 00:00:02.99
>
>I mean, if hundredths of a second, and their rounding, matter in the
>program, maybe the requirements can be stretched to allow them to be
>retained in the output.

I actually kinda like that. Interesting idea!

Note, FWIW, that hundredths actually aren't necessary or significant
(although it could be made so, as you suggest). It is just that that is
the format in which the data is stored - i.e., how it comes to me.

--
There's nothing more American than demanding to carry an AR-15 to
"protect yourself" but refusing to wear a mask to protect everyone else.

Re: Rounding question (gawk)

<tv3i0t$2c3gn$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1416&group=comp.lang.awk#1416

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.awk
Subject: Re: Rounding question (gawk)
Date: Sat, 18 Mar 2023 06:25:48 +0100
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <tv3i0t$2c3gn$1@dont-email.me>
References: <tv1iej$16kf7$1@news.xmission.com> <tv20m6$20vm6$1@dont-email.me>
<20230317210431.683@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 18 Mar 2023 05:25:49 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="1304a8e9e6cb483b4c642a5889513c8e";
logging-data="2493975"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+zQwbo4GTxoiNz1YP/cXKC"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:hXtPu5LtlzQXqqvB1SOROGFoAqA=
In-Reply-To: <20230317210431.683@kylheku.com>
 by: Janis Papanagnou - Sat, 18 Mar 2023 05:25 UTC

On 18.03.2023 05:14, Kaz Kylheku wrote:
> On 2023-03-17, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>> On 17.03.2023 12:20, Kenny McCormack wrote:
>>> Second, is it enough to just add 50 before doing the division? I.e., change
>>> it to:
>>>
>>> strftime("%T",($14+$15+50)/100,1)
>>>
>>> Would it be that easy?
>>
>> That depends on your (rounding-)requirements. Your approach is the
>> simple "always upwards rounding at .5", other applications may want
>> to round up or down depending on the preceding digit. (In the GNU
>
> I think, fancy decimal rounding rules are mainly for money?

Given that there had been hacks in the past where sub-cent amounts
in financial transactions had been accumulated on separate fraud
accounts we might get the impression that rounding applications is
least for money. ;-)

My thought where it matters would have been in numerical math, e.g.
in hydrodynamic and/or chaotic systems where rounding accumulations
would matter most.

Anyway, I do most of my computation in non-FP, so I am not too much
concerned, but physicists may be.

Janis

Re: Rounding question (gawk)

<tv4b3g$2b3s$2@news.gegeweb.eu>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1417&group=comp.lang.awk#1417

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!news.niel.me!news.gegeweb.eu!gegeweb.org!.POSTED.2a01:cb19:8c2c:ce00:18f:eb80:497a:b4b3!not-for-mail
From: tth@none.invalid (tTh)
Newsgroups: comp.lang.awk
Subject: Re: Rounding question (gawk)
Date: Sat, 18 Mar 2023 13:33:52 +0100
Organization: Gegeweb News Server
Message-ID: <tv4b3g$2b3s$2@news.gegeweb.eu>
References: <tv1iej$16kf7$1@news.xmission.com> <tv20m6$20vm6$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 18 Mar 2023 12:33:52 -0000 (UTC)
Injection-Info: news.gegeweb.eu; posting-account="tontonth@usenet.local"; posting-host="2a01:cb19:8c2c:ce00:18f:eb80:497a:b4b3";
logging-data="76924"; mail-complaints-to="abuse@gegeweb.eu"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.8.0
In-Reply-To: <tv20m6$20vm6$1@dont-email.me>
Cancel-Lock: sha256:YIiFqdc/hqQHP2RRZQYyku0HAz5QwgY7CIoM8fYKjrI=
Content-Language: en-US
 by: tTh - Sat, 18 Mar 2023 12:33 UTC

On 3/17/23 16:23, Janis Papanagnou wrote:

> bit, and I think in the know (old) paper "What scientists should
> know about FP numbers" (or a similar sounding title) there's yet
> more details about rounding strategies.)

https://dl.acm.org/doi/pdf/10.1145/103162.103163

--
+-------------------------------------------------------------------+
| https://danstonchat.com/1138.html |
+-------------------------------------------------------------------+

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor