Given two different strings string s1
and string s2
that the contain the same decimal number with different number of trailing zeros after the decimal point, is it always guaranteed that stod(s1)==stod(s2)
?
For example:
#include<iostream>
using namespace std;
bool equalConversion(const string& s1,const string& s2)
{
return stod(s1)==stod(s2);
}
int main(int argc, char *argv[])
{
string s1="0.1232340";
string s2="0.12323400";
cout<<"equal conversion: "<<equalConversion(s1,s2)<<endl;
return 0;
}
I tried with different values for s1
and s2
and it seems so but is there also a strong guarantee especially for strings with very large or small numbers that cannot be represented exactly as a double value and therefore would be rounded to the next representable double value. What if the smallest bigger and largest smaller representable value are equally far from the target number in the string? Can trailing zeros disturb the conversion function stod
in a way that with additional trailing zeros the chosen double representation is different from the one that would be chosen without them? In other words: Is the conversion from string to double consistent?