Commit Graph
223 Commits
Author SHA1 Message Date
Mateusz Pusz d0eceefe80 refactor: dead ratio code removed 2022-09-06 23:49:16 +02:00
Mateusz Pusz a39a49c147 test: VS2019 compilation fixed 2022-09-02 10:21:58 +02:00
Mateusz Pusz 396d39c35b test: AlmostEqualsMatcher compilation fixed 2022-09-01 17:18:02 +02:00
Mateusz Pusz a95cb53f73 test: unit tests for trigonometric functions added 2022-09-01 16:25:33 +02:00
Mateusz Pusz 707b39f0c4 build: catch2 updated to 3.1.0 2022-09-01 16:19:51 +02:00
Mateusz Pusz c1d23b41c1 refactor: pi-based magnitude renamed to mag_pi 2022-09-01 10:47:36 +02:00
Chip Hogg df290a74f0 Remove shadowing declaration of pi 2022-08-11 15:56:10 +00:00
Mateusz Pusz a1e1b70b92 test: Unit tests for 3-argument hypot added 2022-08-03 13:35:18 +02:00
Mateusz Pusz 4c8b3ce290 test: Commented out ceil unit test enabled as it works correctly now 2022-08-03 13:12:24 +02:00
Mateusz Pusz b252da0fe1 test: hypot unit tests added 2022-08-03 13:10:59 +02:00
Chip Hogg ea83af8aa9 Rename as_magnitude<...>() to mag<...>()
This is easier to write and to read.
2022-07-29 01:46:06 +00:00
Chip Hogg 7fd6913b73 Replace exp with 0 everywhere, and remove it
This lets us remove a ton of special-casing throughout the codebase, and
just generally makes things a lot simpler.

We also remove the ability to take rational powers of `ratio`, including
`sqrt` and `cbrt` helpers, because these are intrinsically ill-defined.

Fixes #369.
2022-07-20 00:52:37 +00:00
Chip Hogg 407a13c48f Add extract_power_of_10(Magnitude) utility
This will help with pretty-printing magnitudes by extracting a
reasonable power of 10.
2022-07-07 16:10:34 +00:00
Chip Hogg ef9e6522ca Merge branch 'master' into chiphogg/switch-to-mag 2022-06-15 20:50:30 +00:00
Mateusz Pusz 46b3781743 Revert "refactor: 💥 ! " ⋅ " replaced with "⋅" in derived unit symbols"
This reverts commit db09a065ab.
2022-05-24 14:20:17 +02:00
Mateusz Pusz 38fb9911a9 refactor: 💥 ! " ⋅ " replaced with "⋅" in derived unit symbols 2022-05-24 12:56:14 +02:00
Mateusz Pusz db09a065ab refactor: 💥 ! " ⋅ " replaced with "⋅" in derived unit symbols 2022-05-24 12:48:07 +02:00
Chip Hogg 59fa420272 Fix remaining builds/tests 2022-05-12 16:52:05 +00:00
Chip Hogg 5fb468697b Merge branch 'master' into chiphogg/switch-to-mag 2022-05-12 16:12:01 +00:00
Mateusz Pusz 5300caf17b refactor: 💥 ! derived_deduced_unit renamed to derived_scaled_unit
Naming is hard ;-)
2022-05-11 12:34:52 +02:00
Mateusz Pusz f5d21a0711 refactor: 💥 ! derived_unit renamed to derived_deduced_unit 2022-05-11 11:23:18 +02:00
Mateusz Pusz a0c6252567 refactor: mi(naut) renamed to nmi
Relates to #357
2022-04-19 18:47:48 +02:00
Chip Hogg 78bcb3b019 Remove troublesome ::ratio members
For some reason, MSVC seems to want to instantiate these, even though
nobody ever asks for them (as evidenced by the fact that the builds
passed on other architectures).
2022-04-12 00:35:17 +00:00
Chip Hogg d19b2803ce Migrate units from ratio to Magnitude
This commit is huge, but hopefully the cognitive load is not too bad.
The bulk of this commit is just some fairly mechanical updates from
`ratio` to `Magnitude`.  Other things to call out:

- `UnitRatio` goes away.  We don't need this concept, because Magnitude
  can't even _represent_ anything that doesn't satisfy it.

- I commented out some formatting test cases where the precise
  expression changes, but the number is completely equivalent.  We will
  need to decide how we want to handle Magnitude formatting as a
  separate, follow-on task.  But at least Magnitude gives us all the
  tools we'll need to do so!
2022-04-11 15:16:16 +00:00
Chip Hogg af8eec1102 Implement "common Magnitude" of two Magnitudes 2022-04-11 15:16:15 +00:00
Chip Hogg 2c31bf83ec Clean up 2022-04-06 02:01:23 +00:00
Chip Hogg b616e58216 Give up on implicit conversion
I don't understand what MSVC is complaining about, and it may end up
being easier to just make the conversion explicit.
2022-04-06 01:40:17 +00:00
Chip Hogg 0f776b5b5b Remove incorrectly included test
We had removed this upstream, but apparently it snuck back in from a merge/rebase conflict.
2022-04-06 00:31:18 +00:00
Chip Hogg d7681e188e Support seamless interop between ratio and rational Magnitude
We provide two new functions, `numerator(m)` and `denominator(m)`, for a
Magnitude `m`.  They fulfill the following conditions:

1. `numerator(m)` and `denominator(m)` are always integer Magnitudes.
2. If `m` is rational, then `m == numerator(m) / denominator(m)`.

If `m` is _not_ rational, then the numerator and denominator are not
especially meaningful (there is no uniquely defined "leftover irrational
part").  However, we choose a convention that matches how humans would
write a mixed number.  For example, sqrt(27/16) would have a numerator
of 3, denominator of 4, and a "leftover part" of sqrt(3), matching the
"human" way of writing this as [(3 * sqrt(3)) / 4].  This has no use
yet, but it may later be useful in printing the Magnitude of an
anonymous Unit for end users.

To further reduce friction for the upcoming migration, we provide an
implicit conversion from a Magnitude to a `ratio`.  We restrict this
operation to rational Magnitudes, and guard this with a `static_assert`.
2022-04-05 23:03:44 +00:00
Markus Hofbauer cf7a7ed17a check: run pre-commit on all files 2022-04-02 18:58:23 +02:00
Chip Hogg 438feb3001 Remove offending unit test
Apparently, the constexpr depth which clang and MSVC can handle is too
shallow for me to write a unit test that works on all supported
compilers.
2022-03-19 17:05:55 +00:00
Chip Hogg 0f80c10104 Try "gentler" test case
I verified that we hit GCC 10's constexpr limit with
`wheel_factorizer<1>`, but pass with `wheel_factorizer<4>`.  I hope this
number is enough smaller than the square root of the previous value that
the other compilers will be able to handle it.  If not: we'll go lower.
2022-03-19 16:58:43 +00:00
Chip Hogg 48b6280486 Merge branch 'master' into chiphogg/prime-wheel 2022-03-19 16:01:32 +00:00
Chip Hogg 166dd1e944 Work around numbers with very large first factors
We introduce the `known_first_factor` variable template.
2022-03-19 15:56:12 +00:00
Chip Hogg a99e5f9032 Switch tests to use top-level, anonymous namespace 2022-03-19 15:54:29 +00:00
Chip Hogg 28c4fe3c08 Run clang-format-15 on changed files 2022-03-19 14:29:11 +00:00
Mateusz Pusz 144f6951c0 style: all files refromatted with clang-format 2022-03-17 23:59:48 +01:00
Chip Hogg 04b80f0827 Use wheel factorization for prime numbers
Certain existing units in the library require very large prime
numbers---so large, in fact, that our naive trial division hits the
_iteration limit_ for `constexpr` loops.  We don't want to force users
to provide a compiler option override, so we'd better find another way.

The solution is to use the "wheel factorization" algorithm:
https://en.wikipedia.org/wiki/Wheel_factorization

This lets us skip most composite numbers in our trial division.  The
implementation presented here is configurable in terms of the size of
the "basis" of primes we use.  Bigger bases let us skip more primes, but
at the cost of storing more numbers.  Fortunately, it turns out that N=3
was good enough for our purposes.
2022-03-10 23:29:02 +00:00
Chip Hogg 7cda52af55 Merge branch 'master' into chiphogg/intmax 2022-03-01 02:30:13 +00:00
Mateusz Pusz 87ab85625b The latest Conan generates Catch2 CMake target 2022-02-28 18:27:03 +01:00
Chip Hogg 632f2785a9 Clarify section header 2022-02-24 01:48:02 +00:00
Chip Hogg 240bdd659c Use intmax for base of integral powers
I used `int` before, because it was simple, and I thought _surely_ it
would be enough.  I mean, who's going to make a unit whose magnitude has
a prime factor bigger than 32 bits?

This brings us to the "Dalton", a unit whose ratio-magnitude numerator
is 16605390666050.  The prime factorization of 16605390666050 is
(2 * 5 * 5 * 53 * 6266185157), and this last number is bigger than 2^31
by a factor of 3 or so.

Fortunately, we should have done this from the beginning anyway, because
otherwise there would be numbers we could represent in `ratio` which we
couldn't represent in `Magnitude`, and this should never be the case.
2022-02-24 01:17:27 +00:00
Chip Hogg 7e4ab4206f Remove .value; provide free function only
This is a cleaner interface.

I also checked all of the commented-out test cases.
2022-02-06 21:53:05 +00:00
Chip Hogg b36bc2b582 Turn get_value into free function 2022-01-29 02:40:51 +00:00
Chip Hogg d6eb25f07a Attempt to satisfy MSVC 14
```
test\unit_test\runtime\magnitude_test.cpp(143): error C2672:
'units::check_same_type_and_value': no matching overloaded function
found
```

Maybe it's confused by accessing the static member variable template
using dot-notation on an _instance_?  If so, let's see how it likes the
member-function syntax.
2022-01-28 22:42:23 +00:00
Chip Hogg 2384276ae0 Only test pi if float is smaller than long double 2022-01-28 20:44:29 +00:00
Chip Hogg 243238b3fa Add suggested tests 2022-01-28 20:41:08 +00:00
Chip Hogg a6b6afe438 Add value and categorization helpers for Magnitude
For each Magnitude `m`, we now support:

- `is_rational(m)` tells whether it represents a rational number.
- `is_integral(m)` tells whether it represents an integer (and
  `is_integral(m)` implies `is_rational(m)`).
- `m.value<T>` represents the value of `m` in the type `T`.

If `T` is integral, we only support `m.value<T>` when `is_integral(m)`.

We also perform all intermediate computations in the widest type of the
same category (floating point, signed, or unsigned).  This means we can,
for example, give first-class support to embedded users who may have
hardware support only for `float`: we can ensure they get the most
accurate `float` value we can compute, without burdening them with a
dependency on `long double` in their runtime code.

We are not yet ready to replace `ratio` as the definition of unit
magnitudes, but we're a step closer.  The next step will be to decompose
a Magnitude into numerator, denominator, and irrational parts, giving us
full bidirectional convertibility with existing `ratio` instances.  Then
we can migrate over in a controlled fashion.
2022-01-28 19:06:26 +00:00
Chip Hogg a306472a6b Reuse callables in pairwise_all tests 2022-01-11 19:44:10 -05:00
Chip Hogg 08818ac6a8 Make pow()'s argument a simple (non-template) parameter 2022-01-11 19:19:25 -05:00