Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/morwenn/static_math

Compile time mathematic functions for C++14
https://github.com/morwenn/static_math

Last synced: about 1 month ago
JSON representation

Compile time mathematic functions for C++14

Awesome Lists containing this project

README

        

[![License](http://img.shields.io/:license-mit-blue.svg)](http://doge.mit-license.org)
[![Build Status](https://travis-ci.org/Morwenn/static_math.svg?branch=master)](https://travis-ci.org/Morwenn/static_math)

`static_math` is a C++ library dedicated to compile time mathematics.

## Compatibility

`static_math` heavily relies on C++ features from the C++14 standard - most notably
extended `constexpr` -, therefore only somewhat recent compilers should be able to
compile it. From the latest tests (no as good as they could be), it should work with
at least the following compilers or more recent versions of them:
* g++5
* clang++4
* MSVC++ 19.12 (corresponding to VS 2017 15.5)

It should also work with older versions of VS 2017 minus a single feature (integral
constants) which has been deactivated for these versions.

## Principle

`static_math` makes a heavy use of the C++14 extended `constexpr` to reimplement
a number of existing mathematical functions and some new ones. It also provides
a range of classes (`rational`, `complex`, etc...) whose functions can be used
at compile time too. The library also contains some common mathematical constants.

The library also offers compile-time mathematics at the template parameter level
thanks to it [`constant`](https://github.com/Morwenn/static_math/wiki/Integral-constants)
class, which is a bit like `std::integral_constant` but with an explicit math
support: for example `constant{} + constant{}` will return an
instance of `constant`. One could also write `5_c + 3_c` thanks to the
provided user-defined literals.

## Design choices

### Compile time library

`static_math` has only been made to be usable at compile time and shall not be
used at runtime. It's still possible to use it at runtime though, but it's not
well advised.

Since the library is only meant to be used at compile time, some design choices
have been made to improve usability, convenience or safety over performance. For
example, the rational numbers are simplified after each operation, which could
badly alter the performance in a runtime context, but which greatly lessens the
risk of overflow in rational operations.

### Intuitive feel

One of `static_math`'s aims is to be easy to use. The functions are implemented
so that they can be used in a straightforward way. This implies that the user will
not have to deal with complex metaprogramming tricks - even though some are used in
the implementation.

This intuitive feel is the one reason why `static_math` needs a recent compiler
since the library will need a heavy support for `constexpr`, `std::initializer_list`
and variadic templates in order to work fine.

Some classes (*e.g.* `rational` and `complex`) use public member variables instead
of getters. This simplifies the access to the internals and also ensures that they
cannot be modified after construction.

### Flexibility

Some classes such as `rational` or `complex` make a heavy use of some type traits
facilities such as `std::common_type` in order to offer some flexibility. Many
functions accept different number types and return and instance of the common type
of the arguments::

```cpp
// Two different types used to represent numbers
int a = 5;
long int b = 100000;

// The bigger of them is used as the output type
auto c = gcd(a, b); // type is long int
```

## Documentation

You can find some documentation in the [associated wiki](https://github.com/Morwenn/static_math/wiki).
You can also generate some anemic documentation thanks to the Doxygen comments in
the code. If you want to know more about the details, reading the source code might
still be an interesting solution: the code should be pretty straightforward and
readable enough. Never hesiste to open a documentation issue if you ever feel that
some parts of the documentation are not complete enough or need more examples.