question

Marco Baccalaro avatar image
2 Likes"
Marco Baccalaro asked Phil BoBo edited

Global Table into IF statement

If I use this code it doesn't return 1.

if (Table("GlobalTable1")[1][1])
	return 1;

I have to do like this ...

if (Table("GlobalTable1")[1][1] > 0)
	return 1;

Why?

FlexSim 19.0.2
global tablecoding
gt1.png (2.5 KiB)
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Jordan Johnson avatar image
3 Likes"
Jordan Johnson answered Phil BoBo edited

The bracket operator for the table returns a TableElement type, which you can save off as a Variant. It apparently doesn't cast well to a bool for use in an if statement. I will add an issue to the dev list to improve it. The code you supply above works, as do these alternatives:

Variant value = Table("GlobalTable1")[1][1];
if (value) { return 1; }
if (Table("GlobalTable1")[1][1].as(double)) { return 1; }
· 2
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Marco Baccalaro avatar image Marco Baccalaro commented ·

I have a big module where I fear I've used this notation everywhere. Do you think it will be ready soon in the new version or it's better to control all the code I have and place ">0" where needed?

0 Likes 0 ·
Phil BoBo avatar image Phil BoBo ♦♦ Marco Baccalaro commented ·

You probably should update your code to not use that notation, even if we fix this notation to correctly interpret the boolean comparison of a TableElement as a double Variant.

Floating-point precision is inherently error prone, and if you are doing a comparison with 0, you may get a logical error when the value is 0.000000001 and you get into the if() statement when you don't want to.

I suggest that you update your code to appropriately compare a double value rather than trying to use a double value directly in an if() statement.

Also, even if you wanted to use a double directly in an if() statement, you should use "!=0" instead of ">0" because negative numbers compare to true when performing a boolean comparison. And if you don't want negative numbers to be true, then you absolutely should update your code to ">0" because that's what you meant anyways.

0 Likes 0 ·
Jeff Nordgren avatar image
0 Likes"
Jeff Nordgren answered
@Marco Baccalaro

I believe it is because the value in the global table is less than 1. To evaluate to "true", the value must be 1 or greater. If it could be less than 1, then your second way of doing the comparison would be better.

5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.