c++ float comparison

Discussion in 'Programming & Software Development' started by jars121, Dec 14, 2017.

  1. jars121

    jars121 Member

    Joined:
    Mar 6, 2008
    Messages:
    1,758
    Location:
    Sydney
    Before you get riled up, I know, floats and comparisons don't go well together. I have a particular use case, for which I'm unsure of the most efficient/standard approach.

    I have a configuration .ini file, which stores floats (among other things) for a range of parameters. The format for which is as follows:

    [section_1]
    parameter1 = 5.5
    parameter2 = 0
    parameter3 = 0

    I read each of these parameters into variables in c++ for use throughout my application.

    My issue arises when I check to see if a parameter contains a valid number; i.e. if (parameter2 != 0), which of course fails as 0 isn't always exactly 0.

    My question, how should I store parameters in my .ini file if they're not used? Should the value for that key be left empty (e.g. parameter2 = )? Can I use a character instead (e.g. '-') and check if the parameter is NaN? I'm after an approach which is platform and compiler agnostic if possible.

    Cheers
     
  2. waltermitty

    waltermitty Member

    Joined:
    Feb 19, 2016
    Messages:
    639
    Location:
    BRISBANE
    Use json or yaml or xml or friggin anything is better than INIs
     
  3. Luke212

    Luke212 Member

    Joined:
    Feb 26, 2003
    Messages:
    9,309
    Location:
    Sydney
    i mean a string check for '0' would work but you should probably leave them empty to me more correct.

    why? just more overhead and you still have the same problem.
     
  4. OP
    OP
    jars121

    jars121 Member

    Joined:
    Mar 6, 2008
    Messages:
    1,758
    Location:
    Sydney
    I use json where needed, but I don't see the issue with using a .ini for such basic, lightweight content.

    I'll give empty values a go and see how it performs.
     
  5. theSeekerr

    theSeekerr Member

    Joined:
    Jan 19, 2010
    Messages:
    2,706
    Location:
    Prospect SA
    Whilst I would never rely on a calculation returning exactly 0, in the case of reading a value straight into a float, 0 can be represented precisely.

    Indeed, for a single precision float, any integer in the range +/-16,777,216 can be represented exactly, and for a double, anything in the range +/-9,007,199,254,740,992 - the maximum integer that can be accurately represented is 2^(# mantissa bits + 1).
     
  6. OP
    OP
    jars121

    jars121 Member

    Joined:
    Mar 6, 2008
    Messages:
    1,758
    Location:
    Sydney
    That was my initial thought, but this is the result of a qDebug() on a variable which has read a '0' float value from the .ini key (multiple iterations):

    7.17465e-42
    -8.79896e+20
    -8.79896e+20
    -8.79896e+20
    -8.79896e+20
    -8.79896e+20
    0
    2.24208e-44
    -8.79896e+20
    -8.79896e+20
    -8.79896e+20
    -8.79896e+20
    -8.79896e+20
    -8.79896e+20
    -8.79896e+20
     
  7. theSeekerr

    theSeekerr Member

    Joined:
    Jan 19, 2010
    Messages:
    2,706
    Location:
    Prospect SA
    The fact that you're often getting negative 879 quintillion is probably a hint that you're not looking at a floating-point representation issue.
     
  8. OP
    OP
    jars121

    jars121 Member

    Joined:
    Mar 6, 2008
    Messages:
    1,758
    Location:
    Sydney
    Well that's embarrassing. Turns out the parameter I've been having issues with was never actually initialised, so I'd say you're onto something! I've initialised it now and it reads a null value as 0 as one would expect :)
     

Share This Page