CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2001
    Posts
    16

    How to detect overflow?

    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.

  2. #2
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    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.
    Succinct is verbose for terse

  3. #3
    Join Date
    Jun 1999
    Location
    San Diego, CA
    Posts
    600
    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.

  4. #4
    Join Date
    Sep 2001
    Posts
    16
    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?

  5. #5
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    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.
    Succinct is verbose for terse

  6. #6
    Join Date
    Jan 2001
    Posts
    588
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured