Skip to content

Commit

Permalink
ADD: update float printing
Browse files Browse the repository at this point in the history
  • Loading branch information
T-K-233 committed Jun 6, 2024
1 parent 125a46f commit 82ba2d4
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions nn/src/nn_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,20 @@ void NN_printFloat(float v, int16_t num_digits) {
long int_part = (long)v;
float fractional_part = v - int_part;

// Count the number of digits in the integer part
long temp = int_part;
int int_digits = (int_part == 0) ? 1 : 0; // Handle zero as a special case
while (temp > 0) {
int_digits++;
temp /= 10;
}

// Print the integer part
printf("%ld", int_part);

// Calculate the number of fractional digits we can print
int fractional_digits = num_digits - int_digits;
if (fractional_digits > 0) {
if (num_digits > 0) {
printf("."); // Print the decimal point
}

// Handle the fractional part
while (fractional_digits-- > 0) {
fractional_part *= 10;
int digit = (int)(fractional_part);
printf("%d", digit);
fractional_part -= digit;
}
// Handle the fractional part
while (num_digits > 0) {
num_digits -= 1;
fractional_part *= 10;
int digit = (int)(fractional_part);
printf("%d", digit);
fractional_part -= digit;
}
}

Expand Down

0 comments on commit 82ba2d4

Please sign in to comment.