Need more accuracy in calculation
Hi,
I'm writing a financial calculator. I know that my calculations are more precise when Decimal format is used. But when I calculate 314/365, the answer given by VB is 0.8602814581, where as the real answer is 0.8602739726. This ends up affecting my calculations quite a bit. Is there anyway to get a better, more accurate answer?
Thanks so much!
Re: Need more accuracy in calculation
What do you get when you try the Double format instead?
EDIT: I just tried using the Double format and got:
Re: Need more accuracy in calculation
I actually get the same answer with both double and decimal. The way the calculation is done is like this: Totalx = DateDiff(DayInterval.Day, StartDate, EndDate) / 365
When the I plug in a dates that should yield 314 days, then i get the answers above when Totalx is both decimal and double. Could it be a problem with DateDiff?
Re: Need more accuracy in calculation
Quote:
Originally Posted by
barnaby
I actually get the same answer with both double and decimal. The way the calculation is done is like this: Totalx = DateDiff(DayInterval.Day, StartDate, EndDate) / 365
When the I plug in a dates that should yield 314 days, then i get the answers above when Totalx is both decimal and double. Could it be a problem with DateDiff?
It could be, are you absolutely sure DateDiff is returning 314?
EDIT: Using a bit of algebra, DateDiff would have to be returning:
Maybe changing DateDiff to an Integer type would fix that, or doing some rounding...
EDIT 2: Fixed my number since I can't perform basic algebra...thanks kaliman (and I double checked with calculator this time).
Re: Need more accuracy in calculation
are you sure the dateDiff is not giving you a fractional value? actually using a bit of Algebra DateDiff should be returning 314.0027322
Re: Need more accuracy in calculation
I just checked and did MsgBox(DateDiff(DayInterval.Day, StartDate, EndDate) / 365) and it gave the accurate/right number. Why would it change when assigned the variable name?
Re: Need more accuracy in calculation
It wouldn't, but that MsgBox snippet you just posted shows that you have Option Strict OFF. That means that you are going to get a bunch of implicit conversions going on, and when you do that, just about anything could happen. Try it again with Option Strict ON (after fixing all the errors you will get), and see whether that fixes it. It would be really interesting if it did.
Re: Need more accuracy in calculation
I think I found my problem and it was in a part of the code that occurred earlier in the function. Thank you both for trying to help me though! You were very helpful.