❔ ✅ 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
86 Replies
Here's what happens on mine
I am genuinely lost, because it looks like they start close, and then it just goes everywhere
can you include the table headers so we know what is supposed to go where? those images have different numbers of columns
One moment
including the code that does the math would help too
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
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
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
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
It's what you'd call a clusterfuck, but I just want this over with
Here is my friend's table code for that though.
I was able to see this
sorry if this is a lot, I just want to put out everything I know
what's the definition of the type of
userLoan
?It's a
public class Loan
object (I think thats the right terminology)i mean the full definition, that much is obvious 😛
oh uhhh
wdym by full definition?
if you're trying to track down precision errors we need to know the data types used in your code
Originally, they were all double
I did some looking online and changed it to decimal
No dice
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
That's what I'm thinking
Are you okay with looking at my abomination of code?
that's what the channel is for 😛
$code
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/BlazeBin - ggxglnpbzlmm
A tool for sharing your source code with the world!
Im aware some of the stuff is completely fucked I just want this assignment done and gone
this is probably unrelated but you shouldn't do it because it will get you into trouble
the getter and setter should be symmetrical, as in if i set InterestRate and read it back i should get the same value
Ahh, got it
I'll keep that in mind for future work. I always thought that was a little dodgy
I almost forgot
for basically all of these you should use auto-properties instead of explicit backing fields
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
disgusting, but understandable if your course is forcing you to write bad code on purpose
Yeah, it's horrible
Here's what I forgot.
This is the formula I use to calculate that monthly payment you see ($86.41)
It's for some amortization loan stuff, so hopefully that gives context
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 calculationson
userLoan.MonthlyPayment
?at least, it's more likely than floating point error
yeah, that just stands out to me as potentially being wrong
Wait that's the only place i have it me check
86.41
. Nothing more nothing lessbut does it affect your other math if you're rounding off the fractional cents?
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 headHere's what happens when I remove
:C
from the outputI 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?This is literally not adding up in my mind right now
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.
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
You're working with figures rounded to 2dp
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
They'll presumably just be truncated when displaying, and all calculations are done without any rounding
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 upDo 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
You can check this out
This is an online calculator and his follows that table basically exactly
Even this site:
https://www.bankrate.com/loans/loan-calculator/
Bankrate
Loan Calculator | Bankrate
Bankrate's loan calculator will help you determine the monthly payments on a loan.
Same thing
It's definitely some horrifying rounding shit that I'm not thinking about right now
Weird, only 3 months add up to .41
AHAH
WHAT THE FUCK
WHY????
I was rounding off the monthly payment
????????
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
Oh I thought you stopped rounding that when it was mentioned earlier
I don't believe i did, I might have missed it
Hold on, let me continue this
why the hell is this magically working
magic isn't real, check the differences between your original code and what's working now
This is true
I rewrote this from ground up
As far as I'm aware when you're working with financials you shouldn't round until the very end
I wish I knew that at the start
Let me write the complete code
@Jimmacle did point that out early on 😉
rounding is always a red flag
even if you do need to round, the specific rounding method you use may need to be different
Yeah, youre absolutely right
midpoint, up, down, even, whatever the options are
i don't keep track
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 roundingWhen I remove it, it goes to 80.56
wait 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
im gonna cry
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?
Is it better to use a fork or a spoon?
Each has its uses and should be utilized accordingly
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
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 classesOhhhhh
That helped me refine my search
I dont know why, but I was told it helps keeps things safer
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...
Thanks 🙏
idk how to mark as closed
$close
Use the /close command to mark a forum thread as answered
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.