473,499 Members | 1,909 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I increment a float point num

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?
Jul 22 '05 #1
4 6259
On 1 Aug 2004 09:11:52 -0700, Kerry <te****************@hotmail.com> wrote:
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
Jul 22 '05 #2
"John Harrison" <jo*************@hotmail.com> wrote:

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
17976931348623157081452742373170435679807056752584 499659891747680
31572607800285387605895586327668781715404589535143 824642343213268
89464182768467546703537516986049910576551282076245 490090389328944
07586850845513394230458323690322294816580855933212 334827479782620
4144723168738177180919299881250404026184124858368

If integral numbers were exact then 'double' would have to have
1024 bits (approximately), but on my system, 'double' is 64 bits.
Jul 22 '05 #3
In article <84*************************@posting.google.com> ,
Old Wolf <ol*****@inspire.net.nz> wrote:
"John Harrison" <jo*************@hotmail.com> wrote:

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
1797693134862315708145274237317043567980705675258 4499659891747680
3157260780028538760589558632766878171540458953514 3824642343213268
8946418276846754670353751698604991057655128207624 5490090389328944
0758685084551339423045832369032229481658085593321 2334827479782620
41447231687381771809192998812504040261841248583 68

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.
Jul 22 '05 #4
In message <84*************************@posting.google.com> , Old Wolf
<ol*****@inspire.net.nz> writes
"John Harrison" <jo*************@hotmail.com> wrote:

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)


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
1797693134862315708145274237317043567980705675258 4499659891747680
3157260780028538760589558632766878171540458953514 3824642343213268
8946418276846754670353751698604991057655128207624 5490090389328944
0758685084551339423045832369032229481658085593321 2334827479782620
41447231687381771809192998812504040261841248583 68

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.
but on my system, 'double' is 64 bits.


--
Richard Herring
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
1640
by: puzzlecracker | last post by:
If you run this program, it will give very unexpected results. Can anyone explain the nature of this anamaly? (also what is the function call to execute 'pause'); #include<stdio.h> //void...
10
17102
by: riteshtijoriwala | last post by:
Anyone has any idea on why is there no post/pre increment operators in python ? Although the statement: ++j works but does nothing
15
12797
by: Kay Schluehr | last post by:
I wonder why this expression works: >>> decimal.Decimal("5.5")**1024 Decimal("1.353299876254915295189966576E+758") but this one causes an error 5.5**1024 Traceback (most recent call...
6
7591
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
8
1641
by: Sheldon | last post by:
Hi, I have a problem that and the answer eludes me: I am converting a 2D array to a 1D array within a for loop that looks something like this: ii=0; for (i = 0; i < 300; i++) { for (j =...
7
14950
by: subramanian100in | last post by:
In the post with heading "Learning C - Scanf or Getch, or Getchar not working correctly after first loop" that appears in today's list in comp.lang.c, it has been mentioned in the answer to this...
8
3689
by: Gernot Frisch | last post by:
Uhm... Is there any way of making float pos; a union of float x,y,z; somehow, so I can use: mything = mything.y;
8
2066
by: bintom | last post by:
Why doe the following C++ code behaves as it does? int i; i=1; cout << (++i)++; Output: 2 i=1; cout << ++(++i); Output: 3 i=1; cout << (i++)++; Output: Error. LValue required...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.