# Handwired JavaScript: Try the Integer Branch Example

> Explore Handwired’s integer branch idea with a local slider. Compare ordinary JavaScript with ReLU arithmetic and see why a fractional input changes the result.

Source: https://toolpantry.app/blog/handwired-javascript-integer-branch

## Try an integer, then try 4.5

The ordinary branch returns 10 when the input is greater than 4, and 12 otherwise. Its arithmetic counterpart builds a gate from two ReLU values, then uses that gate to weight the two outputs.

The two results match at input 5. At 4.5, the ordinary branch returns 10, while the arithmetic returns 11.

The input slider needs JavaScript. The calculation and result table below work as a static explanation.

## How the gate works

ReLU means taking the larger of zero and a number. In this example, subtracting the ReLU of input minus 5 from the ReLU of input minus 4 creates a gate: zero at and below 4, one at and above 5, and a ramp between those points.

At input 4, both ReLU terms are zero, so the gate is zero and the weighted output is 12. At input 5, the terms are one and zero, so the output becomes 10. For integer inputs above 5, both terms rise together and their difference stays one.

## What our arithmetic check found

We compared the arithmetic output with the ordinary branch for every integer from −1,000 through 1,000. All 2,001 inputs matched. We also checked fractional inputs around the threshold:

| Input | Gate | Branch | Arithmetic |
|---|---|---|---|
| 4 | 0 | 12 | 12 |
| 4.25 | 0.25 | 10 | 11.5 |
| 4.5 | 0.5 | 10 | 11 |
| 4.75 | 0.75 | 10 | 10.5 |
| 5 | 1 | 10 | 10 |

There is no integer between 4 and 5, so the ramp is invisible on the intended integer domain. A fractional input reveals it. This is a limit of this construction’s input assumption; it does not demonstrate a bug in Handwired’s compiler.

## What this explains about Handwired

[Handwired’s README](https://github.com/chenglou/handwired) describes manually constructed numerical operations for programs, using matrix operations and ReLU-style gates. Its own examples include a JavaScript backend and WebGPU experiments. The small construction here only illustrates a conditional; it does not compile arbitrary JavaScript into a trained language model.

To try the full project, use [the author’s live demos](https://chenglou.me/handwired/) and read the repository’s setup instructions. We have not measured GPU speed, general-program equivalence or training behavior. A successful arithmetic check cannot establish any of those claims.

## A useful way to experiment

1. Start with a tiny condition whose expected result you can calculate independently.
2. Write down whether the input domain is integers, bounded integers or real numbers.
3. Test the boundary and the values on both sides before adding loops or state.
4. Keep performance measurements separate from correctness checks.

If you are inspecting a result object from your own experiment, the [JSON formatter](https://toolpantry.app/json-formatter) can format it locally. This explainer sends no input to a server.

## Notes

We checked this small arithmetic example with Node.js. It is an independent explainer of a construction in Cheng Lou’s README, not a test of the Handwired compiler or WebGPU backend. No model weights or third-party runtime are downloaded.
