Number of characters required for FLT_MAX

A

artifact.one

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
 
J

Jens Thoms Toerring

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
 
A

artifact.one

Do you mean via '%f'?

Well, yes.
Which 'size' parameter?

Sorry, that was a typo. I meant snprintf().
'(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
 
J

Jens Thoms Toerring

#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
 
A

artifact.one

'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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top