diff options
| author | Kristian Evers <kristianevers@gmail.com> | 2018-05-24 14:56:27 +0200 |
|---|---|---|
| committer | Kristian Evers <kristianevers@gmail.com> | 2018-05-24 15:04:36 +0200 |
| commit | 8795737e29efe59526565d757bc560d9d19163cb (patch) | |
| tree | 65462b902c4054581bebdd2c7ff8b693e47e565e | |
| parent | 6db260ff7857fcec63e5988ed43ecebc358a811d (diff) | |
| download | PROJ-8795737e29efe59526565d757bc560d9d19163cb.tar.gz PROJ-8795737e29efe59526565d757bc560d9d19163cb.zip | |
Handle double to int typecasts in ISEA better
Originally the code was doing double to int conversions like
y = (int)(x + 0.5)
which results in rounding when typecasting. In an earlier attempt to
avoid buffer overflows in integer typecasts this was changed to
y = lround(x + 0.5)
which doesn't give the origial results. We fix that here by instead
doing
y = lround(x)
It is safe to so as long as x is positive.
| -rw-r--r-- | src/PJ_isea.c | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/src/PJ_isea.c b/src/PJ_isea.c index c1ac2f00..3c811a18 100644 --- a/src/PJ_isea.c +++ b/src/PJ_isea.c @@ -679,7 +679,7 @@ static int isea_dddi_ap3odd(struct isea_dgg *g, int quad, struct isea_pt *pt, /* TODO I think sidelength is always x.5, so * (int)sidelength * 2 + 1 might be just as good */ - maxcoord = lround((sidelength * 2.0 + 0.5)); + maxcoord = lround((sidelength * 2.0)); v = *pt; hexbin2(hexwidth, v.x, v.y, &h.x, &h.y); @@ -750,7 +750,7 @@ static int isea_dddi(struct isea_dgg *g, int quad, struct isea_pt *pt, } /* todo might want to do this as an iterated loop */ if (g->aperture >0) { - sidelength = lround((pow(g->aperture, g->resolution / 2.0) + 0.5)); + sidelength = lround(pow(g->aperture, g->resolution / 2.0)); } else { sidelength = g->resolution; } @@ -833,20 +833,20 @@ static int isea_disn(struct isea_dgg *g, int quad, struct isea_pt *di) { return g->serial; } /* hexes in a quad */ - hexes = lround((pow(g->aperture, g->resolution) + 0.5)); + hexes = lround(pow(g->aperture, g->resolution)); if (quad == 11) { g->serial = 1 + 10 * hexes + 1; return g->serial; } if (g->aperture == 3 && g->resolution % 2 == 1) { - height = lround((pow(g->aperture, (g->resolution - 1) / 2.0))); + height = lround(floor((pow(g->aperture, (g->resolution - 1) / 2.0)))); sn = ((int) di->x) * height; sn += ((int) di->y) / height; sn += (quad - 1) * hexes; sn += 2; } else { - sidelength = lround((pow(g->aperture, g->resolution / 2.0) + 0.5)); - sn = lround(((quad - 1) * hexes + sidelength * di->x + di->y + 2)); + sidelength = lround((pow(g->aperture, g->resolution / 2.0))); + sn = lround(floor(((quad - 1) * hexes + sidelength * di->x + di->y + 2))); } g->serial = sn; @@ -874,8 +874,8 @@ static int isea_hex(struct isea_dgg *g, int tri, return 1; #ifdef FIXME - d = lround(v.x); - i = lround(v.y); + d = lround(floor(v.x)); + i = lround(floor(v.y)); /* Aperture 3 odd resolutions */ if (g->aperture == 3 && g->resolution % 2 != 0) { @@ -903,7 +903,7 @@ static int isea_hex(struct isea_dgg *g, int tri, } /* aperture 3 even resolutions and aperture 4 */ - sidelength = lround((pow(g->aperture, g->resolution / 2.0) + 0.5)); + sidelength = lround((pow(g->aperture, g->resolution / 2.0))); if (g->quad == 0) { hex->x = 0; hex->y = sidelength; |
