Click to See Complete Forum and Search --> : How to detect overflow?


jshyus
July 16th, 2002, 09:12 AM
I am using atof function to convert a string into a double. The return value from atof is undefined for overflow. So how can I detect if the number is too large? There are no error messages given and I cannot tell from the return value.

cup
July 16th, 2002, 09:22 AM
It really depends on what OS you are using.

atof converts to floats, strtod converts to doubles. On some implementations, doubles have more precision but the same magnitude. On other implementations (eg MS) doubles have more precision and a bigger magnitude.

If you are using MS, and it is an overflow, it returns +/-HUGE_VAL. If you are not using MS, perhaps you could tell us which compiler and OS you are using before we start giving you suggestions that may or may not work.

Anthony Mai
July 16th, 2002, 09:32 AM
The string that is provided to atof() must have come from one or two sources:

1.The original string was converted to string by the computer from a valid, normal float or double. So there is no possibility of overflow.

2.The original string is entered into the computer by a human. Since humen are much more likely to make mistake than machines, it is possible you get an overflow. In this case, just convert the double back to a string, output the new string back to screen to let the user verify that that is what he/she meant to be.

Actually in any machinery UI design, any human input needs to be reflected back so human user can verify the input to make sure it is correct. Have you used an automatic answer system using a touch tone phone? You entered your credit card number and the machine will read it back to you to let you verify if it is correct or not.

jshyus
July 16th, 2002, 10:00 AM
I am using win2k visual c++ 6.0.

I tried the following:
input string 100000000000000:
atoi result: 276,447,232

input string 2.0e1000
atof result: HUGE_VAL

input string: 2.0e-1000
atof result: 0.0

Has anybody used function "statusfp"? Can I use this function to tell if overflow has happened?

cup
July 16th, 2002, 11:49 AM
I've only used _statusfp on calculations: never in strtod/atof. It may not give you what you are looking for.

If the user is likely to fall asleep on the 0 after typing in the number half way, it is probably best to write your own parser for floating point values.

Bob Davis
July 16th, 2002, 12:27 PM
I am using win2k visual c++ 6.0.

I tried the following:
input string 100000000000000:
atoi result: 276,447,232

input string 2.0e1000
atof result: HUGE_VAL

input string: 2.0e-1000
atof result: 0.0


I'm not sure if you were wondering this or not, but the third case that you point out is not overflow. Overflow is the case in which you have a number that is too large in absolute value to represent in the amount of bits you have. The third case is a lack of sufficient precision to represent the number. The floating-point representation is only good to a certain amount of significant digits. 2.0e-1000 is for all intents and purposes zero anyway, so you're not really losing anything in this case. The precision depends on the platform you're coding on, so you may want to look this up to see exactly what you can and cannot represent with a float.