Try println(i/n)… iirc it looks like you’re dividing two ints there but you want a float so you need to divide using floats instead. I think you can just make i a float in your for loop?
Just to note that in your map(i, 0, n, 0, 1), i actually goes from 0 to n - 1.
Edit: Which isn't me trying to be smug or clever; it's just you might be wondering why you never get the maximum amount you're expecting when you reach the end of your loop.
So, here's the order that java (processing) does things on that line:
step 1: get the values from i and n. Let's say they're 3 and 10 (both ints). step 2: divide 3 by 10. Because they're both ints, it does integer division, and arrives at 0. Note that it hasn't yet done anything with the variable that it's going into. step 3: store the result in amnt. It dutifully stores 0 in that variable. Because amnt is a float variable, and java allows automatic casting from int to float, it is now the float 0 instead of the int 0.
If you want to make sure that full floating point division gets done, one of the values needs to be a float before the division happens. You can make one of those variable be a float, or you can cast the value to a float in-line. (like (float)i)
Yes it tried to store the result of the division in the float, but when it is trying to do he diving it sees two integers, so you get an integer answer, that is then saved in a float.
2
u/blazicke Sep 25 '23
This way everything works fine, but if I comment line 4 and uncomment line 5, it doesn't, and the printl returns always 0!
2
u/MandyBrigwell Moderator Sep 25 '23 edited Sep 25 '23
Just to note that in your map(i, 0, n, 0, 1), i actually goes from 0 to n - 1.
Edit: Which isn't me trying to be smug or clever; it's just you might be wondering why you never get the maximum amount you're expecting when you reach the end of your loop.
1
u/blazicke Sep 25 '23
but amnt is a float and is the result of the division...