|
-
Jun 9th, 2011, 02:30 PM
#1
Thread Starter
Lively Member
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!
-
Jun 9th, 2011, 02:50 PM
#2
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:
-
Jun 9th, 2011, 03:03 PM
#3
Thread Starter
Lively Member
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?
-
Jun 9th, 2011, 03:04 PM
#4
Re: Need more accuracy in calculation
 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).
Last edited by formlesstree4; Jun 9th, 2011 at 04:23 PM.
Reason: I multiplied the wrong numbers together...oops.
-
Jun 9th, 2011, 03:08 PM
#5
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
Last edited by kaliman79912; Jun 9th, 2011 at 03:11 PM.
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Jun 9th, 2011, 03:09 PM
#6
Thread Starter
Lively Member
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?
-
Jun 9th, 2011, 03:15 PM
#7
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.
My usual boring signature: Nothing
 
-
Jun 9th, 2011, 03:15 PM
#8
Thread Starter
Lively Member
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|