reading float point from file

Discussion in 'C++' started by Affan Syed, Dec 3, 2004.

  1. Affan Syed

    Affan Syed Guest

    Hi,
    the file that i read has the following column as input:

    1.000005
    1.00001
    1.000015
    1.00002
    1.000025

    Now I have float variable skew.
    When i read in from the file as inFile >>skew;
    I read in following values:
    Skew read= 1.00001
    Skew read= 1.00001
    Skew read= 1.00002
    Skew read= 1.00002

    So it rounds up to the 5th decimal position . Why? I know it might have
    something to do with the insuffereable precision of float points. But is
    there some easy way of gettin to read the correct value from the file and
    not go to BCD?
    Similarly when i do
    float x= skew-1,
    I get something which is not nearly like 0.00001, but a close approximation.
    Again how can i get the correct value?

    Thanks
    Affan
    Affan Syed, Dec 3, 2004
    #1
    1. Advertising

  2. Affan Syed

    Affan Syed Guest

    actually the reading from file was okay... it read in the right value... but
    when i debug it .. it just showed uptil a precision of 7 decimal points.. so
    when i changed the precision of cout to 8, it printed the correctly rounded
    values... So that is solved... however.. how do i do the maths with it...
    as simple as subtracting one form it!!!?

    regards
    Affan
    "Affan Syed" <> wrote in message
    news:coojee$h4p$...
    > Hi,
    > the file that i read has the following column as input:
    >
    > 1.000005
    > 1.00001
    > 1.000015
    > 1.00002
    > 1.000025
    >
    > Now I have float variable skew.
    > When i read in from the file as inFile >>skew;
    > I read in following values:
    > Skew read= 1.00001
    > Skew read= 1.00001
    > Skew read= 1.00002
    > Skew read= 1.00002
    >
    > So it rounds up to the 5th decimal position . Why? I know it might have
    > something to do with the insuffereable precision of float points. But is
    > there some easy way of gettin to read the correct value from the file and
    > not go to BCD?
    > Similarly when i do
    > float x= skew-1,
    > I get something which is not nearly like 0.00001, but a close
    > approximation. Again how can i get the correct value?
    >
    > Thanks
    > Affan
    >
    Affan Syed, Dec 3, 2004
    #2
    1. Advertising

  3. U¿ytkownik Affan Syed napisa³:
    > Hi,
    > the file that i read has the following column as input:
    >
    > 1.000005
    > 1.00001
    > 1.000015
    > 1.00002
    > 1.000025
    >
    > Now I have float variable skew.
    > When i read in from the file as inFile >>skew;
    > I read in following values:
    > Skew read= 1.00001
    > Skew read= 1.00001
    > Skew read= 1.00002
    > Skew read= 1.00002
    >
    > So it rounds up to the 5th decimal position . Why? I know it might have
    > something to do with the insuffereable precision of float points. But is
    > there some easy way of gettin to read the correct value from the file and
    > not go to BCD?
    > Similarly when i do
    > float x= skew-1,
    > I get something which is not nearly like 0.00001, but a close approximation.
    > Again how can i get the correct value?
    >
    > Thanks
    > Affan


    I'd bet that your numbers are in fact read in correctly,
    but only rounded on output. What is the precision of your
    output?

    HTH,
    - J.
    Jacek Dziedzic, Dec 3, 2004
    #3
  4. Affan Syed

    Guest

    Affan Syed <> wrote:
    | Hi,
    | the file that i read has the following column as input:

    | 1.000005
    | 1.00001
    | 1.000015
    | 1.00002
    | 1.000025

    | Similarly when i do
    | float x= skew-1,
    | I get something which is not nearly like 0.00001, but a close approximation.

    Floats are inaccurate in nature, and operations on them will usually
    generate round off errors. There are certain tricks though:

    * Use integers. Integer operations are accurate. It's just a matter of
    placing the decimal separator correctly on output. However useful, this
    requires very low limits on the values.

    * Write 'abs(a - b) < eps' in tests rather than 'a == b' if you want to
    check for equality between a and b. eps must be a sufficiently small
    floating point value.

    * Don't subtract quantities of almost equal size as part of a larger
    calculation. Rather try to rewrite the expression to avoid such things.

    * Use types with more precision. The double type should often be
    preferred over float.

    For your example: If skew is generally "1.000...x", modify the string to
    0.000...x before converting to float.

    | Again how can i get the correct value?

    You can't.
    --
    Robert Bauck Hamar
    , Dec 3, 2004
    #4
  5. Affan Syed

    Affan Syed Guest

    Just as you said and i have already posted.. I can read in the value
    correctly.
    Affan
    "Jacek Dziedzic" <> wrote in message
    news:cookcu$rq2$...
    U¿ytkownik Affan Syed napisa³:
    > Hi,
    > the file that i read has the following column as input:
    >
    > 1.000005
    > 1.00001
    > 1.000015
    > 1.00002
    > 1.000025
    >
    > Now I have float variable skew.
    > When i read in from the file as inFile >>skew;
    > I read in following values:
    > Skew read= 1.00001
    > Skew read= 1.00001
    > Skew read= 1.00002
    > Skew read= 1.00002
    >
    > So it rounds up to the 5th decimal position . Why? I know it might have
    > something to do with the insuffereable precision of float points. But is
    > there some easy way of gettin to read the correct value from the file and
    > not go to BCD?
    > Similarly when i do
    > float x= skew-1,
    > I get something which is not nearly like 0.00001, but a close
    > approximation. Again how can i get the correct value?
    >
    > Thanks
    > Affan


    I'd bet that your numbers are in fact read in correctly,
    but only rounded on output. What is the precision of your
    output?

    HTH,
    - J.
    Affan Syed, Dec 3, 2004
    #5
  6. Affan Syed wrote:
    >
    > actually the reading from file was okay... it read in the right value... but
    > when i debug it .. it just showed uptil a precision of 7 decimal points.. so
    > when i changed the precision of cout to 8, it printed the correctly rounded
    > values... So that is solved... however.. how do i do the maths with it...
    > as simple as subtracting one form it!!!?


    Is this a trick question (and please don't top post. Put your reply beneath
    the text you are replying to. Thank you).

    skew = skew - 1.0;
    or
    skew -= 1.0;

    And also note: Use 'float' only if you know what you do, have the knowledge
    the fight that beast, are willing to actually fight that beast and have
    a very, very, very good reason to use 'float'. In all other cases, use 'double'.


    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Dec 3, 2004
    #6
  7. Affan Syed

    Arijit Guest

    Affan Syed wrote:
    > actually the reading from file was okay... it read in the right value... but
    > when i debug it .. it just showed uptil a precision of 7 decimal points.. so
    > when i changed the precision of cout to 8, it printed the correctly rounded
    > values... So that is solved... however.. how do i do the maths with it...
    > as simple as subtracting one form it!!!?
    >
    > regards
    > Affan


    Try this: http://docs.sun.com/source/806-3568/ncg_goldberg.html
    It has everything you probably ever will want to know about floating
    point (and more :) )
    Arijit, Dec 3, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    7
    Views:
    6,243
    Roedy Green
    May 10, 2004
  2. Jim West
    Replies:
    4
    Views:
    1,408
    Jeff Schwab
    Jan 16, 2004
  3. bd
    Replies:
    0
    Views:
    608
  4. Carsten Fuchs
    Replies:
    45
    Views:
    1,509
    James Kanze
    Oct 8, 2009
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,298
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page