Clamp value with single combinator

This board is to show, discuss and archive useful combinator- and logic-creations.
Smart triggering, counters and sensors, useful circuitry, switching as an art :), computers.
Please provide if possible always a blueprint of your creation.
Tertius
Smart Inserter
Smart Inserter
Posts: 1712
Joined: Fri Mar 19, 2021 5:58 pm
Contact:

Clamp value with single combinator

Post by Tertius »

Didn't see it before elsewhere: clamping some value to an upper limit with a single combinator.

[L]=value to clamp
[M]=upper limit




out.mp4
(336.42 KiB) Downloaded 102 times
mechatimer
Burner Inserter
Burner Inserter
Posts: 8
Joined: Sun Nov 10, 2024 12:53 am
Contact:

Re: Clamp value with single combinator

Post by mechatimer »

Very useful for setting train limits!

For other possible applications, you can even go further and clamp the value between a lower and upper bound:
ClampNumber.png
ClampNumber.png (135.39 KiB) Viewed 380 times
Here, I added [N] as the lower limit. Note that N and M have to have different values, otherwise the 'each' signal will output the sum of both of them.
Tallywort
Inserter
Inserter
Posts: 24
Joined: Thu Mar 25, 2021 11:57 am
Contact:

Re: Clamp value with single combinator

Post by Tallywort »

Wait. How does that even work?
Tertius
Smart Inserter
Smart Inserter
Posts: 1712
Joined: Fri Mar 19, 2021 5:58 pm
Contact:

Re: Clamp value with single combinator

Post by Tertius »

It's kind of a if-then-else emulation.

In general:
You need to understand the EACH wildcard and how a condition with EACH works if combined with AND with another simple condition (one without EACH). If there is a EACH in general, the combinator switches to a slightly different operating mode. It processes every EACH and processes each signal specified for each EACH (from red, or green, or both). It collects all matching signals in a temporary (hidden) list.

If there is a simple condition combined with AND with a condition that contains an EACH, the EACH collects only signals if that simple condition is true.

For each signal matching all these conditions, the signal, its value and its source (red or green) is recorded.
if you have multiple conditions with EACH (optionally with simple conditions combined with AND), and these blocks combined with OR, the temporary list is created from the result of all the individual signal from all EACH that also match their corresponding AND.

Finally, if there is an EACH as output, the temporary list of signals is being output, according to their source (originating from red only, from green only, or from both - then the sum is being output).

If there is a simple signal given as output, for example a simple L, all signals in the temporary list are being counted and summed up. If you give a number as output, for example L=3, the signal count is multiplied by 3 and this becomes the value of L. If you use (input value) instead, L becomes the sum of all values of the signals in the temporary list.

---

Now you know how EACH works and I can explain my combinator.
On green, the number to clamp is in L. On red, the limit is in M.
There are 2 blocks with EACH, each with a EACH condition and a simple condition.

The 1st block results only in something if the number to clamp is less than the limit.
Assume this is the case. In this case, the EACH is processed and operates on every signal on red and green. We have L and M, and the condition is "equal to" L. M is not equal to L (this was checked with the simple condition), but L is equal to L. This is true, so L is being recorded to the temporary list.

The 2nd block results only in something if the number to clamp is greater than or equal to the limit. This is a mutually exclusive condition to the condition above: either the 1st block or the 2nd block results in something in the temporary list.
So in our example this block doesn't result in any temporary list entry.

As output, we have L=input count. Input count means sum of all values in the temporary list, and since there is one 3, the output is finally L=? with ?=whatever L is in the input.


Assume the other case: L is greater then L.
In this case, the 1st each block doesn't result in anything, because the simple condition is false. The temporary list stays empty.

But the simple condition in the 2nd block is now true, so the EACH condition is being processed. It processed all signals on red. We have M on red, and it is being compared as equal to M, and the result is true (M = M). So M is being added to the temporary list.

As output, again the sum is created and returned as L. Since there is M=3 on the output, the sum of 3 is 3 and this value is being output as L.

And that's it.

It's made so one of the EACH will either match the L or the M, but never both.


By the way, this contraption is obsolete as of Factorio 2.1, because we got an ELSE. With this feature, clamping/limiting becomes the direct solution:
07-13-2026, 19-04-29.png
07-13-2026, 19-04-29.png (99.64 KiB) Viewed 43 times
Post Reply

Return to “Combinator Creations”