c - How to loop and increase by 0.01 everytime? -
i'm confused on code.
here's want do: start "v" value of 5, carry out rest of functions/calculations, increase "v" value 0.01, carry out functions/calculations, increase "v" value 0.01 again, carry out functions...do 500 times or until "v" value of 10.00 reached, whichever easier code.
here code @ moment:
//start loop on v for(iv=5;iv<=500;iv++) { v=0.01*iv; //lots , lots of calculations v here }
here get: tried setting iv<=10 10 loops test first before leaving on night. did 6 loops, starting @ v=0.05 , ending @ 0.1. problem a) didn't run 10 loops, b) didn't start @ 5.00, started @ 0.05.
any appreciated.
edit: holy crap, many answers! i've tried 2 different answers far, both work! i've been staring @ , changing code around 3 hours, can't believe easy.
you need start @ iv = 500
. , if want 10 loops, , iv++
update, stop before 510
.
reason: v = 0.01*iv
, v = 5
means iv = 5/0.01 = 500
. number of iterations, if loop of form for (x = n; x < m; x++)
(constant n
, m
), max(0, m-n)
loops executed, if x
not changed in loop , no weird stuff (e.g. overflow, hidden casts of negative numbers unsigned, etc.) occurs.
edit
instead of using v = 0.01 * iv
, v = iv / 100.0
more accurate. reason: 0.01 not representable in floating point, 100.0 is.
Comments
Post a Comment