#include <string>
#include <iostream>
#include <cmath>
#include <stdexcept>
double string_to_double(std::string s, unsigned short radix)
{
std::size_t decimal_pos = s.find('.');
int decimals = 0;
if(decimal_pos != std::string::npos)
{
s.erase(decimal_pos, 1);
decimals = s.size() - decimal_pos;
}
size_t errpos;
double answer = stoll(s, &errpos, radix) / std::pow(radix, decimals);;
if(errpos < s.size())
throw std::invalid_argument("parse error in string_to_double");
return answer;
}
int main()
{
std::cout << string_to_double("10.A",16) << '\n'
<< string_to_double("1001.1",2) << '\n'
<< string_to_double("123.4",10) << '\n'
<< string_to_double("-.123",10) << '\n'
<< string_to_double("abc",16) << '\n'
<< string_to_double("111",2) << '\n';
}
I2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y21hdGg+CiNpbmNsdWRlIDxzdGRleGNlcHQ+Cgpkb3VibGUgc3RyaW5nX3RvX2RvdWJsZShzdGQ6OnN0cmluZyBzLCB1bnNpZ25lZCBzaG9ydCByYWRpeCkKewogICAgc3RkOjpzaXplX3QgZGVjaW1hbF9wb3MgPSBzLmZpbmQoJy4nKTsKICAgIGludCBkZWNpbWFscyA9IDA7CiAgICBpZihkZWNpbWFsX3BvcyAhPSBzdGQ6OnN0cmluZzo6bnBvcykKICAgIHsKICAgICAgICBzLmVyYXNlKGRlY2ltYWxfcG9zLCAxKTsKICAgICAgICBkZWNpbWFscyA9IHMuc2l6ZSgpIC0gZGVjaW1hbF9wb3M7CiAgICB9CiAgICBzaXplX3QgZXJycG9zOwogICAgZG91YmxlIGFuc3dlciA9IHN0b2xsKHMsICZlcnJwb3MsIHJhZGl4KSAvIHN0ZDo6cG93KHJhZGl4LCBkZWNpbWFscyk7OwogICAgaWYoZXJycG9zIDwgcy5zaXplKCkpCiAgICAgICAgdGhyb3cgc3RkOjppbnZhbGlkX2FyZ3VtZW50KCJwYXJzZSBlcnJvciBpbiBzdHJpbmdfdG9fZG91YmxlIik7CiAgICByZXR1cm4gYW5zd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