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:
> 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
> Second, is it enough to just add 50 before doing the division? I.e., change
> it to:
> 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.)