Skip to content

Commit

Permalink
Handle double to int typecasts in ISEA better
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kbevers committed May 24, 2018
1 parent 6db260f commit 8795737
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/PJ_isea.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 8795737

Please sign in to comment.