[PATCH v5 3/5] units: add PERCENT and BASIS_POINTS macros

Jonathan Santos posted 5 patches 1 month, 3 weeks ago
There is a newer version of this series
[PATCH v5 3/5] units: add PERCENT and BASIS_POINTS macros
Posted by Jonathan Santos 1 month, 3 weeks ago
Add macros for percentage related units, with basis points defined as
1/100th of a percent. Basis points are commonly used in finance and
engineering to express small percentage changes with precision.

Signed-off-by: Jonathan Santos <Jonathan.Santos@analog.com>
---
v5 Changes:
* Included PERCENT macro along with BASIS_POINTS.
* Adjusted commit description and comment in the code to add more context and
  examples.

v4 Changes:
* New patch.
---
 include/linux/units.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/include/linux/units.h b/include/linux/units.h
index 00e15de33eca..9c2fbcf04c81 100644
--- a/include/linux/units.h
+++ b/include/linux/units.h
@@ -21,6 +21,20 @@
 #define PICO	1000000000000ULL
 #define FEMTO	1000000000000000ULL
 
+/*
+ * Percentage and basis point units
+ *
+ * Basis points are 1/100th of a percent (1/100), commonly used in finance,
+ * engineering or other applications that require precise percentage
+ * calculations.
+ *
+ * Examples:
+ *   100% = 10000 basis points = BASIS_POINTS
+ *   1%   = 100 basis points   = PERCENT
+ */
+#define PERCENT		100UL
+#define BASIS_POINTS	10000UL
+
 #define NANOHZ_PER_HZ		1000000000UL
 #define MICROHZ_PER_HZ		1000000UL
 #define MILLIHZ_PER_HZ		1000UL
-- 
2.34.1
Re: [PATCH v5 3/5] units: add PERCENT and BASIS_POINTS macros
Posted by Jonathan Cameron 1 month, 2 weeks ago
On Wed, 17 Dec 2025 02:52:45 -0300
Jonathan Santos <Jonathan.Santos@analog.com> wrote:

> Add macros for percentage related units, with basis points defined as
> 1/100th of a percent. Basis points are commonly used in finance and
> engineering to express small percentage changes with precision.
> 
> Signed-off-by: Jonathan Santos <Jonathan.Santos@analog.com>
> ---
> v5 Changes:
> * Included PERCENT macro along with BASIS_POINTS.
> * Adjusted commit description and comment in the code to add more context and
>   examples.
> 
> v4 Changes:
> * New patch.
> ---
>  include/linux/units.h | 14 ++++++++++++++
>  1 file changed, 14 insertions(+)
> 
> diff --git a/include/linux/units.h b/include/linux/units.h
> index 00e15de33eca..9c2fbcf04c81 100644
> --- a/include/linux/units.h
> +++ b/include/linux/units.h
> @@ -21,6 +21,20 @@
>  #define PICO	1000000000000ULL
>  #define FEMTO	1000000000000000ULL
>  
> +/*
> + * Percentage and basis point units
> + *
> + * Basis points are 1/100th of a percent (1/100), commonly used in finance,
> + * engineering or other applications that require precise percentage
> + * calculations.
> + *
> + * Examples:
> + *   100% = 10000 basis points = BASIS_POINTS
> + *   1%   = 100 basis points   = PERCENT

I don't understand the final equality in these examples.
The top line is as it says 10000 basis points but you have it equal
to BASIS_POINTS?  

> + */
> +#define PERCENT		100UL
> +#define BASIS_POINTS	10000UL
> +
>  #define NANOHZ_PER_HZ		1000000000UL
>  #define MICROHZ_PER_HZ		1000000UL
>  #define MILLIHZ_PER_HZ		1000UL
Re: [PATCH v5 3/5] units: add PERCENT and BASIS_POINTS macros
Posted by Andy Shevchenko 1 month, 2 weeks ago
On Sat, Dec 27, 2025 at 5:55 PM Jonathan Cameron <jic23@kernel.org> wrote:
> On Wed, 17 Dec 2025 02:52:45 -0300
> Jonathan Santos <Jonathan.Santos@analog.com> wrote:

...

> > +/*
> > + * Percentage and basis point units
> > + *
> > + * Basis points are 1/100th of a percent (1/100), commonly used in finance,
> > + * engineering or other applications that require precise percentage
> > + * calculations.
> > + *
> > + * Examples:
> > + *   100% = 10000 basis points = BASIS_POINTS
> > + *   1%   = 100 basis points   = PERCENT
>
> I don't understand the final equality in these examples.
> The top line is as it says 10000 basis points but you have it equal
> to BASIS_POINTS?

Also there are pp (percentage points) and others like per mille

https://en.wikipedia.org/wiki/Percentage_point (see "Related units" section).

> > + */
> > +#define PERCENT              100UL
> > +#define BASIS_POINTS 10000UL


-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH v5 3/5] units: add PERCENT and BASIS_POINTS macros
Posted by Jonathan Santos 1 month ago
On 12/27, Andy Shevchenko wrote:
> On Sat, Dec 27, 2025 at 5:55 PM Jonathan Cameron <jic23@kernel.org> wrote:
> > On Wed, 17 Dec 2025 02:52:45 -0300
> > Jonathan Santos <Jonathan.Santos@analog.com> wrote:
> 
> ...
> 
> > > +/*
> > > + * Percentage and basis point units
> > > + *
> > > + * Basis points are 1/100th of a percent (1/100), commonly used in finance,
> > > + * engineering or other applications that require precise percentage
> > > + * calculations.
> > > + *
> > > + * Examples:
> > > + *   100% = 10000 basis points = BASIS_POINTS
> > > + *   1%   = 100 basis points   = PERCENT
> >
> > I don't understand the final equality in these examples.
> > The top line is as it says 10000 basis points but you have it equal
> > to BASIS_POINTS?
> 
> Also there are pp (percentage points) and others like per mille
> 
> https://en.wikipedia.org/wiki/Percentage_point (see "Related units" section).
> 

Hi, Andy. Thanks for the suggestions,but I am still questioning how
the percentage points would be implemented, since it is defined as the
difference between two percentages and we cannot use floating points.

Also, should we keep this approach of unit conversion?
Example:
value / PERCENT -> converts a value from percent to ratio
value * PERCENT -> converts a value from ratio to percent

Just to make sure it is consistent with the rest.

> > > + */
> > > +#define PERCENT              100UL
> > > +#define BASIS_POINTS 10000UL
> 
> 
> -- 
> With Best Regards,
> Andy Shevchenko
Re: [PATCH v5 3/5] units: add PERCENT and BASIS_POINTS macros
Posted by Andy Shevchenko 3 weeks, 6 days ago
On Tue, Jan 13, 2026 at 5:32 AM Jonathan Santos <jonath4nns@gmail.com> wrote:
> On 12/27, Andy Shevchenko wrote:
> > On Sat, Dec 27, 2025 at 5:55 PM Jonathan Cameron <jic23@kernel.org> wrote:
> > > On Wed, 17 Dec 2025 02:52:45 -0300
> > > Jonathan Santos <Jonathan.Santos@analog.com> wrote:

...

> > > > +/*
> > > > + * Percentage and basis point units
> > > > + *
> > > > + * Basis points are 1/100th of a percent (1/100), commonly used in finance,
> > > > + * engineering or other applications that require precise percentage
> > > > + * calculations.
> > > > + *
> > > > + * Examples:
> > > > + *   100% = 10000 basis points = BASIS_POINTS
> > > > + *   1%   = 100 basis points   = PERCENT
> > >
> > > I don't understand the final equality in these examples.
> > > The top line is as it says 10000 basis points but you have it equal
> > > to BASIS_POINTS?
> >
> > Also there are pp (percentage points) and others like per mille
> >
> > https://en.wikipedia.org/wiki/Percentage_point (see "Related units" section).
>
> Hi, Andy. Thanks for the suggestions,but I am still questioning how
> the percentage points would be implemented, since it is defined as the
> difference between two percentages and we cannot use floating points.

My comment was more about the prefix (PER) and the naming (not
BASIS_POINTS, but something from PER*), and for the comprehension I
would rather see all three defined (pp is _not_ part of these as you
mentioned its semantics is different).

> Also, should we keep this approach of unit conversion?
> Example:
> value / PERCENT -> converts a value from percent to ratio
> value * PERCENT -> converts a value from ratio to percent
>
> Just to make sure it is consistent with the rest.

Sounds good at the first glance.

> > > > + */
> > > > +#define PERCENT              100UL
> > > > +#define BASIS_POINTS 10000UL

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH v5 3/5] units: add PERCENT and BASIS_POINTS macros
Posted by Andy Shevchenko 1 month, 2 weeks ago
On Sat, Dec 27, 2025 at 6:52 PM Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
> On Sat, Dec 27, 2025 at 5:55 PM Jonathan Cameron <jic23@kernel.org> wrote:
> > On Wed, 17 Dec 2025 02:52:45 -0300
> > Jonathan Santos <Jonathan.Santos@analog.com> wrote:

...

> Also there are pp (percentage points) and others like per mille
>
> https://en.wikipedia.org/wiki/Percentage_point (see "Related units" section).

And given most of them are in the PER* namespace I would use those
names instead of variants.


-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH v5 3/5] units: add PERCENT and BASIS_POINTS macros
Posted by Andy Shevchenko 1 month, 2 weeks ago
On Sat, Dec 27, 2025 at 5:55 PM Jonathan Cameron <jic23@kernel.org> wrote:
> On Wed, 17 Dec 2025 02:52:45 -0300
> Jonathan Santos <Jonathan.Santos@analog.com> wrote:

...

> > +/*
> > + * Percentage and basis point units
> > + *
> > + * Basis points are 1/100th of a percent (1/100), commonly used in finance,
> > + * engineering or other applications that require precise percentage
> > + * calculations.
> > + *
> > + * Examples:
> > + *   100% = 10000 basis points = BASIS_POINTS
> > + *   1%   = 100 basis points   = PERCENT
>
> I don't understand the final equality in these examples.
> The top line is as it says 10000 basis points but you have it equal
> to BASIS_POINTS?
>
> > + */
> > +#define PERCENT              100UL

Be careful with UL. What would be 10% from -555 (minus five hundreds
fifty five)?

-- 
With Best Regards,
Andy Shevchenko