The Signal State

The Signal State

View Stats:
Bug: Attenuator has incorrect rounding
Hello,

The attenuator module works inconsistently when the result has a non-zero remainder. For instance:

For 50% of 85 it outputs 43, but for 20% of 56 it shows 11. I would assume that the code behaves as "rounding" like this:

```
return roundf((float) percents * value / 100.0f);
```

Where "percents" and "value" both have integer type. But instead it should be "floor", which can be achieved with integer division:

```
return (percents * value) / 100;
```
< >
Showing 1-4 of 4 comments
Nov 16, 2024 @ 10:21pm 
thanks, we'll take a closer look.
I'd like to see more examples, but what you described matches normal rounding rules. 0.5×85 = 42.5 which should round up to 43. 0.2×56 = 11.2 which should round down to 11.

Keep in mind, too, that some programming languages will alternate rounding 0.5 up or down depending on the party of the whole number portion. They do this to avoid rounding bias. In statistics, if you always round 0.5 up, it can slightly skew the average a bit higher. Rather than manually checking and alternating every time you encounter 0.5, having a roughly 50-50 chance of rounding up or down does it for you.

Maybe tonight after I get off work I can play with some random data to see if I can spot the pattern.
Ok, so it rounds to the nearest integer, and x.5 always rounds away from 0.
The 0.5 rounding is a bit odd but once you know how it functions you can work with it. It hasn't stopped me solving any puzzles.
< >
Showing 1-4 of 4 comments
Per page: 1530 50