flauwekeul / honeycomb

Create hex grids easily, in node or the browser.

Home Page:https://abbekeultjes.nl/honeycomb

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nudge Float Rounding Issue

glitchassassin opened this issue · comments

I'm back!

I'm not sure how this was avoided before, but with the fix in the latest version of honeycomb-grid I'm now getting float rounding errors:

Error: Cube coordinates must have a sum of 0. q: 4.000001, r: -3.999999, s: -0.000002, sum: 2.7955602446123017e-16.

You can duplicate with the following:

hex = Honeycomb.extendHex()({q: 4, r: -4, s: 0})
hex.nudge()

The fix is probably to make this zero check more flexible to allow for float rounding issues:

https://github.com/flauwekeul/honeycomb/blob/master/src/hex/index.js#L253

I think this should do the trick, I'm having trouble getting the tests to run on Windows:

if (Math.abs(q + r + s > Number.EPSILON)) {

See discussion at https://stackoverflow.com/a/41716722 and reference documentation https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/EPSILON

Doing some hacky testing, this does not in fact do the trick. Nudging a hex (-11, 16, -5) results in a rounding error of 10 * Number.EPSILON (and it seems to go up from there).

Since our nudge epsilon is on the order of 1e-6, I tested a rounding epsilon of 1e-7 and it seems to be working in my use case:

if (Math.abs(q + r + s > 1e-7)) {

Woops, didn't mean to close this immediately.

I've just released 3.1.3 that should contain a working nudge(). Thanks a lot for finding the bug and (trying to) solve it! I've tried out some things and in the end went with something similar to your solution (see cbb1db9). I hope this pesky bug has now been solved for good. If not, feel free to open another issue.

Thanks and happy holidays! 🎄