Re: ifstream bug in VS2005 Beta2 & Platform SDK

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



> But if someone wrote some number into some media, this
> would be reasonable to expect this data to be read exacly as it was
written.

It is not reasonable and cannot be done.
You know the number 1/3?
Written in decimal it is 0.33333333333333333... forever.
It is said to "recur".
No matter how many places you chop the number of digits off at, it will
never represent 1/3.

And it works out that in any base, only fractions that are multiple of the
divisors of the base are exact.
So mulitples of 1/5 and 1/2 are exact in base 10 because 5 and 2 divide into
10 exactly.
And also powers and combinations. So 3/25 and 7/40 are exact.

Coming back to float's and double's:
Just about all languages use IEEE as the representation.
And that is binary format - so base 2
And that means in base 2 only fractions that are multiples and powers of 1/2
are exact.
So 1/10 or 0.1 is not, it recurs, and cannot be represented exactly as a
float or double.
What you have is a binary approximation, the closest binary approximation to
1/10.
And when you write a float or double to cout, you are converting from a base
2 representation that is exact to a base 10 textual representation. In this
conversion, with unlimited precision, it is always exact because 2 divides
into 10. But converting the other way is inexact. Not all base 10 numbers
have a base 2 representation because of the chopping-when-recurring problem.

Stephen Howe


.



Relevant Pages

  • Re: The spinoza papers: design of the extra-precision number object 2
    ... doesnt terminate, which cause practical problems, e.g in representation ... that we have an exact number. ... Limiting the precision doesn't change this claim. ... including quantum uncertainty represented as ...
    (comp.programming)
  • Re: Why cant I xor strings?
    ... a representation that does have parts and order etc. ... It's a matter of interpretation. ... I said they do represent _exact_ values, ... >who operates with "floats represent a value and have an error range around ...
    (comp.lang.python)
  • Re: Converting from Microsoft Binary Format floats to Python Float
    ... >floating point numbers, In a previous thread, Bengt Richter posted ... >that for one of the examples, the conversion isn't exact. ... not seeing a full representation of the two values. ...
    (comp.lang.python)
  • Re: Hexadecimal Numbers
    ... My problem comes where I am converting decimal to hexadecimal. ... Di are integers between 0 and B-1 inclusive, called the digits. ... We can identify this form to the quotient of N divided by B: ... Notice that we don't convert directly from a representation in base B1 ...
    (comp.programming)
  • Re: Real value of c = 299733463.58589089 ?
    ... The value of pi in base pi is 10 and the exact value of the base is pi, ... Pedant point: ... is not a normalized representation (much like one would ... If you know you are an idiot, and we know you are an idiot, and each ...
    (sci.physics)