adjust-dpi.sh rounding problem

Chris Good chris.good at ozemail.com.au
Wed Aug 24 19:34:34 EDT 2016


> Message: 1
> Date: Tue, 23 Aug 2016 18:27:22 +0200
> From: Geert Janssens <geert.gnucash at kobaltwit.be>
> To: Chris Good <chris.good at ozemail.com.au>
> Cc: gnucash-devel at gnucash.org
> Subject: Re: adjust-dpi.sh rounding problem
> Message-ID: <3568972.mXkdpLXqho at legolas.kobaltwit.lan>
> Content-Type: text/plain; charset="us-ascii"
>
> On Monday 15 August 2016 13:11:27 Chris Good wrote:
> > Hi Geert,
> >
> > There's a couple of problems with gnucash-docs/util/adjust-dpi.sh on
> > Ubuntu 16.04 which makes it update (almost?) every image file each
> > time it runs.
> >
> > 1. The identify utility in imagemagick Version: 8:6.8.9.9-7ubuntu5.1
> > returns an unrounded dpi. E.g
> >
> > existing_dpi=$(identify -format "%x" "$figure")
> > echo $existing_dpi
> > 35.429999999999999716
> >
> > 2. It seems this script expects $existing_dpi to be suffixed with
> > (space)PixelsPerCentimeter but this doesn't happen for me.
> >
> > I was thinking of fixing these problems by using something like:
> >                 existing_dpi=$(identify -format "%x" "$figure")
> >                 existing_dpi=${existing_dpi% PixelsPerCentimeter}
> >   # strip possible trailing (space)PixelsPerCentimeter
> >
> >                 existing_dpi=$( printf '%.2f' "$existing_dpi" ) #
> > round to 2 decimals
> >
> > I guess there is a way to do this all on 1 line but I'd have to do
> > some research and I prefer KISS principle anyway.
> >
> > Is this portable enough?
>
> Hi Chris,
>
> I find that my identify version isn't printing the PixelsPerCentimeter 
> suffix
> either (Fedora 23, ImageMagick-6.9.2.7-1.fc23.x86_64). Your code to strip
it 
> is
> good to catch both ways. You'll have to alter the script to not add it to 
> the
> reference resolution either or you'd still be comparing apples to
oranges...
>
> As for portability, I find that printf is locale-sensitive (it takes a ','

> as decimal
> separator in my locale) while the output of identify is always using a '.'

> for
> decimal separator.
>
> So instead of printf, you may want to reuse the "bc" command like so:
> existing_dpi=$(echo "scale=2; $existing_dpi/1.00" | bc)
>
> And still this will need some refinement, because bc (as well as printf) 
> don't
> *round*, they *truncate*. The result of the calculation in this case would

> be
> 35.42, while the reference resolution is 35.43. I'd have to research more
to
> find a routine that properly rounds instead of truncates, but I'm out of 
> time
> right now...
>
> Regards,
>
> Geert

Hi Geert,

Thanks for your reply.
It's annoying that identify doesn't use the locale decimal separator.
BTW, my locale is en_AU.UTF-8.

Are you sure bash printf doesn't round? It does for me in Ubuntu,
although I was surprised to see 0.005 sometimes does not round up
(I guess it's just the typical floating point rounding problem).
printf "%.2f\n", 1.235
1.24
printf "%.2f\n", 4321.235
4321.23				###???###
printf "%.2f\n", 4321.2351
4321.24
LC_ALL=nl_BE.UTF-8 printf "%.2f\n", 4321,235
4321,23
LC_ALL=nl_BE.UTF-8 printf "%.2f\n", 4321,2351
4321,24

The 0.005 sometimes not rounding up will not be an issue for adjust-dpi.sh
as the numbers are a lot closed to the rounded value than 0.005.

Anyway, I decided to use awk to strip the trailing 
"(space) PixelsPerCentimeter"
and do the rounding in one step.

When you get a chance (or some-else on Fedora 23?), could you please test:
echo 4321.159 | awk '{ printf("%.2f\n", $1) }'
4321.16

I'm also surprised to see my awk is also NOT using the locale decimal
separator:
$ LC_ALL=nl_BE.UTF-8 echo 4321,159 | awk '{ printf("%.2f\n", $1) }'
4321.00
LC_ALL=nl_BE.UTF-8 echo 4321.159 | awk '{ printf("%.2f\n", $1) }'
4321.16

I've never before had to worry much about different locales - Oh well, I'll
get there :-)

Regards, Chris Good
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4817 bytes
Desc: not available
URL: <http://lists.gnucash.org/pipermail/gnucash-devel/attachments/20160825/64c7a40c/attachment.p7s>


More information about the gnucash-devel mailing list