Hello, I am very new to rust, and I do not use any specificity of the language, yet I am facing a very odd problem.
I need to take the remainder by q/2 of some value stored in a two dimensional vector sample1[i][j]
and store it in a second two dimensional vector sample2 [i][j].
wether I do
where redmod_int_sign(i64,i64) is a function returning an i64 that I make myself substracting q as long the value is not in the correct range or a function using %, it returns and store values which are often much bigger than q/2 or smaller than -q/2.
I really don't get why. I printed the values, and they are in the correct range INSIDE the function, but not outside the function.
Do you have any idea of what I am doing wrong?
Thank you already.
yes it is intentional, actually I don't exactly store sample1[i][d] but a linear combination of different sample1[i][j] coming from different loop on different i, since I only select some of them, I need another index l .
I don't see anything obviously wrong. Since you said the values are correct inside your function but not outside I thought maybe there was a problem with your return statements in the function. Note that temp == q_2 will never be true, since temp is the remainder of a signed modulo operation with q_2 as modulus. Hence temp will always be smaller than q_2.
Could it be that there's a bug in your linear combination?
Oh yes you are right, I should use %(q_2+1) since I want representatives in [q/2;q/2[ pretty rookie mistake. The linear combination is just a sum. I have three nested loops, (I know it's wrong), of index i,m,p, and I do things like that sample2[l][d]= redmod_int_sign(sample1[i][d]+sample1[m][d]-sample1[p][d],q); or alternatively I tried sample2[l][d]= (sample1[i][d]+sample1[m][d]-sample1[p][d])%(q>>1); and it still print big values when I print them. I find it super weird.
Basically, it means to write the smallest complete program that you can which still shows the error. It's a lot easier to investigate problems if there's a running piece of code that we can mess with directly, instead of suggesting things for you to try, and then waiting for you to report back.
Generally, you do this by starting with a copy of your program and systematically remove the unrelated parts. Eventually, you should end up with something small enough to paste into the playground, or at least upload to someplace like GitHub or GitLab.
I think it's something with the print and returning to the same line because when I use println! instead of print! it doesn't happen. Thank you all then, and thanks for noticing the q/2 stuff, it creates me bigger statistics. unbelievable.