why doesn't it print 1.0?

J

jwala

can anybody explain me plz, why this code doesn't print 1.0??

#include <stdio.h>
void main(){
float x;
for (x=0;x<=1.0;x+=0.1)
printf("%0.1f ",x);
}
 
R

RoRsOaMrPiEo

can anybody explain me plz, why this code doesn't print 1.0??

because at 'end-1' of 'for', here in my PC 1.0f - x == -0.0f != 0.0f ?
#include <stdio.h>
void main(){
float x;
for (x=0;x<=1.0;x+=0.1)
printf("%0.1f ",x);
}


This seems ok:

#include <stdio.h>
#include <float.h>

int main(void)
{float x;
for(x=0.0f; x <= 1.0f + FLT_EPSILON; x += 0.1f)
printf("%1.1f ", x);
printf("\n");
return 0;
}
 
P

Peter Nilsson

jwala said:
can anybody explain me plz, why this code doesn't print 1.0??

#include <stdio.h>
void main(){

int main(){
float x;
for (x=0;x<=1.0;x+=0.1)
printf("%0.1f ",x);

putchar('\n');
return 0;

If the last line of output isn't terminated with a newline character, the
standard offers no guarantee that your program will output anything.

But to answer your question, you need to do some basic research on how
floating point numbers are implemented. The output of the following program
on my system should give you a clue...

% type float.c
#include <stdio.h>

int main(void)
{
float x;
for (x = 0; x <= 1.1; x += 0.1)
printf("%0.9f\n",x);
return 0;
}

% gcc -ansi -pedantic float.c -o float

% float
0.000000000
0.100000001
0.200000003
0.300000012
0.400000006
0.500000000
0.600000024
0.700000048
0.800000072
0.900000095
1.000000119

%

Floating point computation is generally not exact.
 
M

Malcolm

jwala said:
can anybody explain me plz, why this code doesn't print 1.0??

#include <stdio.h>
void main(){
float x;
for (x=0;x<=1.0;x+=0.1)
printf("%0.1f ",x);
}
0.1 cannot be represented exactly in typical binary exponent-mantissa
floating point format. On your machine it happens that the error has
overflowed slightly above 1.0, so the last number is never printed.
 
R

Russell Hanneken

jwala said:
can anybody explain me plz, why this code doesn't print 1.0??

#include <stdio.h>
void main(){
float x;
for (x=0;x<=1.0;x+=0.1)
printf("%0.1f ",x);
}

I agree with the other answers you've received in this thread. I'll
just add that I think it's best to avoid comparing floating-point
values. You could rewrite your program like this:

#include <stdio.h>

int main(void) {
int x;
for (x = 0; x <= 10; ++x)
printf("%0.1f ", x / 10.0);
fflush(stdout);
return 0;
}

This should give you the output you're expecting.
 
P

Peter Nilsson

Russell Hanneken said:
I agree with the other answers you've received in this thread. I'll
just add that I think it's best to avoid comparing floating-point
values. You could rewrite your program like this:

#include <stdio.h>

int main(void) {
int x;
for (x = 0; x <= 10; ++x)
printf("%0.1f ", x / 10.0);
fflush(stdout);

The explicit flushing of stdout is redundant since stdout will be flushed on
termination. In any case, it doesn't fix the inherent problem...

7.19.2p2: "A text stream is an ordered sequence of characters composed into
lines, each line consisting of zero or more characters plus a terminating
new-line character. Whether the last line _requires_ a terminating new-line
character is implementation-defined. ..." [Emphasis is mine.]
 
L

Liux

jwala said:
can anybody explain me plz, why this code doesn't print 1.0??

#include <stdio.h>
void main(){
float x;
for (x=0;x<=1.0;x+=0.1)
printf("%0.1f ",x);
}
Please remember that the float or double is not always exactly enough. In
this case, the value after 0.9 is around 1.0 and a little bigger.
If changed to "double x" or "x <= 1.00005" in my system ( 80x86 and turbo c
3.0 ) , 1.0 is printed out.
-Rocky
 
M

Martin Ambuhl

jwala said:
can anybody explain me plz, why this code doesn't print 1.0??

#include <stdio.h>
void main(){
float x;
for (x=0;x<=1.0;x+=0.1)
printf("%0.1f ",x);
}

In this short piece of code, you demonstrate at least three times that
you have violated the norms of behavior for posting. Had you followed
the newsgroup, checked the archives, or checked the FAQs before posting,
you would have known that main returns an int, that floating point
arithmetic is inexact for all but numbers whose denominators are powers
of 2, that the behavior of programs without an end-of-line character at
the end of the last output line isn't portably defined, and that in the
prevailing C89/C90 standard, a return value from main should be supplied.

Try running this:
#include <stdio.h>
#include <float.h>

int /* NOT 'void' */ main(void)
{
float x;
for (x = 0; x <= 1.0; x += 0.1) ;
printf("%0.*g %0.*g\n", FLT_DIG, x, FLT_DIG, x - 1);
return 0;
}


[on my implementation]
1 1.19209e-07
 
C

Christian Bau

RoRsOaMrPiEo said:
because at 'end-1' of 'for', here in my PC 1.0f - x == -0.0f != 0.0f ?

Every time you execute "x += 0.1", x is set to a floating point number
which is relatively close to x + 0.1, but not exactly equal to it. The
reason is that C uses binary floating point arithmetic and not decimal
floating point arithmetic (but with decimal floating point arithmetic,
you would have the same problem if you wrote x += 1.0 / 12.0).

So after executing x += 0.1 ten times, the result is that x is a number
that is close to 1.0, it may be equal be coincidence, or may be a little
bit smaller or larger. If it is a little bit larger, then 1.0 will not
be printed. That seems to be what is happening to you.
This seems ok:

#include <stdio.h>
#include <float.h>

int main(void)
{float x;
for(x=0.0f; x <= 1.0f + FLT_EPSILON; x += 0.1f)
printf("%1.1f ", x);
printf("\n");
return 0;
}

Pure coincidence. After adding 0.1 ten times, the rounding error could
very well be more than FLT_EPSILON.
 
R

RoSsiacriilo

Pure coincidence. After adding 0.1 ten times, the rounding error could
very well be more than FLT_EPSILON.

This seems ok:

#include <stdio.h>
#include <float.h>

int main(void)
{float x, epsl=FLT_EPSILON;
for(x=0.0f; x <= 1.0f + epsl; x += 0.1f, epsl = epsl + epsl)
printf("%f ", x);
printf("\nOut for: x=%f epsl=%f\n", x, epsl);
return 0;
}
 
D

Dik T. Winter

> This seems ok:

Try it with the slight modifications I make:
>
> #include <stdio.h>
> #include <float.h>
>
> int main(void)
> {float x, epsl=FLT_EPSILON;
> for(x=0.0f; x <= 1.0f + epsl; x += 0.1f, epsl = epsl + epsl)
change to:
for(x=0.0f; x <= 3.0f + epsl; x += 0.1f, epsl = epsl + epsl)
> printf("%f ", x);
change to:
printf("%f\n", x);
> printf("\nOut for: x=%f epsl=%f\n", x, epsl);
> return 0;
> }

Explain what you are seeing (and be ready to interrupt the program).
 
R

RoRsOaMrPiEo

Try it with the slight modifications I make:
change to:
for(x=0.0f; x <= 3.0f + epsl; x += 0.1f, epsl = epsl + epsl)
Explain what you are seeing (and be ready to interrupt the program).

for j=20 epsl > 0.1f so "x <= 3.0 + epsl" always ?

#include <stdio.h>
#include <float.h>

int main(void)
{float x, epsl=FLT_EPSILON, j;
for(x=0.0f, j=0; x <= 3.0f+epsl; x += 0.1f, epsl = epsl+epsl, ++j)
{printf("%f ", x);
if(j==20) break;
}
printf("\nOut for: x=%f epsl=%f\n", x, epsl);
return 0;
}

C:\Documents and Settings\Ros\Desktop\art>c_b
0.000000 0.100000 0.200000 0.300000 0.400000 0.500000 0.600000
0.700000 0.800000
0.900000 1.000000 1.100000 1.200000 1.300000 1.400000 1.500000
1.600000 1.70000
0 1.800000 1.900000 2.000000
Out for: x=2.000000 epsl=0.125000
 
R

RoRsOaMrPiEo

#include <stdio.h>
#include <float.h>

int main(void)
{float x, epsl=FLT_EPSILON, j;
ok, ok int j; ^^^^^
If j is float, is it possible that j!=20 always? In that case can I
write ++j?
 

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

No members online now.

Forum statistics

Threads
474,310
Messages
2,571,602
Members
48,419
Latest member
EstelaCout

Latest Threads

Top