From de104f0b35fb6f97e82c81623422f6fc377ea5bb Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Tue, 5 Jan 2021 19:23:35 -0600 Subject: [PATCH] Bugfix: conversion scale maximums are too short and rename scale to digits. The "scale" max is not consistently correct. In some cases I have the signed one number less than the unsigned version (which should only be true for the binary representation). In other cases, I should have a larger number because it doesn't fully represent all digits. This is likely a mistake resulting from my misuse of the term "scale". The way I am using "scale" is not exactly correct with the meaning of the word. Rename "scale" to "digits" to better represent what this variable its related defines are for. --- level_0/f_conversion/c/conversion-common.h | 64 ++++++++--------- level_0/f_conversion/c/conversion.h | 4 +- level_1/fl_conversion/c/conversion.c | 110 ++++++++++++++--------------- 3 files changed, 89 insertions(+), 89 deletions(-) diff --git a/level_0/f_conversion/c/conversion-common.h b/level_0/f_conversion/c/conversion-common.h index 465d61d..7ecc648 100644 --- a/level_0/f_conversion/c/conversion-common.h +++ b/level_0/f_conversion/c/conversion-common.h @@ -17,61 +17,61 @@ extern "C" { #endif /** - * Provide custom conversion scale limits based on selected type sizes. + * Provide custom conversion digit limits based on selected type sizes. * - * Utilize the f_type_number_* defines to determine the expected sizes to use for the scales. + * These designate the number of digits required to represent some number for some base unit. * * 64-bit is the designed default. */ #ifndef _di_f_type_number_64_t_ - #define f_conversion_scale_binary_unsigned 64 - #define f_conversion_scale_binary_signed 63 + #define f_conversion_digits_binary_unsigned 64 + #define f_conversion_digits_binary_signed 63 - #define f_conversion_scale_octal_unsigned 21 - #define f_conversion_scale_octal_signed 20 + #define f_conversion_digits_octal_unsigned 22 + #define f_conversion_digits_octal_signed 22 - #define f_conversion_scale_decimal_unsigned 19 - #define f_conversion_scale_decimal_signed 18 + #define f_conversion_digits_decimal_unsigned 19 + #define f_conversion_digits_decimal_signed 19 - #define f_conversion_scale_duodecimal_unsigned 17 - #define f_conversion_scale_duodecimal_signed 17 + #define f_conversion_digits_duodecimal_unsigned 18 + #define f_conversion_digits_duodecimal_signed 18 - #define f_conversion_scale_hexidecimal_unsigned 15 - #define f_conversion_scale_hexidecimal_signed 15 + #define f_conversion_digits_hexidecimal_unsigned 16 + #define f_conversion_digits_hexidecimal_signed 16 #endif // _di_f_type_number_64_t_ #ifdef _en_f_type_number_32_t_ - #define f_conversion_scale_binary_unsigned 32 - #define f_conversion_scale_binary_signed 31 + #define f_conversion_digits_binary_unsigned 32 + #define f_conversion_digits_binary_signed 31 - #define f_conversion_scale_octal_unsigned 10 - #define f_conversion_scale_octal_signed 10 + #define f_conversion_digits_octal_unsigned 10 + #define f_conversion_digits_octal_signed 10 - #define f_conversion_scale_decimal_unsigned 9 - #define f_conversion_scale_decimal_signed 9 + #define f_conversion_digits_decimal_unsigned 9 + #define f_conversion_digits_decimal_signed 9 - #define f_conversion_scale_duodecimal_unsigned 8 - #define f_conversion_scale_duodecimal_signed 8 + #define f_conversion_digits_duodecimal_unsigned 8 + #define f_conversion_digits_duodecimal_signed 8 - #define f_conversion_scale_hexidecimal_unsigned 7 - #define f_conversion_scale_hexidecimal_signed 7 + #define f_conversion_digits_hexidecimal_unsigned 8 + #define f_conversion_digits_hexidecimal_signed 8 #endif // _en_f_type_number_32_t_ #ifdef _en_f_type_number_128_t_ - #define f_conversion_scale_binary_unsigned 128 - #define f_conversion_scale_binary_signed 127 + #define f_conversion_digits_binary_unsigned 128 + #define f_conversion_digits_binary_signed 127 - #define f_conversion_scale_octal_unsigned 42 - #define f_conversion_scale_octal_signed 42 + #define f_conversion_digits_octal_unsigned 43 + #define f_conversion_digits_octal_signed 43 - #define f_conversion_scale_decimal_unsigned 38 - #define f_conversion_scale_decimal_signed 38 + #define f_conversion_digits_decimal_unsigned 38 + #define f_conversion_digits_decimal_signed 38 - #define f_conversion_scale_duodecimal_unsigned 35 - #define f_conversion_scale_duodecimal_signed 35 + #define f_conversion_digits_duodecimal_unsigned 36 + #define f_conversion_digits_duodecimal_signed 36 - #define f_conversion_scale_hexidecimal_unsigned 31 - #define f_conversion_scale_hexidecimal_signed 31 + #define f_conversion_digits_hexidecimal_unsigned 32 + #define f_conversion_digits_hexidecimal_signed 32 #endif // _en_f_type_number_128_t_ #ifdef __cplusplus diff --git a/level_0/f_conversion/c/conversion.h b/level_0/f_conversion/c/conversion.h index c20f6b8..d39098f 100644 --- a/level_0/f_conversion/c/conversion.h +++ b/level_0/f_conversion/c/conversion.h @@ -205,7 +205,7 @@ extern "C" { * @return * F_none if the number was converted to a string. * F_parameter (with error bit) if a parameter is invalid. - * F_memory_reallocation (with error bit) on memory reallocation error. + * F_memory_not (with error bit) on out of memory. */ #ifndef _di_f_conversion_number_signed_to_string_ extern f_status_t f_conversion_number_signed_to_string(const f_number_signed_t number, const uint8_t base, f_string_dynamic_t *destination); @@ -228,7 +228,7 @@ extern "C" { * @return * F_none if the number was converted to a string. * F_parameter (with error bit) if a parameter is invalid. - * F_memory_reallocation (with error bit) on memory reallocation error. + * F_memory_not (with error bit) on out of memory. */ #ifndef _di_f_conversion_number_unsigned_to_string_ extern f_status_t f_conversion_number_unsigned_to_string(const f_number_unsigned_t number, const uint8_t base, f_string_dynamic_t *destination); diff --git a/level_1/fl_conversion/c/conversion.c b/level_1/fl_conversion/c/conversion.c index 0db0c95..0c0ff2c 100644 --- a/level_1/fl_conversion/c/conversion.c +++ b/level_1/fl_conversion/c/conversion.c @@ -16,18 +16,18 @@ extern "C" { return F_data_not; } - uint8_t scale = 0; + uint8_t digits = 0; f_number_unsigned_t digit = 0; f_number_unsigned_t converted = 0; for (f_string_length_t i = range.start; i <= range.stop; i++) { if (f_conversion_character_to_binary(string[i], &digit) == F_none) { - if (scale) { - scale++; + if (digits) { + digits++; if (negative) { - if (scale > f_conversion_scale_binary_signed) { + if (digits > f_conversion_digits_binary_signed) { return F_status_set_error(F_number_underflow); } @@ -35,7 +35,7 @@ extern "C" { converted -= digit; } else { - if (scale > f_conversion_scale_binary_signed) { + if (digits > f_conversion_digits_binary_signed) { return F_status_set_error(F_number_overflow); } @@ -44,7 +44,7 @@ extern "C" { } } else if (digit != 0) { - scale = 1; + digits = 1; if (negative) { converted = 0 - digit; @@ -76,17 +76,17 @@ extern "C" { return F_data_not; } - uint8_t scale = 0; + uint8_t digits = 0; f_number_unsigned_t digit = 0; f_number_unsigned_t converted = 0; for (f_string_length_t i = range.start; i <= range.stop; i++) { if (f_conversion_character_to_binary(string[i], &digit) == F_none) { - if (scale) { - scale++; + if (digits) { + digits++; - if (scale > f_conversion_scale_binary_unsigned) { + if (digits > f_conversion_digits_binary_unsigned) { return F_status_set_error(F_number_overflow); } @@ -94,7 +94,7 @@ extern "C" { converted += digit; } else if (digit != 0) { - scale = 1; + digits = 1; converted = digit; } } @@ -120,7 +120,7 @@ extern "C" { return F_data_not; } - uint8_t scale = 0; + uint8_t digits = 0; f_number_unsigned_t digit = 0; f_number_unsigned_t converted = 0; @@ -128,11 +128,11 @@ extern "C" { if (f_conversion_character_to_decimal(string[i], &digit) == F_none) { - if (scale) { - scale++; + if (digits) { + digits++; if (negative) { - if (scale > f_conversion_scale_decimal_signed) { + if (digits > f_conversion_digits_decimal_signed) { if ((converted * 10) - digit < f_number_t_size_negative || (converted * 10) - digit > converted) { return F_status_set_error(F_number_underflow); } @@ -142,7 +142,7 @@ extern "C" { converted -= digit; } else { - if (scale > f_conversion_scale_decimal_signed) { + if (digits > f_conversion_digits_decimal_signed) { if ((converted * 10) + digit > f_number_t_size_positive || (converted * 10) + digit < converted) { return F_status_set_error(F_number_overflow); } @@ -153,7 +153,7 @@ extern "C" { } } else if (digit != 0) { - scale = 1; + digits = 1; if (negative) { converted = 0 - digit; @@ -185,7 +185,7 @@ extern "C" { return F_data_not; } - uint8_t scale = 0; + uint8_t digits = 0; f_number_unsigned_t digit = 0; f_number_unsigned_t converted = 0; @@ -193,10 +193,10 @@ extern "C" { if (f_conversion_character_to_decimal(string[i], &digit) == F_none) { - if (scale) { - scale++; + if (digits) { + digits++; - if (scale > f_conversion_scale_decimal_unsigned) { + if (digits > f_conversion_digits_decimal_unsigned) { if ((converted * 10) + digit > f_number_t_size_unsigned || (converted * 10) + digit < converted) { return F_status_set_error(F_number_overflow); } @@ -206,7 +206,7 @@ extern "C" { converted += digit; } else if (digit != 0) { - scale = 1; + digits = 1; converted = digit; } } @@ -232,7 +232,7 @@ extern "C" { return F_data_not; } - uint8_t scale = 0; + uint8_t digits = 0; f_number_unsigned_t digit = 0; f_number_unsigned_t converted = 0; @@ -240,11 +240,11 @@ extern "C" { if (f_conversion_character_to_duodecimal(string[i], &digit) == F_none) { - if (scale) { - scale++; + if (digits) { + digits++; if (negative) { - if (scale > f_conversion_scale_duodecimal_signed) { + if (digits > f_conversion_digits_duodecimal_signed) { if ((converted * 12) - digit < f_number_t_size_negative || (converted * 12) - digit > converted) { return F_status_set_error(F_number_underflow); } @@ -254,7 +254,7 @@ extern "C" { converted -= digit; } else { - if (scale > f_conversion_scale_duodecimal_signed) { + if (digits > f_conversion_digits_duodecimal_signed) { if ((converted * 12) + digit > f_number_t_size_positive || (converted * 12) + digit < converted) { return F_status_set_error(F_number_overflow); } @@ -265,7 +265,7 @@ extern "C" { } } else if (digit != 0) { - scale = 1; + digits = 1; if (negative) { converted = 0 - digit; @@ -297,7 +297,7 @@ extern "C" { return F_data_not; } - uint8_t scale = 0; + uint8_t digits = 0; f_number_unsigned_t digit = 0; f_number_unsigned_t converted = 0; @@ -305,10 +305,10 @@ extern "C" { if (f_conversion_character_to_duodecimal(string[i], &digit) == F_none) { - if (scale) { - scale++; + if (digits) { + digits++; - if (scale > f_conversion_scale_duodecimal_unsigned) { + if (digits > f_conversion_digits_duodecimal_unsigned) { if ((converted * 12) + digit > f_number_t_size_unsigned || (converted * 12) + digit < converted) { return F_status_set_error(F_number_overflow); } @@ -318,7 +318,7 @@ extern "C" { converted += digit; } else if (digit != 0) { - scale = 1; + digits = 1; converted = digit; } } @@ -344,7 +344,7 @@ extern "C" { return F_data_not; } - uint8_t scale = 0; + uint8_t digits = 0; f_number_unsigned_t digit = 0; f_number_unsigned_t converted = 0; @@ -352,11 +352,11 @@ extern "C" { if (f_conversion_character_to_hexidecimal(string[i], &digit) == F_none) { - if (scale) { - scale++; + if (digits) { + digits++; if (negative) { - if (scale > f_conversion_scale_hexidecimal_signed) { + if (digits > f_conversion_digits_hexidecimal_signed) { if ((converted << 4) - digit < f_number_t_size_negative || (converted << 4) - digit > converted) { return F_status_set_error(F_number_underflow); } @@ -366,7 +366,7 @@ extern "C" { converted -= digit; } else { - if (scale > f_conversion_scale_hexidecimal_signed) { + if (digits > f_conversion_digits_hexidecimal_signed) { if ((converted << 4) + digit > f_number_t_size_positive || (converted << 4) + digit < converted) { return F_status_set_error(F_number_overflow); } @@ -377,7 +377,7 @@ extern "C" { } } else if (digit != 0) { - scale = 1; + digits = 1; if (negative) { converted = 0 - digit; @@ -409,7 +409,7 @@ extern "C" { return F_data_not; } - uint8_t scale = 0; + uint8_t digits = 0; f_number_unsigned_t digit = 0; f_number_unsigned_t converted = 0; @@ -417,10 +417,10 @@ extern "C" { if (f_conversion_character_to_hexidecimal(string[i], &digit) == F_none) { - if (scale) { - scale++; + if (digits) { + digits++; - if (scale > f_conversion_scale_hexidecimal_unsigned) { + if (digits > f_conversion_digits_hexidecimal_unsigned) { if ((converted << 4) + digit > f_number_t_size_unsigned || (converted << 4) + digit < converted) { return F_status_set_error(F_number_overflow); } @@ -430,7 +430,7 @@ extern "C" { converted += digit; } else if (digit != 0) { - scale = 1; + digits = 1; converted = digit; } } @@ -456,7 +456,7 @@ extern "C" { return F_data_not; } - uint8_t scale = 0; + uint8_t digits = 0; f_number_unsigned_t digit = 0; f_number_unsigned_t converted = 0; @@ -464,11 +464,11 @@ extern "C" { if (f_conversion_character_to_octal(string[i], &digit) == F_none) { - if (scale) { - scale++; + if (digits) { + digits++; if (negative) { - if (scale > f_conversion_scale_octal_signed) { + if (digits > f_conversion_digits_octal_signed) { if ((converted << 3) - digit < f_number_t_size_negative || (converted << 3) - digit > converted) { return F_status_set_error(F_number_underflow); } @@ -478,7 +478,7 @@ extern "C" { converted -= digit; } else { - if (scale > f_conversion_scale_octal_signed) { + if (digits > f_conversion_digits_octal_signed) { if ((converted << 3) + digit > f_number_t_size_positive || (converted << 3) + digit < converted) { return F_status_set_error(F_number_overflow); } @@ -489,7 +489,7 @@ extern "C" { } } else if (digit != 0) { - scale = 1; + digits = 1; if (negative) { converted = 0 - digit; @@ -521,7 +521,7 @@ extern "C" { return F_data_not; } - uint8_t scale = 0; + uint8_t digits = 0; f_number_unsigned_t digit = 0; f_number_unsigned_t converted = 0; @@ -529,10 +529,10 @@ extern "C" { if (f_conversion_character_to_octal(string[i], &digit) == F_none) { - if (scale) { - scale++; + if (digits) { + digits++; - if (scale > f_conversion_scale_octal_unsigned) { + if (digits > f_conversion_digits_octal_unsigned) { if ((converted << 3) + digit > f_number_t_size_unsigned || (converted << 3) + digit < converted) { return F_status_set_error(F_number_overflow); } @@ -542,7 +542,7 @@ extern "C" { converted += digit; } else if (digit != 0) { - scale = 1; + digits = 1; converted = digit; } } -- 1.8.3.1