Remix.run Logo
▲ lelanthran 4 hours ago

That doesn't detect overflows and underflows. Also changes based on locale, which is not thread safe (so you cannot set locale, convert, then unset locale).

▲geocar 2 hours ago | parent [-]

> That doesn't detect overflows and underflows

A trivial change does though: Just round-trip it.

    int s2d(double*restrict out,const char*restrict in){
     char tmp[99];int r,q=sscanf(in,"%lf%n",out,&r),s=sprintf(tmp,"%lf.",*out);
     return q&&!((r>s)||memcmp(in,tmp,r)||tmp[r]>'0');
    }
> Also changes based on locale,

That's a feature: Where did the string come from if not the user?

> you cannot set locale, convert, then unset locale

So what? You're not handling multiple locales: You're using the user's locale. If you're making a server, using anything but LANG=C or a utf8 variant is asking for trouble.

I think the point is it's absolutely possible to portably detect errors, some libc's strtod() just don't which ain't the same thing.

▲daemin 2 hours ago | parent [-]

>> Also changes based on locale,

> That's a feature: Where did the string come from if not the user?

Many games and applications store floating point values in text file formats. This has caused bugs in those programs many times over because they didn't realise it was locale dependent and the data parsing broke in another locale.