Skip to content

Commit

Permalink
Demote require to warning for min/max multiple check when using preci…
Browse files Browse the repository at this point in the history
…sion
  • Loading branch information
tsaubergine committed Mar 21, 2023
1 parent 404898c commit 1135df3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
16 changes: 13 additions & 3 deletions src/codecs2/field_codec_default.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,22 @@ namespace dccl

// allowable epsilon for min / max to diverge from nearest quantile
const double min_max_eps = 1e-10;

bool min_multiple_of_res = std::abs(quantize(min(), resolution()) - min()) < min_max_eps;
bool max_multiple_of_res = std::abs(quantize(max(), resolution()) - max()) < min_max_eps;
if(FieldCodecBase::dccl_field_options().has_resolution())
{
// ensure that max and min are multiples of the resolution chosen
FieldCodecBase::require(std::abs(quantize(min(), resolution()) - min()) < min_max_eps, "(dccl.field).min must be an exact multiple of (dccl.field).resolution");
FieldCodecBase::require(std::abs(quantize(max(), resolution()) - max()) < min_max_eps, "(dccl.field).max must be an exact multiple of (dccl.field).resolution");
FieldCodecBase::require(min_multiple_of_res, "(dccl.field).min must be an exact multiple of (dccl.field).resolution");
FieldCodecBase::require(max_multiple_of_res, "(dccl.field).max must be an exact multiple of (dccl.field).resolution");
}
else
{
auto res = resolution();
// this was previously allowed so we will only give a warning not throw an exception
if(!min_multiple_of_res)
dccl::dlog.is(dccl::logger::WARN, dccl::logger::GENERAL) && dccl::dlog << "Warning: (dccl.field).min should be an exact multiple of 10^(-(dccl.field).precision), i.e. " << res << ": " << this->this_field()->DebugString() << std::endl;
if(!max_multiple_of_res)
dccl::dlog.is(dccl::logger::WARN, dccl::logger::GENERAL) && dccl::dlog << "Warning: (dccl.field).max should be an exact multiple of 10^(-(dccl.field).precision), i.e. " << res << ": " << this->this_field()->DebugString() << std::endl;
}

// ensure value fits into double
Expand Down
2 changes: 1 addition & 1 deletion src/test/dccl_resolution/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ int main(int argc, char* argv[])
msg_in.set_u3(10.2);
msg_in.set_u4(5.6);
msg_in.set_u5(1.95);
msg_in.set_u6(100);
msg_in.set_u6(25500);

std::string encoded;
codec.encode(&encoded, msg_in);
Expand Down
2 changes: 1 addition & 1 deletion src/test/dccl_resolution/test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ message NumericMsg
// old precision field did not require min and max to be a multiple of resolution
required int32 u6 = 8 [
(dccl.field).min = 0,
(dccl.field).max = 16383,
(dccl.field).max = 25599,
(dccl.field).precision = -2
];
}
Expand Down

0 comments on commit 1135df3

Please sign in to comment.