[SOLVED] Entering in arithmetic expression in expression formula is miscalculated

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!
User avatar
bgoodr
Posts: 228
Joined: Sat Jun 14, 2014 9:40 pm

[SOLVED] Entering in arithmetic expression in expression formula is miscalculated

Post by bgoodr »

Code: Select all

$ dpkg --list freecad-daily\*
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                  Version                              Architecture Description
+++-=====================-====================================-============-======================================================
ii  freecad-daily         0.19~pre2~202001221536~ubuntu19.10.1 all          Extensible Open Source CAx program
ii  freecad-daily-common  0.19~pre2~202001221536~ubuntu19.10.1 all          Extensible Open Source CAx program - common files
un  freecad-daily-doc     <none>                               <none>       (no description available)
un  freecad-daily-python2 <none>                               <none>       (no description available)
ii  freecad-daily-python3 0.19~pre2~202001221536~ubuntu19.10.1 amd64        Extensible Open Source CAx program - Python 3 binaries
un  freecad-daily-runtime <none>                               <none>       (no description available)
$ 
In case it matters, here is my Preferences/Unit settings:

Image

When padding a simple Sketch in the XY plane, I used the Image button and typed in this:

Code: Select all

8 + 3 / 4 + 4 + 7 / 8 + 10.5 + 4 + 3 / 16 + 9 + 11 / 16
And saw the Result calculated it to 38.0000 units (happens to be in inches):

Image

I clicked Ok button and saw the usual result, still showing 38.0000 inches:

Image

But then click on the Pad object in the Model view, and see this:

Image

Where is the 1.4961 in coming from?

Earlier it said 38.0000. And it is correct if you evaluate that exact expression as a Python expression in the console:

Image

Is this a bug or is it some other unit getting used by default and I am just unaware of it? If it is a bug, I'll a file a PR.

I used Measure Distance on the two vertices in the Z direction, and it reads 1.4961 in:

Image
Last edited by bgoodr on Fri Jan 24, 2020 2:58 pm, edited 1 time in total.
chrisb
Veteran
Posts: 53919
Joined: Tue Mar 17, 2015 9:14 am

Re: Entering in arithmetic expression in expression formula is miscalculated

Post by chrisb »

I'm afraid you have to add the unit:

Code: Select all

8in + 3in / 4 + 4in + 7in / 8 + 10.5in + 4in + 3in / 16 + 9in + 11in / 16
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
Forthman
Veteran
Posts: 2650
Joined: Fri Apr 27, 2018 11:23 am
Location: Tarn-et-Garonne (82)

Re: Entering in arithmetic expression in expression formula is miscalculated

Post by Forthman »

1.4961 in = 38mm :idea:
User avatar
bgoodr
Posts: 228
Joined: Sat Jun 14, 2014 9:40 pm

Re: Entering in arithmetic expression in expression formula is miscalculated

Post by bgoodr »

chrisb wrote: Thu Jan 23, 2020 5:00 am I'm afraid you have to add the unit:

Code: Select all

8in + 3in / 4 + 4in + 7in / 8 + 10.5in + 4in + 3in / 16 + 9in + 11in / 16
You are right: I now read inside Expressions, specifically the Units section, this:
All values must have a unit. Therefore you must in general use a unit for values in spreadsheets.
In some cases it works even without a unit, for example if you have e.g. in spreadsheet cell B1 just the number 1.5 and refer to it for a pad height. This only works because the pad height predefines the unit mm that is used if no unit is given.
User avatar
bgoodr
Posts: 228
Joined: Sat Jun 14, 2014 9:40 pm

Re: Entering in arithmetic expression in expression formula is miscalculated

Post by bgoodr »

Forthman wrote: Thu Jan 23, 2020 5:54 am 1.4961 in = 38mm :idea:
You are right too.
User avatar
bgoodr
Posts: 228
Joined: Sat Jun 14, 2014 9:40 pm

Re: Entering in arithmetic expression in expression formula is miscalculated

Post by bgoodr »

What threw me is I didn't know what units were for the expression inside the parenthesized expression. I had to dig deeply in the wiki, and only after that deep dive, did I stumble upon the Units section. And where I would have expected to see some discussion on this, in the PartDesign Pad page, does not mention the units being interpreted in this manner as it should IMO.

Here's an idea: It would be better to show "mm" after the parenthesized expression, just like "in" is there right after "1.4961".

Image
User avatar
bgoodr
Posts: 228
Joined: Sat Jun 14, 2014 9:40 pm

Re: Entering in arithmetic expression in expression formula is miscalculated

Post by bgoodr »

Back to the input expression: Having the user type in "in" unit specifier that many times in the expression is a bit tedious. Is there a particular reason why this syntax was not available?:

Code: Select all

(8 + 3 / 4 + 4 + 7 / 8 + 10.5 + 4 + 3 / 16 + 9 + 11 / 16)in
That means, first calculate the number inside the parentheses, and then treat it as inches. Well, if that would be ambiguous with other syntax, then scratch that. Surely there is a reason for disallowing it, but I'm not seeing it, as it makes sense: the units come after a number, so why not an unitless expression in parentheses?
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: Entering in arithmetic expression in expression formula is miscalculated

Post by vocx »

bgoodr wrote: Fri Jan 24, 2020 4:53 am ...Surely there is a reason for disallowing it, ... so why not an unitless expression in parentheses?
The expression engine is not trivial. I cannot explain it with detail but it requires using a "parser", which analyzes the strings and finds what is what. Simply put, it's hard.

https://github.com/FreeCAD/FreeCAD/blob ... onParser.y
https://github.com/FreeCAD/FreeCAD/blob ... onParser.l

In his own branch, realthunder has created an expression engine which is basically a subset or re-implementation of the Python language, meaning that it would be possible to use Python expressions and functions in a nicer way. However, this presents some security risks so these experiments aren't currently part of the FreeCAD master code. It is uncertain if they will make it to FreeCAD at some point, maybe realthunder will revisit the topic at another time, for 0.20 and beyond.

https://github.com/realthunder/FreeCAD_ ... preadsheet
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
chrisb
Veteran
Posts: 53919
Joined: Tue Mar 17, 2015 9:14 am

Re: Entering in arithmetic expression in expression formula is miscalculated

Post by chrisb »

Use instead:

Code: Select all

(8 + 3 / 4 + 4 + 7 / 8 + 10.5 + 4 + 3 / 16 + 9 + 11 / 16)*1in
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
bgoodr
Posts: 228
Joined: Sat Jun 14, 2014 9:40 pm

Re: Entering in arithmetic expression in expression formula is miscalculated

Post by bgoodr »

vocx wrote: Fri Jan 24, 2020 6:50 am The expression engine is not trivial. I cannot explain it with detail but it requires using a "parser", which analyzes the strings and finds what is what. Simply put, it's hard.
Got it. I had a hunch there was a reason and there is. Thanks!
Post Reply