From 9c4e5d259a2af7859c780c6ab97766e92f416f48 Mon Sep 17 00:00:00 2001 From: Waldemar Porscha Date: Tue, 22 Aug 2023 11:28:10 +0200 Subject: [PATCH 1/3] FIX: float16ToPayload - all DPT9 numbers of the form (2^n)/100, n>10 (first value is 20.48) were converted to a payload of 0 - in other words: 20.48; 40.96; 81.92; etc. were send as 0! --- src/knx/dptconvert.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/knx/dptconvert.cpp b/src/knx/dptconvert.cpp index e8b48b7..fc6a932 100644 --- a/src/knx/dptconvert.cpp +++ b/src/knx/dptconvert.cpp @@ -1749,8 +1749,8 @@ void float16ToPayload(uint8_t* payload, size_t payload_length, int index, double value *= 100.0; unsigned short exponent = 0; - if(value > 2048) - exponent = ceil(log2(value) - 11.0); + if(value >= 2048) + exponent = ceil(log2(value + 1.0) - 11.0); short mantissa = roundf(value / (1 << exponent)); From a5c79214e3842a6594c575aedec4f520375e4be8 Mon Sep 17 00:00:00 2001 From: Waldemar Porscha Date: Thu, 24 Aug 2023 10:43:21 +0200 Subject: [PATCH 2/3] Improved float16ToPayload correction --- src/knx/dptconvert.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/knx/dptconvert.cpp b/src/knx/dptconvert.cpp index fc6a932..1e479b2 100644 --- a/src/knx/dptconvert.cpp +++ b/src/knx/dptconvert.cpp @@ -1753,6 +1753,12 @@ void float16ToPayload(uint8_t* payload, size_t payload_length, int index, double exponent = ceil(log2(value + 1.0) - 11.0); short mantissa = roundf(value / (1 << exponent)); + // above calculation causes mantissa overflow for values of the form 2^n, where n>11 + if (mantissa >= 0x800) + { + exponent++; + mantissa = roundf(value / (1 << exponent)); + } if (wasNegative) mantissa *= -1; From fd952c817e1dbbe94450d2ed798a15babb82eb7c Mon Sep 17 00:00:00 2001 From: Waldemar Porscha Date: Thu, 24 Aug 2023 10:49:55 +0200 Subject: [PATCH 3/3] soved merge problem --- src/knx/dptconvert.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/knx/dptconvert.cpp b/src/knx/dptconvert.cpp index 1e479b2..1954d2d 100644 --- a/src/knx/dptconvert.cpp +++ b/src/knx/dptconvert.cpp @@ -1749,8 +1749,8 @@ void float16ToPayload(uint8_t* payload, size_t payload_length, int index, double value *= 100.0; unsigned short exponent = 0; - if(value >= 2048) - exponent = ceil(log2(value + 1.0) - 11.0); + if(value > 2048) + exponent = ceil(log2(value) - 11.0); short mantissa = roundf(value / (1 << exponent)); // above calculation causes mantissa overflow for values of the form 2^n, where n>11