Commit Graph
455 Commits
Author SHA1 Message Date
Chip Hogg 2b37bc7b3e Address review feedback 2022-04-19 15:42:43 +00: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 5e365f1905 Delete obsolete test case 2022-04-11 15:16:16 +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
Mateusz Pusz b221dace3f style: clang-format applied to the remaining files 2022-04-02 21:36:42 +02:00
Markus Hofbauer cf7a7ed17a check: run pre-commit on all files 2022-04-02 18:58:23 +02:00
Mateusz Pusz 63f3dd2be6 Merge pull request #344 from JohelEGP/quotient_remainder_theorem
test(static): simplify quotient_remainder_theorem test cases
2022-03-22 10:04:49 +01:00
Johel Ernesto Guerrero Peña feb1a0b860 test(static): simplify quotient_remainder_theorem test cases 2022-03-21 14:29:50 -04:00
Mateusz Pusz 3729a9fe93 Merge pull request #337 from chiphogg/chiphogg/prime-wheel
Use wheel factorization for prime numbers
2022-03-21 10:37:05 +01: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 c339383873 Convert names to standard_case 2022-03-19 16:07:39 +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
Chip Hogg 1e8460d401 Revert "Try upping the basis size further"
This reverts commit 87073856a7.
It didn't fix the problem, and it caused some new ones.  We need a
different approach.
2022-03-19 13:21:47 +00:00
Markus Hofbauer 4c127797da check: run pre-commit on all files 2022-03-18 16:12:21 +01:00
Mateusz PuszandJohel Ernesto Guerrero Peña 6d82ddebbc Update test/unit_test/static/quantity_kind_test.cpp
Co-authored-by: Johel Ernesto Guerrero Peña <johelegp@gmail.com>
2022-03-18 08:21:15 +01:00
Mateusz Pusz 144f6951c0 style: all files refromatted with clang-format 2022-03-17 23:59:48 +01:00
Chip Hogg 87073856a7 Try upping the basis size further
We are well into a regime of diminishing returns, but we'd better start
by seeing if the easy thing works.  Besides, setting this to 7 trips the
step limit in _generating_ the algorithm!
2022-03-12 18:56:26 +00:00
Chip Hogg 73a56115a1 Fix wheel factorization algorithm 2022-03-11 03:42:19 +00:00
Chip Hogg 24b284fcbb Add tests to support claims in comment 2022-03-10 23:41:36 +00: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
Mateusz Pusz 3651e199bf Merge pull request #331 from JohelEGP/llvm15
refactor: update hacks for LLVM 15
2022-02-08 10:40:31 +01:00
Johel Ernesto Guerrero Peña 3f145a25db refactor: update hacks for LLVM 15 2022-02-07 21:07:22 -04: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 dfe3db0b70 Merge branch 'master' into mag-value-t 2022-01-29 02:49:19 +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
Frank Dischner 774dd91686 Add tests for FPS cubic yard 2022-01-23 21:52:23 -06: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
Chip Hogg a8d6c890a5 Move pi_to_the to the test file 2022-01-11 19:12:27 -05:00
Chip Hogg cee4be2b67 Simplify namespaces and canonicalize formatting
The motivation for the `mag` sub-namespace was to distinguish something
like `mag::product_t<...>` from `dim::product_t<...>`, based on the
idioms of Aurora Units.  However, we have no need for a `product_t` type
trait, since we can just use `operator*()`, so we can eliminate this
sub-namespace.
2022-01-10 14:28:02 -05:00
Chip Hogg c64f392dc3 Remove some extra spaces 2022-01-10 14:23:44 -05:00
Chip Hogg f783d7f274 Handle exp explicitly in as_magnitude()
The new test actually passed without modifying the code.  However, that
might be implementation-dependent (presumably based on canonicalization
of the `ratio` template parameter), so I wanted a more obviously correct
implementation.
2022-01-08 19:34:42 -05:00
Chip Hogg 409aaf4636 Generalize inverse() to pow<...>() 2022-01-08 19:34:21 -05:00