### Testing Array Equality

NS
Nathan Sokalski
Sat, Feb 10, 2024 3:15 AM

When using the Boolean operators (== & !=) to compare arrays, will OpenSCad return true or false? For example, if I have the following code:

testarray1=[0,0];
testarray2=[0,0];
echo((testarray1==testarray2)?"Arrays have the same value":"The arrays are not the same");

Which one will be the result? I can, of course, compare the values individually, but it is obviously more convenient to do it as a single operation.

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

When using the Boolean operators (== & !=) to compare arrays, will OpenSCad return true or false? For example, if I have the following code: testarray1=[0,0]; testarray2=[0,0]; echo((testarray1==testarray2)?"Arrays have the same value":"The arrays are not the same"); Which one will be the result? I can, of course, compare the values individually, but it is obviously more convenient to do it as a single operation. Nathan Sokalski njsokalski@hotmail.com<mailto:njsokalski@hotmail.com>
SL
Steve Lelievre
Sat, Feb 10, 2024 4:08 AM

According to the manual, "If both operands are vectors, an equality test
returns /true/ when the vectors are identical and /false/ otherwise."

Steve

On 2024-02-09 7:15 p.m., Nathan Sokalski via Discuss wrote:

When using the Boolean operators (== & !=) to compare arrays, will
OpenSCad return true or false? For example, if I have the following code:

testarray1=[0,0];
testarray2=[0,0];
echo((testarray1==testarray2)?"Arrays have the same value":"The arrays
are not the same");

Which one will be the result? I can, of course, compare the values
individually, but it is obviously more convenient to do it as a single
operation.

Nathan Sokalski
njsokalski@hotmail.com

According to the manual, "If both operands are vectors, an equality test returns /true/ when the vectors are identical and /false/ otherwise." Steve On 2024-02-09 7:15 p.m., Nathan Sokalski via Discuss wrote: > When using the Boolean operators (== & !=) to compare arrays, will > OpenSCad return true or false? For example, if I have the following code: > > testarray1=[0,0]; > testarray2=[0,0]; > echo((testarray1==testarray2)?"Arrays have the same value":"The arrays > are not the same"); > > Which one will be the result? I can, of course, compare the values > individually, but it is obviously more convenient to do it as a single > operation. > > Nathan Sokalski > njsokalski@hotmail.com
DP
David Phillip Oster
Sat, Feb 10, 2024 7:43 AM

On Fri, Feb 9, 2024 at 8:09 PM Steve Lelievre via Discuss <

According to the manual, "If both operands are vectors, an equality test

returns true when the vectors are identical and false otherwise."

with all the usual caveats about floating point numbers are only an
approximation of mathematical real numbers, generally you want to test that
the absolute value of the difference is smaller than some small value,
rather than use '=='.

On Fri, Feb 9, 2024 at 8:09 PM Steve Lelievre via Discuss < discuss@lists.openscad.org> wrote: > According to the manual, "If both operands are vectors, an equality test returns *true* when the vectors are identical and *false* otherwise." with all the usual caveats about floating point numbers are only an approximation of mathematical real numbers, generally you want to test that the absolute value of the difference is smaller than some small value, rather than use '=='.
J
jon
Sat, Feb 10, 2024 1:28 PM

I actually ran your code, and it works as you might expect.  I am
puzzled as to why you posted this question when it was so easy to get
the answer for yourself.  Easier, in fact, than posting the question.

On 2/9/2024 10:15 PM, Nathan Sokalski via Discuss wrote:

When using the Boolean operators (== & !=) to compare arrays, will
OpenSCad return true or false? For example, if I have the following code:

testarray1=[0,0];
testarray2=[0,0];
echo((testarray1==testarray2)?"Arrays have the same value":"The arrays
are not the same");

Which one will be the result? I can, of course, compare the values
individually, but it is obviously more convenient to do it as a single
operation.

Nathan Sokalski
njsokalski@hotmail.com

To unsubscribe send an email todiscuss-leave@lists.openscad.org

--
This email has been checked for viruses by AVG antivirus software.
www.avg.com

I actually ran your code, and it works as you might expect.  I am puzzled as to why you posted this question when it was so easy to get the answer for yourself.  Easier, in fact, than posting the question. On 2/9/2024 10:15 PM, Nathan Sokalski via Discuss wrote: > When using the Boolean operators (== & !=) to compare arrays, will > OpenSCad return true or false? For example, if I have the following code: > > testarray1=[0,0]; > testarray2=[0,0]; > echo((testarray1==testarray2)?"Arrays have the same value":"The arrays > are not the same"); > > Which one will be the result? I can, of course, compare the values > individually, but it is obviously more convenient to do it as a single > operation. > > Nathan Sokalski > njsokalski@hotmail.com > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email todiscuss-leave@lists.openscad.org -- This email has been checked for viruses by AVG antivirus software. www.avg.com
JB
Jordan Brown
Sat, Feb 10, 2024 7:19 PM

On 2/9/2024 11:43 PM, David Phillip Oster via Discuss wrote:

with all the usual caveats about floating point numbers are only an
approximation of mathematical real numbers, generally you want to test
that the absolute value of the difference is smaller than some small
value, rather than use '=='.

Makes me wonder whether either (a) == should allow for some fuzz, or (b)
we should introduce ~= that would allow some fuzz.

To be precise, you want to test for whether the difference is smaller
than some small fraction of the values involved.

On 2/9/2024 11:43 PM, David Phillip Oster via Discuss wrote: > with all the usual caveats about floating point numbers are only an > approximation of mathematical real numbers, generally you want to test > that the absolute value of the difference is smaller than some small > value, rather than use '=='. Makes me wonder whether either (a) == should allow for some fuzz, or (b) we should introduce ~= that would allow some fuzz. To be precise, you want to test for whether the difference is smaller than some small fraction of the values involved.
DP
Dan Perry
Sat, Feb 10, 2024 7:32 PM

Back in my working days, probably in Cadence Design Systems SKILL language,
I used 'ne' --> nearly equal.

Dan

On Sat, Feb 10, 2024 at 7:19 PM Jordan Brown via Discuss <

On 2/9/2024 11:43 PM, David Phillip Oster via Discuss wrote:

with all the usual caveats about floating point numbers are only an
approximation of mathematical real numbers, generally you want to test that
the absolute value of the difference is smaller than some small value,
rather than use '=='.

Makes me wonder whether either (a) == should allow for some fuzz, or (b)
we should introduce ~= that would allow some fuzz.

To be precise, you want to test for whether the difference is smaller than
some small fraction of the values involved.

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

Back in my working days, probably in Cadence Design Systems SKILL language, I used 'ne' --> nearly equal. Dan On Sat, Feb 10, 2024 at 7:19 PM Jordan Brown via Discuss < discuss@lists.openscad.org> wrote: > On 2/9/2024 11:43 PM, David Phillip Oster via Discuss wrote: > > with all the usual caveats about floating point numbers are only an > approximation of mathematical real numbers, generally you want to test that > the absolute value of the difference is smaller than some small value, > rather than use '=='. > > > Makes me wonder whether either (a) == should allow for some fuzz, or (b) > we should introduce ~= that would allow some fuzz. > > To be precise, you want to test for whether the difference is smaller than > some small fraction of the values involved. > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
FH
Father Horton
Sat, Feb 10, 2024 7:50 PM

Altering the definition of == could be breaking, though I would be a bit
surprised. But I'd rather see the fuzzy comparison made explicit.

function fuzeq(a, b, epsilon = 0.001) = abs(a - b) < epsilon;

echo(fuzeq(1, 1.1));
echo(fuzeq(1, 1.00001));
echo(fuzeq(1, 1.00001, epsilon = 0.0000001));

ECHO: false
ECHO: true
ECHO: false

Altering the definition of == could be breaking, though I would be a bit surprised. But I'd rather see the fuzzy comparison made explicit. function fuzeq(a, b, epsilon = 0.001) = abs(a - b) < epsilon; echo(fuzeq(1, 1.1)); echo(fuzeq(1, 1.00001)); echo(fuzeq(1, 1.00001, epsilon = 0.0000001)); ECHO: false ECHO: true ECHO: false
SP
Sanjeev Prabhakar
Sun, Feb 11, 2024 12:05 AM

Maybe round a number to 4 or 5 decimal places and then comparison should
work.

E.g.
round(2.0000001,4)==2 , should give  "True" as result.

Function "round" needs to be written in this case

For most of the practical cases rounding a number up to 5 decimal places
should be fine

On Sun, 11 Feb, 2024, 1:20 am Father Horton via Discuss, <

Altering the definition of == could be breaking, though I would be a bit
surprised. But I'd rather see the fuzzy comparison made explicit.

function fuzeq(a, b, epsilon = 0.001) = abs(a - b) < epsilon;

echo(fuzeq(1, 1.1));
echo(fuzeq(1, 1.00001));
echo(fuzeq(1, 1.00001, epsilon = 0.0000001));

ECHO: false
ECHO: true
ECHO: false

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

Maybe round a number to 4 or 5 decimal places and then comparison should work. E.g. round(2.0000001,4)==2 , should give "True" as result. Function "round" needs to be written in this case For most of the practical cases rounding a number up to 5 decimal places should be fine On Sun, 11 Feb, 2024, 1:20 am Father Horton via Discuss, < discuss@lists.openscad.org> wrote: > Altering the definition of == could be breaking, though I would be a bit > surprised. But I'd rather see the fuzzy comparison made explicit. > > function fuzeq(a, b, epsilon = 0.001) = abs(a - b) < epsilon; > > echo(fuzeq(1, 1.1)); > echo(fuzeq(1, 1.00001)); > echo(fuzeq(1, 1.00001, epsilon = 0.0000001)); > > ECHO: false > ECHO: true > ECHO: false > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
RW
Raymond West
Sun, Feb 11, 2024 12:25 PM

you have to include a value for the fuzziness, so not difficult to add
that, as you've shown. It's OK for numbers, but how about for shapes?
Personally, I have not looked at in detail, but I think for practical
purposes, we could not bother with floating point. 64 bit integer can
cover a range big enough for most, imnsho.

On 10/02/2024 19:50, Father Horton via Discuss wrote:

Altering the definition of == could be breaking, though I would be a
bit surprised. But I'd rather see the fuzzy comparison made explicit.

function fuzeq(a, b, epsilon = 0.001) = abs(a - b) < epsilon;

echo(fuzeq(1, 1.1));
echo(fuzeq(1, 1.00001));
echo(fuzeq(1, 1.00001, epsilon = 0.0000001));

ECHO: false
ECHO: true
ECHO: false

To unsubscribe send an email todiscuss-leave@lists.openscad.org

you have to include a value for the fuzziness, so not difficult to add that, as you've shown. It's OK for numbers, but how about for shapes? Personally, I have not looked at in detail, but I think for practical purposes, we could not bother with floating point. 64 bit integer can cover a range big enough for most, imnsho. On 10/02/2024 19:50, Father Horton via Discuss wrote: > Altering the definition of == could be breaking, though I would be a > bit surprised. But I'd rather see the fuzzy comparison made explicit. > > function fuzeq(a, b, epsilon = 0.001) = abs(a - b) < epsilon; > > echo(fuzeq(1, 1.1)); > echo(fuzeq(1, 1.00001)); > echo(fuzeq(1, 1.00001, epsilon = 0.0000001)); > > ECHO: false > ECHO: true > ECHO: false > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email todiscuss-leave@lists.openscad.org
MM
Michael Möller
Sun, Feb 11, 2024 2:54 PM

Ohhh, I am curious, too, but refrained from asking. I wondered, was the
question badly phrased, as in: what is the result for undef, unequal sizes,
unequal nesting ? Maybe the test was done, but the answer was "wrong".

A few years working in IT support led me to reduce a problem description
"interview" to:

1. what did you do (code)?
2. what did you expect?
3. what did happen?

Michael

lør. 10. feb. 2024 14.29 skrev jon via Discuss discuss@lists.openscad.org:

I actually ran your code, and it works as you might expect.  I am puzzled
as to why you posted this question when it was so easy to get the answer
for yourself.  Easier, in fact, than posting the question.

On 2/9/2024 10:15 PM, Nathan Sokalski via Discuss wrote:

When using the Boolean operators (== & !=) to compare arrays, will
OpenSCad return true or false? For example, if I have the following code:

testarray1=[0,0];
testarray2=[0,0];
echo((testarray1==testarray2)?"Arrays have the same value":"The arrays are
not the same");

Which one will be the result? I can, of course, compare the values
individually, but it is obviously more convenient to do it as a single
operation.

Nathan Sokalski
njsokalski@hotmail.com