toString_float

This commit is contained in:
timerix 2023-01-19 23:03:57 +06:00
parent 936ccf6c66
commit 8b5db6c23f
3 changed files with 31 additions and 38 deletions

View File

@ -61,28 +61,25 @@ char* toString_uint(uint64 n, bool withPostfix, bool uppercase){
return cptr_copy((char*)str+i); return cptr_copy((char*)str+i);
} }
char* toString_float(float64 n, bool withPostfix, bool uppercase){ #define _toString_float_impl(bufsize, maxPrecision) {\
// int64 d=n; char str[bufsize];\
// float64 r=n-d; if(precision>maxPrecision)\
// char* strint=toString_int(d); throw("too big precision");\
// char strfract[32]; if(precision==0)\
// uint8 i=0; precision=toString_float_default_precision;\
// strfract[i++]='.'; int cn=sprintf(str, "%.*f", precision, n);\
// while(r!=0){ if(withPostfix)\
// r*=10.0; str[cn++]= uppercase ? 'F' : 'f';\
// char fc=r; str[cn]='\0';\
// strfract[i++]=fc; return cptr_copy(str);\
// r-=fc;
// }
// if(withPostfix)
// strfract[i++]= uppercase ? 'F' : 'f';
// strfract[i]=0;
// char* str==cptr_concat(strint, strfract);
// free(strint);
// return str;
return cptr_copy("<float>");
} }
char* toString_float32(float32 n, uint8 precision, bool withPostfix, bool uppercase)
_toString_float_impl(48, toString_float32_max_precision)
char* toString_float64(float64 n, uint8 precision, bool withPostfix, bool uppercase)
_toString_float_impl(512, toString_float64_max_precision)
#define byte_to_bits(byte) {\ #define byte_to_bits(byte) {\
str[cn++]='0' + (uint8)((byte>>7)&1); /* 8th bit */\ str[cn++]='0' + (uint8)((byte>>7)&1); /* 8th bit */\
str[cn++]='0' + (uint8)((byte>>6)&1); /* 7th bit */\ str[cn++]='0' + (uint8)((byte>>6)&1); /* 7th bit */\
@ -204,7 +201,7 @@ __toString_uint_def(64)
switch(kp_fmt_dataFormat(f)){\ switch(kp_fmt_dataFormat(f)){\
case kp_f: ;\ case kp_f: ;\
float##BITS n=*(float##BITS*)_n;\ float##BITS n=*(float##BITS*)_n;\
return toString_float(n, kp_fmt_withPostfix(f), kp_fmt_isUpper(f));\ return toString_float64(n, toString_float_default_precision, kp_fmt_withPostfix(f), kp_fmt_isUpper(f));\
case kp_b:\ case kp_b:\
return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f));\ return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f));\
case kp_h:\ case kp_h:\

View File

@ -6,27 +6,37 @@ extern "C" {
#include "../errors.h" #include "../errors.h"
// functions for base types // char and cstring
// has different output for fmtChar and fmtString // has different output for fmtChar and fmtString
char* __toString_char(void* c, uint32 fmt); char* __toString_char(void* c, uint32 fmt);
// bool
char* __toString_bool(void* c, uint32 fmt); char* __toString_bool(void* c, uint32 fmt);
// signed int
char* toString_int(int64 n); char* toString_int(int64 n);
char* __toString_int8(void* n, uint32 fmt); char* __toString_int8(void* n, uint32 fmt);
char* __toString_int16(void* n, uint32 fmt); char* __toString_int16(void* n, uint32 fmt);
char* __toString_int32(void* n, uint32 fmt); char* __toString_int32(void* n, uint32 fmt);
char* __toString_int64(void* n, uint32 fmt); char* __toString_int64(void* n, uint32 fmt);
// unsigned int
char* toString_uint(uint64 n, bool withPostfix, bool uppercase); char* toString_uint(uint64 n, bool withPostfix, bool uppercase);
char* __toString_uint8(void* n, uint32 fmt); char* __toString_uint8(void* n, uint32 fmt);
char* __toString_uint16(void* n, uint32 fmt); char* __toString_uint16(void* n, uint32 fmt);
char* __toString_uint32(void* n, uint32 fmt); char* __toString_uint32(void* n, uint32 fmt);
char* __toString_uint64(void* n, uint32 fmt); char* __toString_uint64(void* n, uint32 fmt);
char* toString_float(float64 n, bool withPostfix, bool uppercase); // very bad implimentation // float
#define toString_float32_max_precision 6
#define toString_float64_max_precision 15
#define toString_float_default_precision 6
char* toString_float32(float32 n, uint8 precision, bool withPostfix, bool uppercase); // uses sprintf
char* toString_float64(float64 n, uint8 precision, bool withPostfix, bool uppercase); // uses sprintf
char* __toString_float32(void* n, uint32 fmt); char* __toString_float32(void* n, uint32 fmt);
char* __toString_float64(void* n, uint32 fmt); char* __toString_float64(void* n, uint32 fmt);
///@param inverse set to true for little endian numbers (their bytes are in reverse order) ///@param inverse set to true for little endian numbers (their bytes are in reverse order)
char* toString_bin(void* bytes, uint32 size, bool inverse, bool withPrefix); char* toString_bin(void* bytes, uint32 size, bool inverse, bool withPrefix);
///@param inverse set to true for little endian numbers (their bytes are in reverse order) ///@param inverse set to true for little endian numbers (their bytes are in reverse order)

View File

@ -69,27 +69,13 @@ void kprintf(const char* format, ...){
break; break;
case 'f': case 'f':
// float32 is promoted to float64 when passed through '...' // float32 is promoted to float64 when passed through '...'
argstr=toString_float(va_arg(vl, float64),0,0); argstr=toString_float64(va_arg(vl, float64), toString_float_default_precision,0,0);
break; break;
case 'l': case 'l':
l=true; l=true;
if((c=format[i++])) if((c=format[i++]))
goto format_escape_seq; goto format_escape_seq;
break; break;
// switch (c) {
// case 'u':
// argstr=toString_uint(va_arg(vl, uint64),0,0);
// break;
// case 'i':
// argstr=toString_int(va_arg(vl, uint64));
// break;
// case 'f':
// argstr=toString_float(va_arg(vl, float64),0,0);
// break;
// default:
// throw(ERR_FORMAT);
// }
// break;
case 'p': case 'p':
case 'x': ; case 'x': ;
uint64 px=va_arg(vl, uint64); uint64 px=va_arg(vl, uint64);