1. ## Floating point problem

hey i am a complete newbie to c.

I am having this problem with floating points and the output is producing really wired results
I'm using gcc to compile it

Code:
```float i=0.0;
for(;i<=20.0;i+=0.1)
{
if(i==0.0 || ((int)(i*10))&#37;10==0)
{
printf("\n");
printf("%5.1f ",i);
}

printf("%5.2f ",sqrt(i));
}```
the above code is to print a table of square roots in a sort of tabled layout.
when we start it prints 0.0 and print the list of sqrt from 0.1 to 0.9
when 1.0 it again goes to a new line prints 1.0 as well as the sqrts from 1.0 to 1.9
but it creates a problem in 3.0 in my machine for some reason. Please try it out.
It's really strange

Regards

2. And what strange problem is that? Care to enlighten me?

Usually 0.0 = double, append a f on the end if you mean float, ie 0.0f

Code:
```float i = 0.0f;
for(; i <= 20.0f; i += 0.1)
{
if(i == 0.0f || ((int)(i * 10.0f))&#37;10 == 0)
printf("\n%5.1f ",i);

printf("%5.2f ", sqrt(i));
}```

3. > if(i==0.0
You can't compare floats for equality. They are approximations, which means not all (in fact most) numbers are not represented accurately, only the nearest representable value.

What follows is that while ( 1.0 / 3.0 ) * 3.0 might be 1 (mathematically), it doesn't follow that the floating point representation of that calculation will compare equal to 1 (it might, but don't bet on it long term).

Long sequences of calculations like "for(;i<=20.0;i+=0.1)" are especially problematic.

Arrange your grid using integers to maintain accuracy, then convert the grid coordinate into a float to perform your calculations.

4. Simply never use a real number type as the for loop variable.
This is how it should be written:
Code:
```for(int i = 0; i <= 200; ++i) // i represents 'tenths'
{
float fi = i * 0.1f;
if (i % 10 == 0)
printf("\n%5.1f ", fi);

printf("%5.2f ", sqrt(fi));
}```

5. But that assumes a C99 compiler, because you're declaring i in the for loop initialization section.

Also, I think that perhaps the OP meant to use
Code:
```int i;
for(i = 0; i <= 200; ++i) // i represents 'tenths'
{
float fi = i * 0.1f;
if (i &#37; 10 == 0)
printf("\n%5.1f ", fi);
else
printf("%5.2f ", sqrt(fi));
}```
and also make the 5.1 and 5.2 the same.

6. but you assume C99 compiler as well with the one line comment

7. I was just copying your code. If it was my code I'd've used fsqrt() as well, since we're dealing with floats here, not doubles.

Not to mention using single quotes for a string.

8. ## thanks

thanks for the replies. It's been real useful to know that float numbers are approximations and may give absurd results.

I had just been going through complex XORing to swap 2 variables without using a temp.
But when i used the same code with arrays it produced strange results.
Code:
```int a=2,b=5;
a^=b^=a^=b;

printf("%d %d \n",a,b);

int c[2]={2,5};

c[0]^=c[1]^=c[0]^=c[1];

printf("%d %d",c[0],c[1]);```
output
5 2
0 2

9. The XOR hack to swap two numbers is probably a bad idea these days. It's tough for a compiler to optimise it. And I think it might actually be technically undefined.

That code works just fine for me, though GCC gives a warning on the first swap line.
Code:
```5:    a^=b^=a^=b;

5 C:\path\xorhackarray.c [Warning] operation on `a' may be undefined```