C
C#14mo ago
fry

❔ ✅ Strange precision errors, double vs, decimal, and loan calculations

This is kind of a last resort, and I'm not looking for answers Whenever I try doing some calculations, I get weird errors. The first image is what I SHOULD get, but when I do mine, I get some really funky results. This output is done with someone using doubles too. I'm so lost, please hgelp
No description
86 Replies
fry
fryOP14mo ago
Here's what happens on mine
No description
fry
fryOP14mo ago
I am genuinely lost, because it looks like they start close, and then it just goes everywhere
Jimmacle
Jimmacle14mo ago
can you include the table headers so we know what is supposed to go where? those images have different numbers of columns
fry
fryOP14mo ago
One moment
fry
fryOP14mo ago
No description
Jimmacle
Jimmacle14mo ago
including the code that does the math would help too
fry
fryOP14mo ago
I didnt finish my rows on this one, since I wanted to save them for later (far left is months) MonthlyPayment, PrincipalPaid, InterestPaid, TotalInterestPaid, Balance
No description
Jimmacle
Jimmacle14mo ago
i'm not sure what it's supposed to look like but at the very first line it looks like the code is producing incorrect output, there's no 0th cycle in the expected output
fry
fryOP14mo ago
Oh, that one is a 'debug' line that I put in you can safely ignore it This is just an output from a friend, not the teacher But I still will not have access to the full code
fry
fryOP14mo ago
I wrote this all today because I was busy for the week, excuse the code (im kind of a beginner) This is what does the table math
No description
fry
fryOP14mo ago
It's what you'd call a clusterfuck, but I just want this over with
fry
fryOP14mo ago
Here is my friend's table code for that though. I was able to see this
No description
fry
fryOP14mo ago
sorry if this is a lot, I just want to put out everything I know
Jimmacle
Jimmacle14mo ago
what's the definition of the type of userLoan?
fry
fryOP14mo ago
It's a public class Loan object (I think thats the right terminology)
Jimmacle
Jimmacle14mo ago
i mean the full definition, that much is obvious 😛
fry
fryOP14mo ago
oh uhhh wdym by full definition?
Jimmacle
Jimmacle14mo ago
if you're trying to track down precision errors we need to know the data types used in your code
fry
fryOP14mo ago
Originally, they were all double I did some looking online and changed it to decimal No dice
Jimmacle
Jimmacle14mo ago
if you're using decimal then the chances of this being caused by floating point error are practically 0 there's probably some other error in your program
fry
fryOP14mo ago
That's what I'm thinking Are you okay with looking at my abomination of code?
Jimmacle
Jimmacle14mo ago
that's what the channel is for 😛 $code
MODiX
MODiX14mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
fry
fryOP14mo ago
BlazeBin - ggxglnpbzlmm
A tool for sharing your source code with the world!
fry
fryOP14mo ago
Im aware some of the stuff is completely fucked kekw I just want this assignment done and gone
Jimmacle
Jimmacle14mo ago
this is probably unrelated but you shouldn't do it because it will get you into trouble
public decimal InterestRate
{
get { return interestRate; }
set { interestRate = value / 100; } // Turn user's decimal input into a percentage
}
public decimal InterestRate
{
get { return interestRate; }
set { interestRate = value / 100; } // Turn user's decimal input into a percentage
}
the getter and setter should be symmetrical, as in if i set InterestRate and read it back i should get the same value
fry
fryOP14mo ago
Ahh, got it I'll keep that in mind for future work. I always thought that was a little dodgy I almost forgot
Jimmacle
Jimmacle14mo ago
for basically all of these you should use auto-properties instead of explicit backing fields
public decimal MyProperty { get; set; }
public decimal MyProperty { get; set; }
fry
fryOP14mo ago
I just do it because our teacher is using a C#6 book with videos from 2012 (so god knows what C# version that is) And that was how they did it in a video
Jimmacle
Jimmacle14mo ago
disgusting, but understandable if your course is forcing you to write bad code on purpose
fry
fryOP14mo ago
Yeah, it's horrible
fry
fryOP14mo ago
Here's what I forgot. This is the formula I use to calculate that monthly payment you see ($86.41)
No description
fry
fryOP14mo ago
It's for some amortization loan stuff, so hopefully that gives context
Jimmacle
Jimmacle14mo ago
idk anything about the math, but i would start with checking that usage of Math.Round that's almost definitely what's screwing up your calculations
fry
fryOP14mo ago
on userLoan.MonthlyPayment?
Jimmacle
Jimmacle14mo ago
at least, it's more likely than floating point error yeah, that just stands out to me as potentially being wrong
fry
fryOP14mo ago
Wait that's the only place i have it LULLLLLL me check
fry
fryOP14mo ago
No description
fry
fryOP14mo ago
86.41. Nothing more nothing less
Jimmacle
Jimmacle14mo ago
but does it affect your other math if you're rounding off the fractional cents?
fry
fryOP14mo ago
As far as I know, it's only doing Math.Round to MonthlyPayment Hold on, let me step through this one by one in my head
fry
fryOP14mo ago
Here's what happens when I remove :C from the output
No description
fry
fryOP14mo ago
I know it's unreadable, but 80.57666666 gets rounded up, and I think that's what's causing the errors? Let me crack open a calculator 5.833333333 is correct. The problem row is principal paid And somehow that translates into balance having an issue But 86.41-5.83 = 80.58 What the shit?
fry
fryOP14mo ago
This is literally not adding up in my mind right now
No description
fry
fryOP14mo ago
According to WikiHow
Your monthly payment is [example on site]. The dollar amount of the payment stays constant. However, the portion of the payment that is principal or interest will change.
fry
fryOP14mo ago
No description
fry
fryOP14mo ago
My mind is exploding @Jimmacle Am I high on drugs or something?? How does $0.01 just magically vanish? I have been at this literally all day, maybe Im missing something
SinFluxx
SinFluxx14mo ago
You're working with figures rounded to 2dp
fry
fryOP14mo ago
Yeah, but are they rounded by truncation or the way I think rounding works? Truncation makes no sense, that would be way out of scope for my class
SinFluxx
SinFluxx14mo ago
They'll presumably just be truncated when displaying, and all calculations are done without any rounding
fry
fryOP14mo ago
But in the line my classmate uses, it's this: $"{PrincipalPaid, -15:C" The :C is for currency right? It seems to round everything normally I am still unable to fathom this at all I redid everything in nicer code, and the same problem comes up
SinFluxx
SinFluxx14mo ago
Do you know that your classmate's version is definitely correct? As tbh while rounding rules with currency do vary from business to business, any Total Payment should still actually be the sum of all its parts
fry
fryOP14mo ago
You can check this out This is an online calculator and his follows that table basically exactly
fry
fryOP14mo ago
Bankrate
Loan Calculator | Bankrate
Bankrate's loan calculator will help you determine the monthly payments on a loan.
fry
fryOP14mo ago
Same thing
No description
fry
fryOP14mo ago
It's definitely some horrifying rounding shit that I'm not thinking about right now
No description
SinFluxx
SinFluxx14mo ago
Weird, only 3 months add up to .41
fry
fryOP14mo ago
AHAH WHAT THE FUCK
fry
fryOP14mo ago
No description
fry
fryOP14mo ago
WHY???? I was rounding off the monthly payment
fry
fryOP14mo ago
????????
No description
fry
fryOP14mo ago
You are about to witness a meltdown, because I swear on my life this is what I had initially But it just became $80.58 first
SinFluxx
SinFluxx14mo ago
Oh I thought you stopped rounding that when it was mentioned earlier
fry
fryOP14mo ago
I don't believe i did, I might have missed it Hold on, let me continue this why the hell is this magically working
Jimmacle
Jimmacle14mo ago
magic isn't real, check the differences between your original code and what's working now
fry
fryOP14mo ago
This is true I rewrote this from ground up
SinFluxx
SinFluxx14mo ago
As far as I'm aware when you're working with financials you shouldn't round until the very end
fry
fryOP14mo ago
I wish I knew that at the start Let me write the complete code
SinFluxx
SinFluxx14mo ago
@Jimmacle did point that out early on 😉
Jimmacle
Jimmacle14mo ago
rounding is always a red flag even if you do need to round, the specific rounding method you use may need to be different
fry
fryOP14mo ago
Yeah, youre absolutely right
Jimmacle
Jimmacle14mo ago
midpoint, up, down, even, whatever the options are i don't keep track
fry
fryOP14mo ago
That's true, I remember seeing a midpoint option in on Stack userLoan.MonthlyPayment = userLoan.LoanAmount * ((userLoan.MonthlyInterest * interestExponent) / ((interestExponent) - 1)); This was the line with the rounding
fry
fryOP14mo ago
When I remove it, it goes to 80.56
No description
fry
fryOP14mo ago
wait LULLLLLL that's because I thought it would be funny to just subtract 1 cent at the start whatttttttttttttt the fuck my original code magically works I have two versions of the same thing
fry
fryOP14mo ago
im gonna cry
No description
fry
fryOP14mo ago
If i remember correctly, when I did this originally, I was having some issues with it coming up as some messed up numbers So I thought rounding would fix it It has been 5 hours of this, and I am no longer able to remember my steps I guess that's that then. Wtf? One more question, is it better to use public, or private? What should I do with my classes and their variables?
Angius
Angius14mo ago
Is it better to use a fork or a spoon? Each has its uses and should be utilized accordingly
fry
fryOP14mo ago
I see, so in that case I should reword it In what situations should I use public/private? They never bothered explaining it to me I can see answers on Stack but they dont process with me
Angius
Angius14mo ago
You use public for class members if they're supposed to be... public, accessible from the outside. You use private if they're supposed to be... private, inaccessible from the outside or child classes And protected if they're supposed to be inaccessible from the outside, but accessible to all child classes
fry
fryOP14mo ago
Ohhhhh That helped me refine my search I dont know why, but I was told it helps keeps things safer
fry
fryOP14mo ago
Software Engineering Stack Exchange
Why do we need private variables?
Why do we need private variables in classes? Every book on programming I've read says this is a private variable, this is how you define it but stops there. The wording of these explanations alw...
fry
fryOP14mo ago
Thanks 🙏 idk how to mark as closed
Angius
Angius14mo ago
$close
MODiX
MODiX14mo ago
Use the /close command to mark a forum thread as answered
Accord
Accord14mo 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