I had an impression that for such big number it will be printed in
scientific notation. But I guess that's the difference between %g and
%f. And it turns out my habit of using %g instead of %f is a very good
thing.
Thank you very much for the explanation, Jay.


Pavel

On Thu, Mar 3, 2011 at 12:06 PM, Jay A. Kreibich <j...@kreibi.ch> wrote:
> On Thu, Mar 03, 2011 at 11:06:33AM -0500, Pavel Ivanov scratched on the wall:
>> > ?A favorite interview question is, given this line and no other
>> > ?information, how big must buf_size be to never clip the output?
>> > ?You can assume the default 1.6 precision ("%1.6f").
>> > ? ?snprintf( buf, buf_size, "%f", v );
>> > ?The answer? ?At least 318 characters.
>>
>> This is very interesting. Jay, could you explain the answer? Doesn't
>> %1.6f limit number of digits to print?
>
>  Although the default precision is 1.6, that means "please use *at
>  least* one digit to the left of the decimal point, and no more than six
>  to the right."  The right side is limited, the left is not.
>
>  As per the discussion about var-arg values, you must assume "v" is a
>  64-bit double.  Most people assume it is a 32-bit float.
>
>  The largest number that can be represented by a 64-bit IEEE 754
>  number is 309 digits long.  Add one character for the decimal
>  point, six more for trailing zeros, and one for the terminator
>  character.  Making the number negative adds one more character
>  in front.
>
>  The binary value is 0xFFEFFFFFFFFFFFFF.  The value written into the
>  buffer is:
>
>  -1797693134862315708145274237317043567980705675258
>  44996598917476803157260780028538760589558632766878
>  17154045895351438246423432132688946418276846754670
>  35375169860499105765512820762454900903893289440758
>  68508455133942304583236903222948165808559332123348
>  27479782620414472316873817718091929988125040402618
>  4124858368.000000[\0]
>
>
>  This kind of thing is exactly why so much software is full of
>  buffer overruns.
>
>   -j
>
> ------------------------------------------------------------
> #include <stdio.h>
> #include <string.h>
>
> int main( int argc, char** argv)
> {
>        long long int   i = 0xFFEFFFFFFFFFFFFFLL;
>        double          *p = (double*)&i;
>        char            b[512];
>
>        snprintf( b, sizeof(b), "%f", *p );
>        printf( "%d\n%s\n", strlen( b )+1, b );  /* add one for \0 */
> }
> ------------------------------------------------------------
>
>
>
>   -j
>
> --
> Jay A. Kreibich < J A Y  @  K R E I B I.C H >
>
> "Intelligence is like underwear: it is important that you have it,
>  but showing it to the wrong people has the tendency to make them
>  feel uncomfortable." -- Angela Johnson
>
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to