Commit Graph
188 Commits
Author SHA1 Message Date
Mateusz Pusz bd5a476bab fix: pow<> requires a ratio as a template argument
Thank you @Nostracodus and @TartanLlama!
2022-05-11 22:52:03 +02:00
Chip Hogg 2b37bc7b3e Address review feedback 2022-04-19 15:42:43 +00:00
Chip Hogg 94fe48dd64 Add <compare> to satisfy clang 2022-04-12 00:44:53 +00:00
Chip Hogg c805a9390f Implement suggestion 2022-04-12 00:40:42 +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 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 5afe7766e9 Use Magnitude implementation for base_units_ratio
This resolves a TODO and lets us use arbitrary exponent denominators.

I also attempt to clarify the semantics.  This is based on my best
effort of understanding pre-existing concepts in the library, so I hope
I got it right!
2022-04-11 15:16:16 +00:00
Chip Hogg 5c0eb0667c Remove inverse_if_negative()
Fixes #332.
2022-04-11 15:16:16 +00:00
Chip Hogg 85d6e35d79 Loosen requirements on Rep for Magnitude
This is necessary to be able to use Magnitudes in Units with a Rep that
is not `std::is_arithmetic`.
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 bc98254e7a Support - and <=> in ratio 2022-04-11 15:16:15 +00:00
Chip Hogg 44cccfc743 Address review comments 2022-04-09 15:39:29 +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 f77a92ca04 Reproduce requires clause
clang appears to care about this.
2022-04-06 00:36:18 +00:00
Chip Hogg 746aa34fcc Tweak unwise variable names 2022-04-06 00:18:52 +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
Johel Ernesto Guerrero Peña 9b19dcf193 refactor(bits): remove unused header in prime.h 2022-03-24 16:57:12 -04:00
Johel Ernesto Guerrero Peña 8491e94c94 refactor(bits): remove unused headers in external/type_list.h 2022-03-23 18:03:44 -04:00
Mateusz Pusz 6218a709ae refactor: first_factor_maybe() replaced with get_first_of() 2022-03-21 18:13:47 +01:00
Mateusz Pusz ca283b94f6 refactor: find_first_factor() refactored 2022-03-21 11:16:50 +01: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 b589ba8d86 Omit redundant computation
This stems from an earlier mistake where I was using primes in the first
wheel, rather than coprimes-to-the-basis.  1 is not prime, so we used to
need to handle it separately (in an implementation which was, to be
clear, wrong).  It _is_ coprime, so now we get it for free!
2022-03-19 22:10:01 +00:00
Mateusz Pusz e1f7266b51 refactor: <algorithm.h> header added
Should be replaced with `<algorithm>` when C++20 modules will be used. Right now this header is too expensive to compile.
2022-03-19 20:07:55 +01:00
Chip Hogg 6872117bae Replace reduce with bespoke implementation
If _this_ isn't `constexpr` compatible, I'm going to propose removing
support for the MacOS clang build.
2022-03-19 17:35:20 +00:00
Chip Hogg f495ad9e75 Replace accumulate with reduce
Perhaps this will also satisfy Apple's Clang 13?  Since `reduce` is
newer, it may be more likely to be `constexpr` compatible.
2022-03-19 17:27:38 +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 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 Pusz b58b696e92 style: clang-format formatting improved with additional parenthesis 2022-03-18 09:10:27 +01:00
Mateusz PuszandJohel Ernesto Guerrero Peña 6531d188d1 Update src/core/include/units/quantity.h
Co-authored-by: Johel Ernesto Guerrero Peña <johelegp@gmail.com>
2022-03-18 08:20:43 +01:00
Mateusz PuszandJohel Ernesto Guerrero Peña f4fbac86b4 Update src/core/include/units/quantity.h
Co-authored-by: Johel Ernesto Guerrero Peña <johelegp@gmail.com>
2022-03-18 08:20:36 +01:00
Mateusz PuszandJohel Ernesto Guerrero Peña 03290d74a2 Update src/core/include/units/quantity.h
Co-authored-by: Johel Ernesto Guerrero Peña <johelegp@gmail.com>
2022-03-18 08:20:27 +01:00
Mateusz PuszandJohel Ernesto Guerrero Peña 6e59e7a927 Update src/core/include/units/quantity.h
Co-authored-by: Johel Ernesto Guerrero Peña <johelegp@gmail.com>
2022-03-18 08:20:08 +01:00
Mateusz PuszandJohel Ernesto Guerrero Peña c6f35b0231 Update src/core/include/units/quantity_cast.h
Co-authored-by: Johel Ernesto Guerrero Peña <johelegp@gmail.com>
2022-03-18 08:19:40 +01:00
Mateusz Pusz 144f6951c0 style: all files refromatted with clang-format 2022-03-17 23:59:48 +01:00
Mateusz Pusz cc90985958 fix: Clang-12 compilation fixed 2022-03-15 13:14:15 +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 a719a8b912 Try upping the basis size 2022-03-11 03:57:21 +00:00
Chip Hogg 70640a1017 Remove constexpr-incompatible assert() 2022-03-11 03:56:57 +00:00
Chip Hogg 59d9cd1407 static_cast for first factor 2022-03-11 03:50:08 +00:00
Chip Hogg bfa8db6139 Use std::accumulate 2022-03-11 03:48:57 +00:00
Chip Hogg 73a56115a1 Fix wheel factorization algorithm 2022-03-11 03:42:19 +00:00
Chip Hogg 6c73947fe0 Satisfy complaint
This line shouldn't _actually_ ever be reachable, but I can't fault the
compiler for not figuring that out.
2022-03-10 23:50:33 +00:00
Chip Hogg c8a44adee2 Add missing header for std::integral 2022-03-10 23:41:47 +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