473,626 Members | 3,480 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 6275
On 1 Aug 2004 09:11:52 -0700, Kerry <te************ ****@hotmail.co m> 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
179769313486231 570814527423731 704356798070567 525844996598917 47680
315726078002853 876058955863276 687817154045895 351438246423432 13268
894641827684675 467035375169860 499105765512820 762454900903893 28944
075868508455133 942304583236903 222948165808559 332123348274797 82620
414472316873817 718091929988125 040402618412485 8368

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************ *************@p osting.google.c om>,
Old Wolf <ol*****@inspir e.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
implementati on 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
17976931348623 157081452742373 170435679807056 752584499659891 747680
31572607800285 387605895586327 668781715404589 535143824642343 213268
89464182768467 546703537516986 049910576551282 076245490090389 328944
07586850845513 394230458323690 322294816580855 933212334827479 782620
41447231687381 771809192998812 504040261841248 58368

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,7 40,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,7 40,992 or 9,007,199,254,7 40,994, but not 9,007,199,254,7 40,993.
Jul 22 '05 #4
In message <84************ *************@p osting.google.c om>, Old Wolf
<ol*****@inspir e.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
implementati on 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
17976931348623 157081452742373 170435679807056 752584499659891 747680
31572607800285 387605895586327 668781715404589 535143824642343 213268
89464182768467 546703537516986 049910576551282 076245490090389 328944
07586850845513 394230458323690 322294816580855 933212334827479 782620
41447231687381 771809192998812 504040261841248 58368

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
1649
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 pause(){ unsigned long i=0; while(i++<10000000); } int main(){ float i;
10
17150
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
12811
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 last):
6
7606
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, Karthi
8
1657
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 = 0; j < 250; j++) {
7
14959
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 post, that double should be preferred to float when space is not a constraint. Can someone explain why double should be preferred to float ?
8
3701
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
2080
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 i=1; cout << ++(i++); Output: Error. LValue required
0
8205
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8713
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8644
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8370
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8514
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6126
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4094
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4208
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1516
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.