Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > reading float point from file

Reply
Thread Tools

reading float point from file

 
 
Affan Syed
Guest
Posts: n/a
 
      12-03-2004
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


 
Reply With Quote
 
 
 
 
Affan Syed
Guest
Posts: n/a
 
      12-03-2004
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" <(E-Mail Removed)> wrote in message
news:coojee$h4p$(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
 
 
 
Jacek Dziedzic
Guest
Posts: n/a
 
      12-03-2004
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.
 
Reply With Quote
 
roberth+news@ifi.uio.no
Guest
Posts: n/a
 
      12-03-2004
Affan Syed <(E-Mail Removed)> 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
 
Reply With Quote
 
Affan Syed
Guest
Posts: n/a
 
      12-03-2004
Just as you said and i have already posted.. I can read in the value
correctly.
Affan
"Jacek Dziedzic" <(E-Mail Removed)> wrote in message
news:cookcu$rq2$(E-Mail Removed)...
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.


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      12-03-2004
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Arijit
Guest
Posts: n/a
 
      12-03-2004
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 )
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
Scenario 5: IS-IS routing on Frame Relay Multi-point and Point-to-Point David Sudjiman Cisco 0 06-08-2006 09:11 AM
need code to convert float format to internal java float format which is kept in 4 bytes integer Andy Java 7 05-10-2004 09:26 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments