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:
Review how division works in C Also maybe review how floating point numbers work in C
Mason Rivera
x will never be equal to 0.52
Brandon Perry
In this case the division between integers does not change anything even if it might not be what is intended
Thomas Reyes
If a programmer thinks that that is not a mistake, hiring that programmer was a mistake
Cameron Hughes
sorry, I just had one class hehe, I'm not a programmer yet
Zachary Martin
Try printing x just to see what 1/3 is. Then read about floating point numbers.
Brayden Stewart
It might be the wanted result you shitter how would you know That is not what makes the code not work
Leo Morris
You should not compare floating point numbers using == and !=. Subtract them and check that result is less than margin of error.
Lincoln Parker
thanks!!
Camden Flores
I didn't know that, thank you
Jaxon Foster
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.
Andrew Hernandez
I think i got it now
Asher Reed
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.
Joshua Harris
>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?
Luis Price
What, you want me to cast an int to int just so you're sure I want an int?
Joshua Evans
There is no mistakes there. x never becomes 0.52 and the loop goes on forever. Did you meant x < 0.52?
Isaiah Brown
>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
Brayden Sanders
Make it clear that you're doing it on purpose
Henry Miller
it was given to me like that hahaha
Mason Carter
Could just do x
Aiden Nelson
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.
Jace Roberts
You could, but if you want to precisely check that x is equal to y, you generally should do something like (x - y)
Aaron Hughes
Also check that x is larger than y if you do this.
Colton Rivera
Well, yeah, or you could just add abs(), but that's a detail.
Adam Carter
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
Mason Thomas
This
Dylan Sullivan
>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
Mason Young
>have to write your own float comparator in C >C tards will defend this
Grayson Baker
This is right.
Dylan Russell
>he doesn't get the point is to learn >obviously if needed you can import a library joke's on you