By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,848 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

Number of characters required for FLT_MAX

P: n/a
Hello.

Does anybody know of a portable method to determine the maximum
number of characters required to hold an ASCII representation of
FLT_MAX, in 'long' notation (that is, with no exponential notation)?

Ideally, I'd like to be able to do this with a preprocessor macro.

It's actually for passing to sprintf(), but I'd like to pass the
'correct'
value as the 'size' parameter, as opposed to having snprintf() just
truncate the string.

thanks,
MC

Mar 20 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
ar**********@googlemail.com wrote:
Hello.
Does anybody know of a portable method to determine the maximum
number of characters required to hold an ASCII representation of
FLT_MAX, in 'long' notation (that is, with no exponential notation)?
Do you mean via '%f'?
Ideally, I'd like to be able to do this with a preprocessor macro.
It's actually for passing to sprintf(), but I'd like to pass the
'correct'
value as the 'size' parameter
Which 'size' parameter?
>, as opposed to having snprintf() just
truncate the string.
'(int)log(FLT_MAX)+1' should do for the part before the decimal
point. Add as many as needed for the decimal point and digits
following it, as that depends on the format you pass to printf()
(not that it would make any sense to print any, already the in-
teger part of that number will typically have many more digits
than the precision the number is stored with).

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Mar 20 '07 #2

P: n/a
On Mar 20, 8:40 am, j...@toerring.de (Jens Thoms Toerring) wrote:
artifact....@googlemail.com wrote:
Hello.
Does anybody know of a portable method to determine the maximum
number of characters required to hold an ASCII representation of
FLT_MAX, in 'long' notation (that is, with no exponential notation)?

Do you mean via '%f'?
Well, yes.
>
Ideally, I'd like to be able to do this with a preprocessor macro.
It's actually for passing to sprintf(), but I'd like to pass the
'correct'
value as the 'size' parameter

Which 'size' parameter?
Sorry, that was a typo. I meant snprintf().
>
, as opposed to having snprintf() just
truncate the string.

'(int)log(FLT_MAX)+1' should do for the part before the decimal
point. Add as many as needed for the decimal point and digits
following it, as that depends on the format you pass to printf()
(not that it would make any sense to print any, already the in-
teger part of that number will typically have many more digits
than the precision the number is stored with).
Would:

#define FLT_MAX_LEN ((unsigned int)log(FLT_MAX) + 4 + FLT_DIG)

....be a reasonable estimate? If the buffer is too large, that's
not a problem - obviously I don't want it to be too small. The
"+ 3" is for the possible leading '-' character, '.' character
and terminating null.

MC

Mar 20 '07 #3

P: n/a
ar**********@googlemail.com wrote:
Would:
#define FLT_MAX_LEN ((unsigned int)log(FLT_MAX) + 4 + FLT_DIG)
...be a reasonable estimate? If the buffer is too large, that's
not a problem - obviously I don't want it to be too small. The
"+ 3" is for the possible leading '-' character, '.' character
and terminating null.
'FLT_DIG' doesn't make sense - that's the number of digits of
precision of a float, but printf() etc. don't care about that.
If you use just '%f' then replace 'FLT_DIG' by 6, since without
a precision in the format specifier the number of digits after
the point is always this. But if you use e.g. '%.17f' then you
would have to use 17 instead.

But why do you count in a char for a '-'? I can hardly imagine
a system where the largest possible float is negative;-)

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Mar 20 '07 #4

P: n/a
On Mar 20, 9:12 am, j...@toerring.de (Jens Thoms Toerring) wrote:
artifact....@googlemail.com wrote:
Would:
#define FLT_MAX_LEN ((unsigned int)log(FLT_MAX) + 4 + FLT_DIG)
...be a reasonable estimate? If the buffer is too large, that's
not a problem - obviously I don't want it to be too small. The
"+ 3" is for the possible leading '-' character, '.' character
and terminating null.

'FLT_DIG' doesn't make sense - that's the number of digits of
precision of a float, but printf() etc. don't care about that.
If you use just '%f' then replace 'FLT_DIG' by 6, since without
a precision in the format specifier the number of digits after
the point is always this. But if you use e.g. '%.17f' then you
would have to use 17 instead.

But why do you count in a char for a '-'? I can hardly imagine
a system where the largest possible float is negative;-)
Ok.

I have to be honest, I don't know how the stdio functions really
deal with floating point numbers. I hope to eventually write my
own float string formatting function as I need one that's
guaranteed to be realtime safe (and portable, I've trodden on
more than one problematic implementation of snprintf!).

MC

Mar 20 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.