fixed float printing errors

This commit is contained in:
timerix 2023-01-20 00:24:55 +06:00
parent 8b5db6c23f
commit c5e1a42eb7
3 changed files with 18 additions and 8 deletions

View File

@ -68,6 +68,9 @@ char* toString_uint(uint64 n, bool withPostfix, bool uppercase){
if(precision==0)\ if(precision==0)\
precision=toString_float_default_precision;\ precision=toString_float_default_precision;\
int cn=sprintf(str, "%.*f", precision, n);\ int cn=sprintf(str, "%.*f", precision, n);\
/* remove trailing zeroes except .0*/\
while(str[cn-1]=='0' && str[cn-2]!='.')\
cn--;\
if(withPostfix)\ if(withPostfix)\
str[cn++]= uppercase ? 'F' : 'f';\ str[cn++]= uppercase ? 'F' : 'f';\
str[cn]='\0';\ str[cn]='\0';\

View File

@ -20,7 +20,14 @@ typedef union {
float64 f64; float64 f64;
void* ptr; void* ptr;
} __kp_value_union; } __kp_value_union;
#define __kpVU(value) (__kp_value_union){ value }
static inline __kp_value_union __kpVU_f(float64 f) { return (__kp_value_union){ .f64=f }; }
inline __kp_value_union __kpVU_i(int64 f) { return (__kp_value_union){ .i64=f }; }
#define __kpVU_selectType(V) _Generic(V, float: __kpVU_f, double: __kpVU_f, default: __kpVU_i)(V)
#define __kpVU(V) __kpVU_selectType(V)
#define __kp_argsToFmts8(\ #define __kp_argsToFmts8(\
a0, a1, a2, a3, a4, a5, a6, a7,...)\ a0, a1, a2, a3, a4, a5, a6, a7,...)\

View File

@ -10,19 +10,19 @@ void test_kprint(){
kp_u|kp_post,-8888, kp_c|kp_post|kp_upper,' ', kp_u,0, kp_c,' ', kp_u|kp_post,-8888, kp_c|kp_post|kp_upper,' ', kp_u,0, kp_c,' ',
kp_u,1234567890987654321LL, kp_c,'\n'); kp_u,1234567890987654321LL, kp_c,'\n');
//float //float
kprint(kp_bgGreenD|kp_fgRed| kprint(kp_fgCyanD|
kp_f,-4000.0109f, kp_c,' ', kp_f,-0.000020004f, kp_c,'\n', kp_f,-4000.0109f, kp_c,' ', kp_f,-0.000020004f, kp_c,'\n',
kp_f,-1.0f, kp_c,' ', kp_f,0.0f, kp_c,' ', kp_f,1.0f, kp_c,'\n', kp_f,-1.0f, kp_c,' ', kp_f,0.0f, kp_c,' ', kp_f,1.0f, kp_c,'\n',
kp_f|kp_post,0.000020004f, kp_c,' ', kp_f|kp_post,0.000020004f, kp_c,' ',
kp_f|kp_post|kp_upper,4000.0109f, kp_c,'\n'); kp_f|kp_post|kp_upper,4000.0109f, kp_c,'\n');
//double //double
kprint(kp_bgBlueD|kp_fgRed| kprint(kp_fgYellowD|
kp_f,-4000.0109, kp_c,' ', kp_f,-0.000020004, kp_c,'\n', kp_f,-4000.0109, kp_c,' ', kp_f,-0.000020004, kp_c,'\n',
kp_f,-1.0, kp_c,' ', kp_f,0.0, kp_c,' ', kp_f,1.0, kp_c,'\n', kp_f,-1.0, kp_c,' ', kp_f,0.0, kp_c,' ', kp_f,1.0, kp_c,'\n',
kp_f|kp_post,0.000020004, kp_c,' ', kp_f|kp_post,0.000020004, kp_c,' ',
kp_f|kp_post|kp_upper,4000.0109, kp_c,'\n'); kp_f|kp_post|kp_upper,4000.0109, kp_c,'\n');
//hex //hex
kprint(kp_bgBlack|kp_fgYellow| kprint(kp_fgYellow|
kp_h,0, kp_c,' ', kp_h,1, kp_c,' ', kp_h,-1, kp_c,' ', kp_h,15, kp_c,'\n', kp_h,0, kp_c,' ', kp_h,1, kp_c,' ', kp_h,-1, kp_c,' ', kp_h,15, kp_c,'\n',
kp_h,4095, kp_c,' ', kp_h,-4095, kp_c,'\n', kp_h,4095, kp_c,' ', kp_h,-4095, kp_c,'\n',
kp_h,1234567890987654321LL, kp_c,'\n', kp_h,-1234567890987654321LL, kp_c,'\n'); kp_h,1234567890987654321LL, kp_c,'\n', kp_h,-1234567890987654321LL, kp_c,'\n');
@ -34,12 +34,12 @@ void test_kprint(){
kp_h|kp_pre|kp_upper,-115515.009, kp_c,'\n'); kp_h|kp_pre|kp_upper,-115515.009, kp_c,'\n');
//bin //bin
kprint(kp_fgBlue| kprint(kp_fgBlue|
kp_b,0, kp_c,' ', kp_b,1, kp_c,' ', kp_b,-1, kp_c,' ', kp_b,15, kp_c,' ', kp_b,0, kp_c,'\n', kp_b,1, kp_c,'\n', kp_b,-1, kp_c,'\n', kp_b,15, kp_c,'\n',
kp_b,4095, kp_c,' ', kp_b,-4095, kp_c,'\n', kp_b,4095, kp_c,'\n', kp_b,-4095, kp_c,'\n',
kp_b,1234567890987654321LL, kp_c,'\n', kp_b,-1234567890987654321LL, kp_c,'\n'); kp_b,1234567890987654321LL, kp_c,'\n', kp_b,-1234567890987654321LL, kp_c,'\n');
kprint( kprint(
kp_b,-1.0f, kp_c,' ', kp_b,0.0f, kp_c,' ', kp_b,1.0f, kp_c,'\n', kp_b,-1.0f, kp_c,'\n', kp_b,0.0f, kp_c,'\n', kp_b,1.0f, kp_c,'\n',
kp_b, 0.00016f, kp_c,' ', kp_b,-115515.009f, kp_c,'\n'); kp_b, 0.00016f, kp_c,'\n', kp_b,-115515.009f, kp_c,'\n');
kprint( kprint(
kp_b|kp_pre, 0.00016, kp_c,'\n', kp_b|kp_pre, 0.00016, kp_c,'\n',
kp_b|kp_pre|kp_upper,-115515.009, kp_c,'\n'); kp_b|kp_pre|kp_upper,-115515.009, kp_c,'\n');