How do I increment a float point num

K

Kerry

Let's say I have float num = 1.1 and have float increment = 0.1 ,
num +=increment doesn't compute exactly to 1.2 ... is there a way to
make it equal exactly to 1.2?
 
J

John Harrison

Let's say I have float num = 1.1 and have float increment = 0.1 ,
num +=increment doesn't compute exactly to 1.2 ... is there a way to
make it equal exactly to 1.2?

No, for the simple reason that no floating point number exists which
equals one and one fifth. In exactly the same way there is no floating
point number that exactly equals one and one tenth, or any floating point
number that exactly equals one tenth.

Some floating point numbers are exact. Any integral number is exact, as
are fractions like one half, one quarter, one eighth, and multiples
thereof; but tenths you can just forget it, they are inherently
inaccurate. If you need exact artihmetic with tenths then the best way is
to use integers that are ten times bigger than the numbers you actually
need, and then divide by 10 as the last step.

john
 
O

Old Wolf

John Harrison said:
Some floating point numbers are exact. Any integral number is exact,

I thought this was implementation-dependent. On the only floating point
implementation I've ever looked at closely, integral numbers were not
exact unless they were powers of two. (They were stored as 0 <= m < 1
and integer e, and the value was m * 2^e)

Also, some a-priori reasoning: on my system DBL_MAX is
17976931348623157081452742373170435679807056752584499659891747680
31572607800285387605895586327668781715404589535143824642343213268
89464182768467546703537516986049910576551282076245490090389328944
07586850845513394230458323690322294816580855933212334827479782620
4144723168738177180919299881250404026184124858368

If integral numbers were exact then 'double' would have to have
1024 bits (approximately), but on my system, 'double' is 64 bits.
 
J

John Cochran

I thought this was implementation-dependent. On the only floating point
implementation I've ever looked at closely, integral numbers were not
exact unless they were powers of two. (They were stored as 0 <= m < 1
and integer e, and the value was m * 2^e)

Also, some a-priori reasoning: on my system DBL_MAX is
17976931348623157081452742373170435679807056752584499659891747680
31572607800285387605895586327668781715404589535143824642343213268
89464182768467546703537516986049910576551282076245490090389328944
07586850845513394230458323690322294816580855933212334827479782620
4144723168738177180919299881250404026184124858368

If integral numbers were exact then 'double' would have to have
1024 bits (approximately), but on my system, 'double' is 64 bits.

Let me rephrase Mr Harrison's answer.

Any integral number that can be represented in no more bits than the
mantissa is represented exactly. For IEEE double precision with 64 bits,
the mantissa is 53 bits long, it is capable of representing exactly any
integer value that has an absolute value less than or equal to 2^53 - 1
or 9,007,199,254,740,991 for any value larger than that, the value stored
is exact. However, not all values may be stored. For example, you may store
9,007,199,254,740,992 or 9,007,199,254,740,994, but not 9,007,199,254,740,993.
 
R

Richard Herring

Old Wolf said:
I thought this was implementation-dependent. On the only floating point
implementation I've ever looked at closely, integral numbers were not
exact unless they were powers of two. (They were stored as 0 <= m < 1
and integer e, and the value was m * 2^e)

That's an unusual representation. Many FP formats save a bit by not
storing the leading 1 of the fraction (and treat zero as a special
case), so 1 <= m < 2 or 1/2 <=m < 1, depending where you put the point.

But it still doesn't rule out exact non-powers-of-two. For example, 3
can be stored exactly as m = 3/4 (binary 1100...0), e = 2.
Also, some a-priori reasoning: on my system DBL_MAX is
17976931348623157081452742373170435679807056752584499659891747680
31572607800285387605895586327668781715404589535143824642343213268
89464182768467546703537516986049910576551282076245490090389328944
07586850845513394230458323690322294816580855933212334827479782620
4144723168738177180919299881250404026184124858368

If integral numbers were exact then 'double' would have to have
1024 bits (approximately),

only if every integer less than DBL_MAX could also be stored exactly.
They can't.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top