C
C#2y ago
osmorredor

❔ ✅ += question

For some reason I dont get why t isnt equal to -15 The mental process I have is the following: initial value of t + result on the right side, thats the meaning of += t = -10 -10 + right side right side: -10 * (5/10) -10 * 0.5 -5 - 10 + (-5) = -15 Why in the hell the answer is -10
39 Replies
Pobiega
Pobiega2y ago
Try running the code locally with a breakpoint, so you can step through it and see the value at each step.
osmorredor
osmorredorOP2y ago
how? is a 2 step calculation
Pobiega
Pobiega2y ago
Are you using visual studio?
osmorredor
osmorredorOP2y ago
yep
Pobiega
Pobiega2y ago
Pobiega
Pobiega2y ago
click on the left margin, where my red dot is that creates a "breakpoint" that means the debugger will stop the program when you reach that point, and you can now see all the values then, run the program in debug mode.
osmorredor
osmorredorOP2y ago
Im clicking and nada oh nvm
Pobiega
Pobiega2y ago
$debug if you want more detailed instructions
MODiX
MODiX2y ago
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
osmorredor
osmorredorOP2y ago
okay... it shows me t = -10
Pobiega
Pobiega2y ago
What line did you set the breakpoint on?
osmorredor
osmorredorOP2y ago
t += ...
Pobiega
Pobiega2y ago
Use F10 to "step" forward that will run the currently highlighted line the breakpoint stops before the line you set it on so you can see the value before/after
osmorredor
osmorredorOP2y ago
it remains as the final answer and initial answer are the same t = -10 remains at every lane of the code after t is called I feel like the debugging thing would be interesting/helpful if I had different operations and my issue was that I got lost with the values at any given point but in here is a single operation that Im not able to grasp the result of it unless x isnt greater than 60 when the input is 66
Pobiega
Pobiega2y ago
Ah, okay It has to do with integer division 🙂 t += t * (5 / 10); that surely equates to.. t = -10 + (-10 * 0.5) right?
osmorredor
osmorredorOP2y ago
yes
MODiX
MODiX2y ago
Pobiega#2671
REPL Result: Success
5/10
5/10
Result: int
0
0
Compile: 403.274ms | Execution: 22.848ms | React with ❌ to remove this embed.
Pobiega
Pobiega2y ago
but 5/10 = 0
osmorredor
osmorredorOP2y ago
okay, I understand that if the inside of t was an integer BUT t is a double isnt it?
Pobiega
Pobiega2y ago
yep but 5 and 10 are what?
osmorredor
osmorredorOP2y ago
integers...
Pobiega
Pobiega2y ago
yep
osmorredor
osmorredorOP2y ago
holy shit
MODiX
MODiX2y ago
Pobiega#2671
REPL Result: Success
(5 / (double)10)
(5 / (double)10)
Result: double
0.5
0.5
Compile: 422.646ms | Execution: 31.487ms | React with ❌ to remove this embed.
osmorredor
osmorredorOP2y ago
so if I want 5/10 to give me 0.5 I need to do: 5.0/10.0?
MODiX
MODiX2y ago
Pobiega#2671
REPL Result: Success
(5d / 10d)
(5d / 10d)
Result: double
0.5
0.5
Compile: 420.055ms | Execution: 23.743ms | React with ❌ to remove this embed.
ero
ero2y ago
do the other one too
Pobiega
Pobiega2y ago
what, decimal? neah 😛
osmorredor
osmorredorOP2y ago
wow this is insane so every single time I do operations , I must take that in cosndieration I thought that if I call a variable double , that type of variable would remain within its calculations
Pobiega
Pobiega2y ago
if both sides of an operation are integers, the result will be an integer
osmorredor
osmorredorOP2y ago
ooooooooooh so if I call my double -10.0 instead of -10 , it woudl give me -15
Pobiega
Pobiega2y ago
no its pretty rare that you do constant math like that thou like, this code should be t += t * 0.5; and it would work
osmorredor
osmorredorOP2y ago
certainly, it should be this was a quiz question I had and I couldnt get my mind around it
Pobiega
Pobiega2y ago
yeah, its a trick question
osmorredor
osmorredorOP2y ago
obviously I had this one wrong, and now I can rest in peace
Pobiega
Pobiega2y ago
to see if you can identify the integer division
osmorredor
osmorredorOP2y ago
I certainly did not hahahaha okay, sounds good thank you @Pobiega !
Pobiega
Pobiega2y ago
yw if thats all, use /close to mark the thread as resolved 🙂
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server