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);
}
char* toString_float(float64 n, bool withPostfix, bool uppercase){
// int64 d=n;
// float64 r=n-d;
// char* strint=toString_int(d);
// char strfract[32];
// uint8 i=0;
// strfract[i++]='.';
// while(r!=0){
// r*=10.0;
// char fc=r;
// strfract[i++]=fc;
// 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>");
#define _toString_float_impl(bufsize, maxPrecision) {\
char str[bufsize];\
if(precision>maxPrecision)\
throw("too big precision");\
if(precision==0)\
precision=toString_float_default_precision;\
int cn=sprintf(str, "%.*f", precision, n);\
if(withPostfix)\
str[cn++]= uppercase ? 'F' : 'f';\
str[cn]='\0';\
return cptr_copy(str);\
}
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) {\
str[cn++]='0' + (uint8)((byte>>7)&1); /* 8th bit */\
str[cn++]='0' + (uint8)((byte>>6)&1); /* 7th bit */\
@ -204,7 +201,7 @@ __toString_uint_def(64)
switch(kp_fmt_dataFormat(f)){\
case kp_f: ;\
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:\
return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f));\
case kp_h:\

View File

@ -6,27 +6,37 @@ extern "C" {
#include "../errors.h"
// functions for base types
// char and cstring
// has different output for fmtChar and fmtString
char* __toString_char(void* c, uint32 fmt);
// bool
char* __toString_bool(void* c, uint32 fmt);
// signed int
char* toString_int(int64 n);
char* __toString_int8(void* n, uint32 fmt);
char* __toString_int16(void* n, uint32 fmt);
char* __toString_int32(void* n, uint32 fmt);
char* __toString_int64(void* n, uint32 fmt);
// unsigned int
char* toString_uint(uint64 n, bool withPostfix, bool uppercase);
char* __toString_uint8(void* n, uint32 fmt);
char* __toString_uint16(void* n, uint32 fmt);
char* __toString_uint32(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_float64(void* n, uint32 fmt);
///@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);
///@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;
case 'f':
// 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;
case 'l':
l=true;
if((c=format[i++]))
goto format_escape_seq;
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 'x': ;
uint64 px=va_arg(vl, uint64);