❔ ✅ += 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 -1039 Replies
Try running the code locally with a breakpoint, so you can step through it and see the value at each step.
how?
is a 2 step calculation
Are you using visual studio?
yep
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.
Im clicking and nada
oh
nvm
$debug if you want more detailed instructions
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.
okay...
it shows me t = -10
What line did you set the breakpoint on?
t += ...
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/afterit 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
Ah, okay
It has to do with integer division 🙂
t += t * (5 / 10);
that surely equates to..
t = -10 + (-10 * 0.5)
right?yes
Pobiega#2671
REPL Result: Success
Result: int
Compile: 403.274ms | Execution: 22.848ms | React with ❌ to remove this embed.
but 5/10 = 0
okay, I understand that if the inside of t was an integer
BUT t is a double
isnt it?
yep
but 5 and 10 are what?
integers...
yep
holy shit
Pobiega#2671
REPL Result: Success
Result: double
Compile: 422.646ms | Execution: 31.487ms | React with ❌ to remove this embed.
so if I want 5/10 to give me 0.5 I need to do: 5.0/10.0?
Pobiega#2671
REPL Result: Success
Result: double
Compile: 420.055ms | Execution: 23.743ms | React with ❌ to remove this embed.
do the other one too
what, decimal? neah 😛
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
if both sides of an operation are integers, the result will be an integer
ooooooooooh
so if I call my double -10.0 instead of -10 , it woudl give me -15
no
its pretty rare that you do constant math like that thou
like, this code should be
t += t * 0.5;
and it would workcertainly, it should be
this was a quiz question I had and I couldnt get my mind around it
yeah, its a trick question
obviously I had this one wrong, and now I can rest in peace
to see if you can identify the integer division
I certainly did not hahahaha
okay, sounds good
thank you @Pobiega !
yw
if thats all, use
/close
to mark the thread as resolved 🙂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.