Using While loop



Help for C/C++ for MVS, OS/390 C/C++, z/OS C/C++ and C/C++ Productivity Tools for OS/390

Using While loop

Postby Sabreen Khan » Sat Mar 01, 2008 2:30 pm

float a=10.0;
while (a<=10.5)
{
printf("\nrain drop ");
printf("and whiskers");
a=a+0.1;
}

When i run this program, it should give me an output of 6 lines but it only gives an output of 5 lines. However, when i use double instead of float, i get the expected output i.e 6 lines. Can anyone tell me why?

by the way, im just a beginner in this language.
Sabreen Khan
 
Posts: 4
Joined: Sat Mar 01, 2008 2:22 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Using While loop

Postby dick scherrer » Sat Mar 01, 2008 10:23 pm

Hello Sabreen Khan and welcome to the forums,

by the way, im just a beginner in this language.
I haven't even begun working in "c" yet. . . :)

However, you don't want to use float for small numbers requiring high precision. There are often "problems". . .

Here is something i posted in another forum some time ago:

At the heart of many strange results is one fundamental: floating-point on computers is usually base 2, whereas the external representation is base 10. We expect that 1/3 will not be exactly representable, but it seems intuitive that .01 would be. Not so! .01 in IEEE single-precision format is exactly 10737418/1073741824 or approximately 0.009999999776482582. You might not even notice this difference until you see a bit of code like the following:
REAL X
DATA X /.01/
IF ( X * 100.d0 .NE. 1.0 ) THEN
   PRINT *, 'Many systems print this surprising result. '
ELSE
   PRINT *, 'And some may print this.'
ENDIF


Floating-point arithmetic on digital computers is inherently inexact. The 24 bits (including the hidden bit) of mantissa in a 32-bit floating-point number represent approximately 7 significant decimal digits. Unlike the real number system, which is continuous, a floating-point system has gaps between each number. If a number is not exactly representable, then it must be approximated by one of the nearest representable values.
Because the same number of bits are used to represent all normalized numbers, the smaller the exponent, the greater the density of representable numbers. For example, there are approximately 8,388,607 single-precision numbers between 1.0 and 2.0, while there are only about 8191 between 1023.0 and 1024.0.

On any computer, mathematically equivalent expressions can produce different values using floating-point arithmetic. In the following example, Z and Z1 will typically have different values because (1/Y) or 1/7 is not exactly representable in binary floating-point.



If you'd like a lot more explanation/examples of how floating point arithmetic differs from decimal arithmetic, check out
http://www.lahey.com/float.htm which is where the above info came from.

Good luck.
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times


Return to C, C++