Noob C programmer

Hi guys, I'm just starting to program in C, and i'm having some issues with this excercice. I have this fragment of code and I have to find mistakes, but i can't find any mistakes there, if anyone can help me I'll be very happy c:
so this is the code:

float x = 1/3;
while(x != 0.52 )
x += .01;

Attached: The_C_Programming_Language_logo.svg.png (1200x1276, 77K)

0.01 not .01

did you try 0.01?

so I always have to put a number before the pint?

Review how division works in C
Also maybe review how floating point numbers work in C

x will never be equal to 0.52

In this case the division between integers does not change anything even if it might not be what is intended

If a programmer thinks that that is not a mistake, hiring that programmer was a mistake

sorry, I just had one class hehe, I'm not a programmer yet

Try printing x just to see what 1/3 is.
Then read about floating point numbers.

It might be the wanted result you shitter how would you know
That is not what makes the code not work

You should not compare floating point numbers using == and !=. Subtract them and check that result is less than margin of error.

thanks!!

I didn't know that, thank you

If you want to compare floatingpoint numbers like you do, the number must be the sum of (negative) powers of two.

1.0/3.0 and 0.52 are neither that and your comparison will always return false.

float x = 100.0/256.0;
while(x != 130.0/256.0 )
x += 1.0/256.0;

will work.

I think i got it now

It basically boils down to how your CPU represents floating point numbers (tl;dr: with 3 integers: sign, mantissa and exponent). Unlike integers, they aren't very precise when it comes to mathematical operations and equality comparison.

>It might be the wanted result you shitter how would you know
Exactly, how do I know if it's intended behavior or a mistake if it looks like a mistake?

What, you want me to cast an int to int just so you're sure I want an int?

There is no mistakes there.
x never becomes 0.52 and the loop goes on forever.
Did you meant x < 0.52?

>float x = 1/3;
won't convert to float before the divide, so I think it rounds the result to zero (as an integer), then converts to a float
>while(x != 0.52 )
comparison may fail because floating point comparisons are inexact. you should use a small delta (range)
>x += .01;
this will have to run 52 times to get at least greater than 0.52

Make it clear that you're doing it on purpose

it was given to me like that hahaha

Could just do x

An infinite loop that occurs because a condition can never be false is usually a mistake.
The point of this exercise is probably to understand how floating-point numbers work, and why they aren't equal. The solution would be a loop that terminates when x is close enough to .52.

You could, but if you want to precisely check that x is equal to y, you generally should do something like (x - y)

Also check that x is larger than y if you do this.

Well, yeah, or you could just add abs(), but that's a detail.

the infinite loop here could be caused either because 1/3 float can have too many digits. but also because even if you had
float x=0.00
while (x!=0.52)
x+=0.01
floats aren't equal on the hardware as you type them, since our decimal system 10^-N is not compatible to what the computer can store which is on binary, so adding 0.01 is really an aproximation that eventually builds an offset value of the rational decimals or something, so you better read how these work in reality. Which will lead you to create a workaround

This

>float x = 1/3
makes x = 0 because of integer truncation
Do x = 1.0/3.0 or 1.0/3 or 1 / 3.0

>have to write your own float comparator in C
>C tards will defend this

This is right.

>he doesn't get the point is to learn
>obviously if needed you can import a library
joke's on you

float x = 1f / 3f;
for (; x