[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Using numbers as float
- From: Glenn Maynard <glenn@...>
- Date: Thu, 29 Jun 2006 15:58:29 -0400
On Thu, Jun 29, 2006 at 12:41:14PM -0700, Mark Edgar wrote:
> strtod() works just fine; no cast is required:
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(void)
> {
> float x = strtod("3.2", NULL);
I think VC will say "warning: implicit conversion loses data" or
something to that effect. Which I do find to be a useful warning;
I prefer to get float vs. double correct, and do like to be told
if I'm being inconsistent. (It isn't smart enough to not warn
about "uint8_t = i & 0xFF", though, and it's mildly annoying to
have to cast that to shut it up. Net win, though, in my opinion.)
> You probably won't find anything faster, as your implementation's
> strtod() is probably written in nice fast assembly.
The strtod() implementations I've seen havn't been assembly; it's
a complicated conversion to do right. (Maybe some inner loops could
benefit, but better compilers have increased the threshold of it being
worth doing so ...)
> If you have a C99 conforming system, you can use strtof() instead. I
> don't know that you'll notice a performance difference between strtod()
> and strtof().
You'll see a serious performance difference on systems without hardware
doubles. You'd probably know if you were on one of those, though, and
be compiling Lua yourself to be sure it's using floats.
Note that I've seen strtof() implementations that are simply casted
wrappers around strtod(), where I had to change it by hand.
--
Glenn Maynard