I have a to_string function that transforms the integer part like this
template<typename T>
inline string to_string_num_base_integer(T num)const noexcept{
string aret;
do{//do while, also has a return value when num is 0
const auto first_char_index = mod(num,_radix);
num /= (T)_radix;
aret.push_front(_radix_table[(size_t)first_char_index]);
}while(num>=T{1});
return aret;
}
And for getting a number from a string, its integer part looks like this
template<typename T>
inline T from_string_get_num_base_integer(string str) const noexcept {
T aret{};
for(size_t i = 0; i < str.size(); i++) {
const size_t index = _radix_table.find(str[i]);
if(index == string::npos)
return T();
aret *= (T)_radix;
aret += (T)index;
}
return aret;
}
To avoid misunderstandings caused by the ambiguity of my narrative, here is a link to the specific definition of to_string
I was confident a while back that my to_string and from_string_get could convert any arithmetic type to string and back again without any loss, so I wrote a brenchmark that looked something like this
template<class T>
T rand() {
using namespace elc::defs;//data_view
T aret;
data_view<T> ret_data_view{&aret};
for(auto& i: ret_data_view)
i = byte(::std::rand() % 256);
return aret;
}
static void ELC_to_string(benchmark::State& state){
elc::string str;
for(auto _ : state){
auto num=rand<double>();
str=elc::to_string(num);
//check
state.PauseTiming();
auto check_num = elc::from_string_get<double>(str);
if(num != check_num) {
auto debug_view = str.c_str();
__debugbreak();
}
state.ResumeTiming();
}
}
BENCHMARK(ELC_to_string);
However, once the program was up and running, I found that I was so wrong that basically my program passed at most one benchmaek iteration, or failed check on the first try Here's the data from a few random errors, it looks like it's a little off for very large absolute values
num -2.4036764711757781e+254 double
check_num -2.4036764711757750e+254 double
debug_view 0x000001754b13c490 U"-240367647117577728022446624802680682422824428288020260400400084004424482080208820222020204062260868842284288622624422042822844260426280602884262806220668240682804604406006206026884268064026880864280662062262284000686220022420882642600280488082246242286202" const char32_t *
//
num 8.6154366127603682e+286 double
check_num 8.6154366127603513e+286 double
debug_view 0x00000223c6639580 U"86154366127603626606660082262640004866628468864688428288602262664666266684028042888846648644004400062268644624004624288622444666422464206066220046268260080844820628642240264646022846802064206462084048028268042804280644268088820448646800866008206646204206808086622826046822408866088646226" const char32_t *
I would like to know how I can fix this problem?
