News:

  • October 14, 2025, 12:56:07 AM

Login with username, password and session length

Author Topic: Math not updating  (Read 1343 times)

unsyndicated

  • Full Member
  • ***
  • Posts: 21
Math not updating
« on: May 26, 2022, 10:26:07 AM »
Hello,

I've put in a fairly simple math formula, but for some unknown reason it won't update.  I"m inputting a set speed for a extruder, and just trying to have the color feeder feed rate percentage stay consistent with the extruder speed.    I'm triggering it along with the extruder output scale on the same ST1 contact.  The only value that shows up is the incremental adjustment if I want to manually darken or lighten the resin.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3761
    • Host Engineering
Re: Math not updating
« Reply #1 on: May 26, 2022, 10:58:13 AM »
To get more accurate answers, do divisions last, especially integer division.

Change the part of your expression
from ((V409 / 20) * V400)
to ((V409 * V400) / 20)

While algebraically they are "equivalent", computers are discrete beasts and cannot calculate to exact precision (not even with floating point).  Integer division can definitely introduce some error.  When you do integer division like (10 * 5) / 20, you get a different answer than (10 / 20) * 5.
(10 * 5) / 20
50 / 20
2 (integer division truncates)

(10 / 20) * 5
0 * 5 (integer division truncates)
0

Try to do as much with integer arithmetic as you can, even though you get these strange answers, it's faster and sometimes more accurate working with larger numbers than 32 bit floating point (sometimes you have to do floating point because it's TOO large).  As long as V409 * V400 can never exceed 2,147,483,647
then your integer math will be fine.

unsyndicated

  • Full Member
  • ***
  • Posts: 21
Re: Math not updating
« Reply #2 on: May 26, 2022, 11:44:07 AM »
And changing my formula to what you suggested did the trick.  I would have struggled with that for a long time.

Thank you very much. 

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3761
    • Host Engineering
Re: Math not updating
« Reply #3 on: May 26, 2022, 12:09:41 PM »
Here's a hack to do rounding with integer division.  It works well since you are dividing by a constant.  Rounding is the same as adding a half then truncating. So 12.8 + 0.5 truncated is 13.  So + 0.5.  But that's a floating point number!  We don't like to introduce floats in integer arithmetic.  But remember that fractions can also represent floating point numbers.  So 1/2 or 2/4, or even 10/20 (hint, hint).

So change your MATH sub-expression to the following to ROUND using INTEGER math:

(((V409 * V400) + 10) / 20)

We're basically adding 10 to the numerator, with a denominator of 20, that's the same as adding a half to the result of the sub-expression.  The truncation of THAT gives a nice rounded value to the nearest whole integer!  Rounding of a real value using INTEGER ARITHMETIC ONLY  ;D.

*** EDIT ***
example
V409 = 2
V400 = 38

Integer No rounding
((2 * 38) / 20)
(76 / 20)
3

Algebraically using Real numbers:
((2.0 * 38.0) / 20.0)
(76.0 / 20.0)
3.8

Integer With rounding
(((2 * 38) + 10) / 20)
((76 + 10) / 20)
(86 / 20)
4
« Last Edit: May 26, 2022, 12:32:48 PM by franji1 »