Problem using expressions/functions

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
MrGadget49
Posts: 27
Joined: Mon Oct 03, 2022 6:31 am

Problem using expressions/functions

Post by MrGadget49 »

I'm trying to use an expression in sketcher to reference a Constraint and use it in an expression function such as 'round' or 'trunc' in another expression, but it seems to be giving me the wrong result.

For example, if I enter: 'trunc(2.016)' I get the expected result of 2.000

However if I enter: 'trunc(.Constraints.input)' where .Constraints.input is equal to 2.016 I get the erroneous result of 2.008.

It has something to do with units because if I enter: 'trunc(2.016in) I also get the erroneous result of 2.008.

I get the same results if I do the same thing in a FreeCAD spreadsheet.

I know this must be something I'm forgetting or doing wrong as I believe I got this working once before.

I would certainly be grateful if someone could get me pointed in the right direction!

Jody

Code: Select all

OS: Debian GNU/Linux 10 (buster) (MATE/mate)
Word size of FreeCAD: 64-bit
Version: 0.21.30623 (Git)
Build type: Unknown
Branch: myWorkBranch
Hash: 27f08f38b1658e419e20a153e13236eff1293b77
Python 3.7.3, Qt 5.11.3, Coin 4.0.0a, Vtk 6.3.0, OCC 7.3.0
Locale: English/UnitedStates (en_US)
Installed mods: 
  * lattice2 1.0.0
  * Assembly4 0.12.4
  * Curves 0.5.8
  * fasteners 0.4.13
  * sheetmetal 0.2.57
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: Problem using expressions/functions

Post by Roy_043 »

I think it is truncating the value in mm.
MrGadget49
Posts: 27
Joined: Mon Oct 03, 2022 6:31 am

Re: Problem using expressions/functions

Post by MrGadget49 »

Roy_043 wrote: Sun Nov 27, 2022 9:45 am I think it is truncating the value in mm.
You are correct, if I change my default units to mm, it seems to work. At least it gives me the rite number. Any idea how to make it work for inches?
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: Problem using expressions/functions

Post by Roy_043 »

Untested:

Code: Select all

trunc(.Constraints.input / 1in) * 1in
MrGadget49
Posts: 27
Joined: Mon Oct 03, 2022 6:31 am

Re: Problem using expressions/functions

Post by MrGadget49 »

Thanks, I'll give it a try.

Jody
MrGadget49
Posts: 27
Joined: Mon Oct 03, 2022 6:31 am

Re: Problem using expressions/functions

Post by MrGadget49 »

YES, that did work!

Seems kind of buggy to have to do that though. Any idea where that could be found in the source code. I have a version that I built from the GIT repository, but I have zero experience with the source code. I'd like to take a look at it,but finding the place to start is the hardest part.

Jody
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Problem using expressions/functions

Post by onekk »

MrGadget49 wrote: Sun Nov 27, 2022 10:54 am ...

Jody
The problem is not about being buggy.

Problem resides in the fact that internally FreeCAD uses mm as default unit so if you use unitless quantities it uses his defaults.

But divisors could be "quantities" so something / 2 = half of something or something * 0.5 if you want use some abstraction.

so if you state that unitless numbers will use default units the result will be "a real bug" as it is not what intended.

Sadly computers have no magic in it, only algorithms so you when using expression you must be consistent.

multiply thing for 1unit will result in a correct expression that will return correct results with unit.

Better to state and specify this thing in documents and maybe it is written somewhere, but people usually don't read even the IMPORTANT.... banner on top of almost always forum sections, so probably it will be a debated things for many times going.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
MrGadget49
Posts: 27
Joined: Mon Oct 03, 2022 6:31 am

Re: Problem using expressions/functions

Post by MrGadget49 »

OK, I guess I get it. It took me a while, but it finally makes sense to me what its doing.

When I enter trunc(2.016in). It is using mm as the units for the input, and that resolves to be 51.2064mm? The answer returned must be the value of 51.000mm when that value is converted to inches, it is 2.008in? So, that's how it comes up with that value.

It seems that if I supply a units to my input, either mm or in, it returns a value with units in inches which is what I have set as my default units. If I do not supply a units, it returns a unit-less value.

I can accept that it's not buggy, but It would make more sense to me, if it just did the calculations based on what is set as the default units instead of mm.

Thanks for helping me understand this.

BTW, I did read the warning banner and documentation on FreeCAD expressions. I do know what you mean about that though. I've written a lot of documentation in my time and It sure does seem like no one is reading it. :)

Jody
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Problem using expressions/functions

Post by onekk »

MrGadget49 wrote: Sun Nov 27, 2022 6:18 pm OK, I guess I get it. It took me a while, but it finally makes sense to me what its doing.
...

Jody
As you correctly found when using your mind to see because trunc() will return a "wrong value", Python but almost anything computer language use "decimal", or to be exact binary, so if you ask to perform trunc() on a measure specified in inch FreeCAD is doing what you are asking.

Computer has to translate your measure in a "unitless number" he can work on, it have to convert between inches and mm (call it if you hate mm "FreeCAD internal computational units") and then perform the trunc() that eliminate decimals, and then it retranslate the unitless quantity in a "unit quantity".

I agree that there could be better ways, but in this case, I see no better ways, an alternative would be to threat the number as "decimal inches" and perform the conversion in "FreeCAD internal computational units" only when really used to perform some CAD operation, but probably the expression parser has to be rewritten, so you question should be better go in the "Discussion" or Developer Forum sections.

As there is some other requests, maybe adding some more "problem that have to be solved" could be useful, to have a "correct image" of what is necessary to make users more "comfortable" with FreeCAD, it is not a trivial thing, and probably it could not be solved in a short time, but once problem is raised probably developer will take it in account.

As you have asked for "source code" to inspect, probably if you are skilled enough you could even help to "put together" a solution for this and many other problems,

Sadly I'm not skilled enough in C++ to help development, but when I've raised some problems, I've found developers open to hear and if possible modify source. So probably it will not be a futile effort.

Kind Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
MrGadget49
Posts: 27
Joined: Mon Oct 03, 2022 6:31 am

Re: Problem using expressions/functions

Post by MrGadget49 »

onekk wrote: Mon Nov 28, 2022 9:40 am As you correctly found when using your mind to see because trunc() will return a "wrong value", Python but almost anything computer language use "decimal", or to be exact binary, so if you ask to perform trunc() on a measure specified in inch FreeCAD is doing what you are asking.

...

Carlo D.
Thanks Carlo!

I am a programmer with 45 years of experience in many different languages so I understand exactly what you are talking about. Unfortunately, I have just recently started to dabble in Python so it's not one of my strengths.

If it were up to me to design these type of functions, I would convert the input to a unit-less number by just removing the units. I.E. '2.06 in' would become 2.06. I would then do the function and label the output the same as the input. Using the trunc function for example the result would be 2.00 which would be returned as 2.00in. The conversion to the user set default units would be done after that. For example if the default units were mm, it would be converted to 50.8mm. I think most of the time, the input units would be the same as the default units.

The real problem with introducing changes at this stage of the game is how to handle all the legacy code that has already been written using these functions as they are. The users wouldn't like it much if you told them that all the models that use these functions would break because now we're doing things different. The only way I could think of to handle that would be to introduce new funcs with new names that could be used in place of the old functions. Maybe this can be done with a macro or something.

At this point I'm just grateful to know how it works and what I have to do to get the desired result. Probably the best thing we could do at this point would be to update the documentation so that it describes what happens if you use units with your input. Right now, it just discusses the functions as if you are using unit-less numbers.

I have enjoyed having this conversation with you!

Jody
Post Reply