Velocity Reviews > C++ > Doing the 0.1-trick in C++

# Doing the 0.1-trick in C++

Stefan Ram
Guest
Posts: n/a

 03-19-2013
SG <(E-Mail Removed)> writes:
>_exact_ value of a floating point number in decimal. Why? I guess it's
>just for educational purposes ...

Yes.

SG
Guest
Posts: n/a

 03-19-2013
Am 19.03.2013 10:46, schrieb Juha Nieminen:
> SG <(E-Mail Removed)> wrote:
>> representations that are close enough.

>
> If we are talking about exact representation, then trying to print the
> value in base-10 is completely moot because not all IEEE double-precision
> floating point values can be represented exactly in base-10.

That's not true. There is always an exact decimal representation with a
finite number of digits. That's because 2 (the radix of such a floating
point number representation) is a factor of 10 (the radix of decimal
representations). It may be very long, but there is one such
representation. You might find hints for this in my other answers.
Check'em out and think about it a little more. If you have trouble
figuring it let me know.

Bart van Ingen Schenau
Guest
Posts: n/a

 03-20-2013
On Wed, 20 Mar 2013 08:44:52 +0000, Juha Nieminen wrote:

> SG <(E-Mail Removed)> wrote:
>>> If we are talking about exact representation, then trying to print the
>>> value in base-10 is completely moot because not all IEEE
>>> double-precision floating point values can be represented exactly in
>>> base-10.

>>
>> That's not true. There is always an exact decimal representation with a
>> finite number of digits. That's because 2 (the radix of such a floating
>> point number representation) is a factor of 10 (the radix of decimal
>> representations). It may be very long, but there is one such
>> representation. You might find hints for this in my other answers.
>> Check'em out and think about it a little more. If you have trouble
>> figuring it let me know.

>
> Or you could simply print if with "%a" and get a compact accurate ascii
> representation of the number which is also portable and readable back.

And how does that hexadecimal representation make it clear that the
decimal fraction 1/10 can not be accurately stored in a floating point
variable that uses binary fractions?

Bart v Ingen Schenau