Skip to content

Commit d39140b

Browse files
surfdadolukash
authored andcommitted
Round scaled floats before serializing to integers
Casting a scaled float directly to an integer truncates values such as 0.59 at scale 100 to 58. This causes configuration read-backs and repeated save/read cycles to lose precision. Use lroundf in buffer_append_float16 and buffer_append_float32 in all three buffer.c implementations, covering the package and example copies. This matches VESC Tool's nearest-integer rounding for either sign. Validated every signed 16-bit encoding at scales 1, 10, 100, 1000, 10000 and 100000 through ten round trips, plus larger 32-bit samples and signed halfway cases, against each copy with a temporary host harness. All pass; each original implementation fails the round-trip check.
1 parent fd0708b commit d39140b

3 files changed

Lines changed: 6 additions & 6 deletions

File tree

‎src/conf/buffer.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ void buffer_append_uint32(uint8_t *buffer, uint32_t number, int32_t *index) {
6868
}
6969

7070
void buffer_append_float16(uint8_t *buffer, float number, float scale, int32_t *index) {
71-
buffer_append_int16(buffer, (int16_t) (number * scale), index);
71+
buffer_append_int16(buffer, (int16_t) lroundf(number * scale), index);
7272
}
7373

7474
void buffer_append_float32(uint8_t *buffer, float number, float scale, int32_t *index) {
75-
buffer_append_int32(buffer, (int32_t) (number * scale), index);
75+
buffer_append_int32(buffer, (int32_t) lroundf(number * scale), index);
7676
}
7777

7878
/*

‎vesc_pkg_lib/examples/config/conf/buffer.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ void buffer_append_uint32(uint8_t* buffer, uint32_t number, int32_t *index) {
4646
}
4747

4848
void buffer_append_float16(uint8_t* buffer, float number, float scale, int32_t *index) {
49-
buffer_append_int16(buffer, (int16_t)(number * scale), index);
49+
buffer_append_int16(buffer, (int16_t)lroundf(number * scale), index);
5050
}
5151

5252
void buffer_append_float32(uint8_t* buffer, float number, float scale, int32_t *index) {
53-
buffer_append_int32(buffer, (int32_t)(number * scale), index);
53+
buffer_append_int32(buffer, (int32_t)lroundf(number * scale), index);
5454
}
5555

5656
/*

‎vesc_pkg_lib/examples/custom_data_comm/buffer.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ void buffer_append_uint32(uint8_t* buffer, uint32_t number, int32_t *index) {
4646
}
4747

4848
void buffer_append_float16(uint8_t* buffer, float number, float scale, int32_t *index) {
49-
buffer_append_int16(buffer, (int16_t)(number * scale), index);
49+
buffer_append_int16(buffer, (int16_t)lroundf(number * scale), index);
5050
}
5151

5252
void buffer_append_float32(uint8_t* buffer, float number, float scale, int32_t *index) {
53-
buffer_append_int32(buffer, (int32_t)(number * scale), index);
53+
buffer_append_int32(buffer, (int32_t)lroundf(number * scale), index);
5454
}
5555

5656
/*

0 commit comments

Comments
 (0)