Bug in macro interpreter - anonymous function?

Hey all,

I just discovered a potential bug in ImageJs macro interpreter. There is apparently something like an anonymous function doing math with string parameters:

This macro:

variable = "hallo";
print(variable);

variable = ("hallo");
print(variable);

Leads to this output:
image

Is that known behaviour? If someone can point me to the piece of code causing this, I’m happy to help fixing it.

Cheers,
Robert

2 Likes

Hi Robert,

what’s the propose of the parenthesis? I think there’s no math behind but a reference to a “not a number”.

Cheers

Mafalda

1 Like

Hi Robert and Mafalda,

It looks like a string within parenthesis is always interpreted as a float:

variable = 4.2;
print(variable);

variable = ("4.2");
print(variable);
print(variable - 2.7);

variable = ("4.2abc");
print(variable);

Gives as output:
image

Furthermore,

variable = (NaN);
print(variable - 2.7);

variable = ("NaN");
print(variable - 2.7);

both yield NaN as output, while

variable = "NaN";
print(variable - 2.7);

results in an error.

Best regards,
Bram

4 Likes

In the latest ImageJ daily build (1.52q25)

  variable = ("hallo");
  print(variable);

outputs “NaN” instead of “0”.

Hey Wayne,

thanks for the bugfix! Would it be possible to make it return the string? IMHO that would be more correct, or am I missing a side effect? That would then also make these examples work:

name = "Robert";
print("hello " + name);
print(("hello " + name));

image

Best,
Robert

I remember this being discussed a while ago already (can’t find the place right now), where it was about string concatenation vs. numeric addition. I agree that the current behavior seems broken (one of several things that make the macro language inferior to other, full-fledged scripting languages in my opinion), but changing any of this will certainly break backwards compatibility, and there are certainly some macros out there that rely on this functionality as it is now.


See also the macro language documentation, where it says (below the Operators table):

Parentheses should not be used for string expressions (currently, expressions with parentheses are always interpreted as numeric).

2 Likes

I was just reporting the bug because some of my students struggled with that during a seminar. I had a hard time explaining why it is how it is.

I see your point.

I would love to see one.

Cheers,
Robert

I do not see a way to have it return a string without making the macro interpreter more complex and slower. One advantage of the macro language is that it is much faster (30 times faster than JavaScript) at evaluating a few lines of code. On the other hand, JavaScript, because it is compiled, runs complex and repetitive code up to 30 times faster.

1 Like