Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check that every magnitude fits in its target type #211

Merged
merged 1 commit into from
Dec 15, 2023

Conversation

chiphogg
Copy link
Contributor

get_value<T>(m) converts a magnitude m to its representation in type
T. It produces a hard compiler error if the value can't fit. Its
relative get_value_result<T>(m) always returns a result, but produces
an error code if the value can't be represented.

Previously, there was a hole in this system. We assumed that each
magnitude could be represented in std::uintmax_t. Of course, if it
couldn't, get_value would fail to compile as it should. But
get_value_result would also fail to compile, instead of producing an
error condition.

To fix this, we need to move our checking utilities further up in the
file, so that all of our utilities can use them. product, int_pow,
base_power_value --- they all need to check their computations at
every step. When we do this, we're able to gracefully detect the
non-representability of huge numbers even for the biggest types.

`get_value<T>(m)` converts a magnitude `m` to its representation in type
`T`.  It produces a hard compiler error if the value can't fit.  Its
relative `get_value_result<T>(m)` always returns a result, but produces
an error code if the value can't be represented.

Previously, there was a hole in this system.  We assumed that each
magnitude could be represented in `std::uintmax_t`.  Of course, if it
couldn't, `get_value` would fail to compile as it should.  But
`get_value_result` would _also_ fail to compile, instead of producing an
error condition.

To fix this, we need to move our checking utilities further up in the
file, so that all of our utilities can use them.  `product`, `int_pow`,
`base_power_value` --- they all need to _check their computations_ at
every step.  When we do this, we're able to gracefully detect the
non-representability of huge numbers even for the biggest types.
@chiphogg chiphogg added the release notes: 🐛 lib (bugfix) PR fixing a defect in the library code label Dec 14, 2023
@chiphogg chiphogg requested a review from geoffviola December 14, 2023 04:03
Copy link
Contributor

@geoffviola geoffviola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checks look useful. Tests look good.

@chiphogg chiphogg merged commit a831130 into main Dec 15, 2023
9 checks passed
@chiphogg chiphogg deleted the chiphogg/check-every-mag#110 branch December 15, 2023 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release notes: 🐛 lib (bugfix) PR fixing a defect in the library code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants