Boost logo

Boost :

Subject: Re: [boost] Interest for a Fixed Point Arithmetics library
From: Paul A. Bristow (pbristow_at_[hidden])
Date: 2017-12-31 15:49:41


> -----Original Message-----
> From: Boost [mailto:boost-bounces_at_[hidden]] On Behalf Of Julien Vernay via Boost
> Sent: 30 December 2017 18:30
> To: boost_at_[hidden]
> Cc: Julien Vernay
> Subject: [boost] Interest for a Fixed Point Arithmetics library
>
> Hello everybody.
> I am working on a project that needs and can use fixed point numbers, so I
> started searching for a library implementing fixed point numbers, but I
> didn't find anything. So I was wondering if people would need fixed point
> arithmetics, and here I am.
>
> The aim of this FixedPointNumber is to fix the point at compile-time using
> template, and having another template argument for the underlying type.
> So we have :
>
> template<typename T, size_t N>
> class FixedPointNumber {
> T value;
> public:
> //interface
> };
>
> The N last bits of value is the decimal part : so to convert a
> FixedPointNumber to an int, we need only to use a bitshift operator (and
> same for construction from int).
> sizeof(FixedPointNumber<T,N>) == sizeof(T) , so there is no overhead in
> this implementation.
> Arithmetic and relational operators will be implemented.
> Multiplication needs one int multiplication and one bit shift.
> Division needs one int division and one bit shift.
> There is only one value for zero.
> Sign can be extracted with Most Significant Bit.
>
> I have already a basic implementation but if it interests more people I
> will do a proper implementation.

An implementation of fixed-point has been written (and is in real-life use) by Chris Kormanyos

see https://github.com/BoostGSoC15/fixed_point/

this uses a specified (fixed at compile-time) fixed-point format. Big formats are possible and it works seamlessly with many
Boost.Math functions, often using constexpr for compile-time computations).

It did not receive much attention when tentatively proposed for Boost.

C++ standards groups are also considering a proposal for an 'elastic' (at run-time) fixed-point library by John Macfarlane

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0554r0.html

I suggest you should study these different approaches (not necessarily mutually exclusive)?

Paul

---
Paul A. Bristow
Prizet Farmhouse
Kendal UK LA8 8AB
+44 (0) 1539 561830

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk