n255thsOf

Fast 8-bit “percentage” function

This function optimizes its runtime performance by substituting the division by 255 with an approximation using bitshifts.

Nonetheless, its result are as accurate as a floating point division with 64-bit precision.

@safe pure nothrow @nogc
ubyte
n255thsOf
(
const ubyte nPercentage
,
const ubyte value
)

Parameters

nPercentage ubyte

percentage as the number of 255ths (“two hundred fifty-fifths”)

value ubyte

base value (“total”)

Return Value

Type: ubyte

round(value * nPercentage / 255.0)

Meta