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?
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?
[...] 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.)
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.
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?
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
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.)
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.
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?
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.)
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 793 |
Nodes: | 10 (1 / 9) |
Uptime: | 38:23:00 |
Calls: | 11,106 |
Calls today: | 3 |
Files: | 186,086 |
Messages: | 1,751,445 |