discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Incorrect Results When Comparing Array Values

NS
Nathan Sokalski
Tue, Mar 26, 2024 7:42 PM

I have an array called upperflatpoints, when I run the following code:

echo("upperflatpoints",upperflatpoints);
echo(upperflatpoints[1],upperflatpoints[2],upperflatpoints[1]==upperflatpoints[2],upperflatpoints[1][0]==upperflatpoints[2][0],upperflatpoints[1][1]==upperflatpoints[2][1],upperflatpoints[1].x==upperflatpoints[2].x,upperflatpoints[1].y==upperflatpoints[2].y);

I get the following output in the Console:

ECHO: "upperflatpoints", [[1.625, 1.625], [37.75, 1.625], [37.75, 1.625], [26, 1.625]]

ECHO: [37.75, 1.625], [37.75, 1.625], false, true, false, true, false

Notice the following in the output:

The values at index 1 & 2 have equal values
2.
OpenSCad returns false when comparing [1] & [2]
3.
When comparing the first value in each item, OpenSCad returns true, but when comparing the second value, OpenSCad returns false

Why do the first & second values not return the same value? My ultimate goal is to test for equality between 2 points, but I cannot seem to find a way to do this when only part of the result is correct. Any ideas?

Nathan Sokalski
njsokalski@hotmail.commailto:njsokalski@hotmail.com

I have an array called upperflatpoints, when I run the following code: echo("upperflatpoints",upperflatpoints); echo(upperflatpoints[1],upperflatpoints[2],upperflatpoints[1]==upperflatpoints[2],upperflatpoints[1][0]==upperflatpoints[2][0],upperflatpoints[1][1]==upperflatpoints[2][1],upperflatpoints[1].x==upperflatpoints[2].x,upperflatpoints[1].y==upperflatpoints[2].y); I get the following output in the Console: ECHO: "upperflatpoints", [[1.625, 1.625], [37.75, 1.625], [37.75, 1.625], [26, 1.625]] ECHO: [37.75, 1.625], [37.75, 1.625], false, true, false, true, false Notice the following in the output: 1. The values at index 1 & 2 have equal values 2. OpenSCad returns false when comparing [1] & [2] 3. When comparing the first value in each item, OpenSCad returns true, but when comparing the second value, OpenSCad returns false Why do the first & second values not return the same value? My ultimate goal is to test for equality between 2 points, but I cannot seem to find a way to do this when only part of the result is correct. Any ideas? Nathan Sokalski njsokalski@hotmail.com<mailto:njsokalski@hotmail.com>
HL
Hans L
Tue, Mar 26, 2024 8:46 PM

Binary floating point numbers can't represent all decimal values exactly,
so echo of values gets truncated/rounded to some extent.
So values that appear the same from an echo do not always test the same for
equality.  I assume these values are coming from some kind of
calculations.  Unless the exact same operations are performed in the exact
order with the same inputs, then they could be just a single
least-significant-bit different which would fail an equality test.

If you must do this sort of testing for same points from calculated values,
I would checking that the distance between two points is below than some
very small epsilon
e.g:  norm(p1 - p2) < 1e-6

On Tue, Mar 26, 2024 at 2:42 PM Nathan Sokalski via Discuss <
discuss@lists.openscad.org> wrote:

I have an array called upperflatpoints, when I run the following code:

echo("upperflatpoints",upperflatpoints);

echo(upperflatpoints[1],upperflatpoints[2],upperflatpoints[1]==upperflatpoints[2],upperflatpoints[1][0]==upperflatpoints[2][0],upperflatpoints[1][1]==upperflatpoints[2][1],upperflatpoints[1].x==upperflatpoints[2].x,upperflatpoints[1].y==upperflatpoints[2].y);

I get the following output in the Console:

ECHO: "upperflatpoints", [[1.625, 1.625], [37.75, 1.625], [37.75, 1.625],
[26, 1.625]]

ECHO: [37.75, 1.625], [37.75, 1.625], false, true, false, true, false

Notice the following in the output:

1. The values at index 1 & 2 have equal values
2. OpenSCad returns false when comparing [1] & [2]
3. When comparing the first value in each item, OpenSCad returns true,
but when comparing the second value, OpenSCad returns false

Why do the first & second values not return the same value? My ultimate
goal is to test for equality between 2 points, but I cannot seem to find a
way to do this when only part of the result is correct. Any ideas?

Nathan Sokalski
njsokalski@hotmail.com


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Binary floating point numbers can't represent all decimal values exactly, so echo of values gets truncated/rounded to some extent. So values that appear the same from an echo do not always test the same for equality. I assume these values are coming from some kind of calculations. Unless the exact same operations are performed in the exact order with the same inputs, then they could be just a single least-significant-bit different which would fail an equality test. If you must do this sort of testing for same points from calculated values, I would checking that the distance between two points is below than some very small epsilon e.g: norm(p1 - p2) < 1e-6 On Tue, Mar 26, 2024 at 2:42 PM Nathan Sokalski via Discuss < discuss@lists.openscad.org> wrote: > I have an array called upperflatpoints, when I run the following code: > > echo("upperflatpoints",upperflatpoints); > > echo(upperflatpoints[1],upperflatpoints[2],upperflatpoints[1]==upperflatpoints[2],upperflatpoints[1][0]==upperflatpoints[2][0],upperflatpoints[1][1]==upperflatpoints[2][1],upperflatpoints[1].x==upperflatpoints[2].x,upperflatpoints[1].y==upperflatpoints[2].y); > > I get the following output in the Console: > > ECHO: "upperflatpoints", [[1.625, 1.625], [37.75, 1.625], [37.75, 1.625], > [26, 1.625]] > > ECHO: [37.75, 1.625], [37.75, 1.625], false, true, false, true, false > > Notice the following in the output: > > 1. The values at index 1 & 2 have equal values > 2. OpenSCad returns false when comparing [1] & [2] > 3. When comparing the first value in each item, OpenSCad returns true, > but when comparing the second value, OpenSCad returns false > > Why do the first & second values not return the same value? My ultimate > goal is to test for equality between 2 points, but I cannot seem to find a > way to do this when only part of the result is correct. Any ideas? > > > Nathan Sokalski > njsokalski@hotmail.com > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
JB
Jordan Brown
Wed, Mar 27, 2024 2:35 AM

One good thing to try to diagnose this kind of thing is to subtract the
two values, and echo the difference.  Thus, in this case,

echo(upperflatpoints[1][1] - upperflatpoints[2][1]);

Hans is probably right, that the two values are almost the same, but
not quite.  This will show that clearly; probably the difference will be
some number around 1e-15.

One good thing to try to diagnose this kind of thing is to subtract the two values, and echo the difference.  Thus, in this case, echo(upperflatpoints[1][1] - upperflatpoints[2][1]); Hans is probably right, that the two values are *almost* the same, but not quite.  This will show that clearly; probably the difference will be some number around 1e-15.