C
C#2w ago
Ktoto

Y it doubles on adding?

I have a code like this
c#
a += b;
c#
a += b;
I have overloaded operator +. T is class with list & a lot's of readonly fields depending on this list. b is always small, something like ~0.001 and on adding elements become 2 limes larger. When i debug it, it works normal. Here is code of operator:
c#
class T
{
List<float> list;
public static T operator +(T a, T b)
{
T result = a;
for (int i = 0; i < a.list.Count; i++)
{
result.list[i] += b.list[i];
}
return result;
}
}
c#
class T
{
List<float> list;
public static T operator +(T a, T b)
{
T result = a;
for (int i = 0; i < a.list.Count; i++)
{
result.list[i] += b.list[i];
}
return result;
}
}
27 Replies
Ktoto
KtotoOP2w ago
here is the whole code: https://pastebin.com/EpVn45eR
Pastebin
using System.Collections;using System.Collections.Generic;using Uni...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Ktoto
KtotoOP2w ago
yes, it's vector for as many axises as u want don't ask me for what do i need it, it'll take too long but for my work i can't use fixed-size vectors
Angius
Angius2w ago
What do you mean "on adding elements become 2 times larger"? Like, every element in the list gets multiplied by 2?
Ktoto
KtotoOP2w ago
yeah
qqdev
qqdev2w ago
A minimum example would be good here Which would only contain the necessary code to reproduce the issue And without any Unity dependencies
Ktoto
KtotoOP2w ago
Pastebin
using System.Collections.Generic;public struct Vector{ public List
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
qqdev
qqdev2w ago
Thanks! Do you have an example for some problematic vectors? Not at my computer rn, but I’ll be able to test it in a couple of min
Ktoto
KtotoOP2w ago
i can run debugger & find it. i think it won't be too long "0,001032626;0,0005739216;" + "0,001032684;0,0005739013;" = "0,003097994;0,001721724;" "0,001032512;0,0005739623;" + "0,003097994;0,001721724;" = "0,0072285;0,004017411;" "0,00103234;0,0005740232;" + "0,0072285;0,004017411;" = "0,01548934;0,008608845;" " there're for seeing certain elements & i take this values from to string function
qqdev
qqdev2w ago
Hm, I don't see any issue It behaves the way you implemented it For debug and release But I get this result for the first addition 0.00206531 0.001147823 Can you show a gif/video of it producing different results?
Ktoto
KtotoOP2w ago
ok, but it'll be not with the same values, cause it's generated randomy
Ktoto
KtotoOP2w ago
Ktoto
KtotoOP2w ago
qqdev
qqdev2w ago
Use the minimal example you posted please I have the feeling that sth else is going on which is unrelated to the code you showed us
Ktoto
KtotoOP2w ago
wdym?
qqdev
qqdev2w ago
This one If it has the same issue it should also work That screen recording is hard to follow
var a = new Vector { coords = [0.001032626F, 0.0005739216F] };
var b = new Vector { coords = [0.001032684F, 0.0005739013F] };
var c = a + b;

Console.WriteLine($"{c.coords[0]} {c.coords[1]}");
var a = new Vector { coords = [0.001032626F, 0.0005739216F] };
var b = new Vector { coords = [0.001032684F, 0.0005739013F] };
var c = a + b;

Console.WriteLine($"{c.coords[0]} {c.coords[1]}");
Ktoto
KtotoOP2w ago
it may work, but to generate iter i need all this fields & this issue is really weird cause when i go into += with debugger it doesn't appear i can hide all properties that u don't actually need. is it what u want?
qqdev
qqdev2w ago
Nah, just isolate the issue and put it into a pastebin
Ktoto
KtotoOP2w ago
i understand that i sound like a stupid weirdo but i can't isolate it because i don't know what causes it. i have other place where i also use this operator but all ok
qqdev
qqdev2w ago
No worries, it does not sound weird But it's a sign that the operator might not be the issue then
Ktoto
KtotoOP2w ago
guy, really thx i have lines like
c#
coordChange = iter;
velocities = iter;
c#
coordChange = iter;
velocities = iter;
& now it's
c#
iter.ForEach(i =>
{
coordChange.Add(i.copy);
velocities.Add(i.copy);
});
c#
iter.ForEach(i =>
{
coordChange.Add(i.copy);
velocities.Add(i.copy);
});
& now no more vectors like trillions+
Angius
Angius2w ago
Why not just a regular ol' foreach loop?
Ktoto
KtotoOP2w ago
maybe it setted this variables like a links to iter
Angius
Angius2w ago
Could be The struct itself is passed by value But the list inside, by reference Could be some shenanigans there
Ktoto
KtotoOP2w ago
no, not this list of structures with list was problem maker
qqdev
qqdev2w ago
Oh, did you thought that the list also gets copied?
Ktoto
KtotoOP2w ago
no, but it's logical in cs u can't overload = operator + there's unity, i use linking feature a lot with classes like GameObject
qqdev
qqdev2w ago
Thank god :when:
Want results from more Discord servers?
Add your server