Skip to content

Commit

Permalink
Fixed precision/number of decimals in conversion block. Issue #73
Browse files Browse the repository at this point in the history
  • Loading branch information
ihedvall committed Feb 15, 2024
1 parent 03586d7 commit f0ded04
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
15 changes: 10 additions & 5 deletions mdflib/src/cc4block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,16 @@ ConversionType Cc4Block::Type() const {
return static_cast<ConversionType>(type_);
}

void Cc4Block::Decimals(uint8_t decimals) { precision_ = decimals; }
void Cc4Block::Decimals(uint8_t decimals) {
flags_ |= CcFlag::PrecisionValid;
precision_ = decimals;
}

uint8_t Cc4Block::Decimals() const {
const auto max = static_cast<uint8_t>(
channel_data_type_ == 4 ? std::numeric_limits<float>::max_digits10
: std::numeric_limits<double>::max_digits10);
return std::min(precision_, max);
std::numeric_limits<double>::max_digits10);
return flags_ & CcFlag::PrecisionValid ?
precision_ : max;
}

bool Cc4Block::IsUnitValid() const { return Link(kIndexUnit) != 0; }
Expand All @@ -116,7 +119,9 @@ bool Cc4Block::IsDecimalUsed() const {
}

void Cc4Block::Range(double min, double max) {
flags_ |= CcFlag::RangeValid;
if (max > min) {
flags_ |= CcFlag::RangeValid;
}
range_min_ = min;
range_max_ = max;
}
Expand Down
3 changes: 2 additions & 1 deletion mdflib/src/cn4block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ uint8_t Cn4Block::Decimals() const {
const auto max = static_cast<uint8_t>(
DataBytes() == 4 ? std::numeric_limits<float>::max_digits10
: std::numeric_limits<double>::max_digits10);
return std::min(precision_, max);
return flags_ & CnFlag::PrecisionValid ?
precision_ : max;
}

bool Cn4Block::IsDecimalUsed() const { return flags_ & CnFlag::PrecisionValid; }
Expand Down
12 changes: 12 additions & 0 deletions mdflib_test/src/testwrite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -850,14 +850,20 @@ TEST_F(TestWrite, Mdf4WriteCN_SI_CC) {
EXPECT_EQ(cn4->DataType(),ChannelDataType::UnsignedIntegerLe);
EXPECT_EQ(cn4->DataBytes(), 8);
// EXPECT_DOUBLE_EQ(cn4->SamplingRate(), 1.33); Not supported in MDF4

EXPECT_TRUE(cn4->Flags() & CnFlag::RangeValid);
const auto range = cn4->Range();
EXPECT_TRUE(range.has_value());
EXPECT_DOUBLE_EQ(range.value().first, 0);
EXPECT_DOUBLE_EQ(range.value().second, 10);

EXPECT_TRUE(cn4->Flags() & CnFlag::LimitValid);
const auto limit = cn4->Limit();
EXPECT_TRUE(limit.has_value());
EXPECT_DOUBLE_EQ(limit.value().first, 1);
EXPECT_DOUBLE_EQ(limit.value().second, 9);

EXPECT_TRUE(cn4->Flags() & CnFlag::ExtendedLimitValid);
const auto ext_limit = cn4->ExtLimit();
EXPECT_TRUE(ext_limit.has_value());
EXPECT_DOUBLE_EQ(ext_limit.value().first, 2);
Expand All @@ -883,11 +889,17 @@ TEST_F(TestWrite, Mdf4WriteCN_SI_CC) {
EXPECT_STREQ(cc4->Description().c_str(),"Cc1-Desc");
EXPECT_STREQ(cc4->Unit().c_str(),"Cc1-Unit");
EXPECT_EQ(cc4->Type(),ConversionType::Linear);

EXPECT_TRUE(cc4->Flags() & CcFlag::PrecisionValid);
EXPECT_EQ(cc4->Decimals(),3);

EXPECT_TRUE(cc4->Flags() & CcFlag::RangeValid);
const auto range1 = cc4->Range();
EXPECT_TRUE(range1.has_value());
EXPECT_DOUBLE_EQ(range1.value().first, 0);
EXPECT_DOUBLE_EQ(range1.value().second, 10);

EXPECT_TRUE(cc4->Flags() & CcFlag::PrecisionValid);
}
}
}
Expand Down

0 comments on commit f0ded04

Please sign in to comment.