Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mathematical calculation help

Status
Not open for further replies.

vbpadawan

Programmer
Oct 8, 2002
28
US
I am having problems with a formula in VB. I have two variables declared as X and Y. I set X = 5/100/12. I want to set Y = X^360. I am getting 0 for Y and do not know why when the answer should be 1.3303......
I have even tried to set X = (5/100/12)^360 and I still get 0. What am I doing wrong?
Thanks.
 
I dont think the result is 1.33.. Its much smaller, outside of the precision of a VB double, hence rounded to 0.

Double (double-precision floating-point) variables are stored as IEEE 64-bit (8-byte) floating-point numbers ranging in value from 4.94065645841247E-324 to 1.79769313486232E308 for positive values.



 
I don't think you understood. I am getting 0. The answer should be 1.330310392440718301964487060798e-857. I obviously don't need that entire thing so I can round it but the problem is that I am still getting 0.
 
How do you say that the result will be 1.3303...?
In fact the result will be zero (approximately).
You may verify with the calcultor.
 
5/100/12 = 0.00416666666666666666666666666666667

0.00416666666666666666666666666666667 ^ 360 = 1.330310392440718301964487060798e-857

right from the calculator
 
OK, SonOfEmidec1100 is right. The result is so small that it is outside the precision limits of VB double, that's why you are getting 0.
 
Even using a Double datatype you will be well outside the limits.
Quote from VBHelp:
The Double data type requires 8 bytes of memory and can store negative values between -1.79769313486232 x 10308 and -4.94065645841247 x 10-324 and positive values between 4.94065645841247 x 10-324 and 1.79769313486232 x 10308.


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
There is a way you can perform this calculation. The idea is to use logarithms and return the results as a string.

For that you have to write a Power function of your own instead of using the ^ operator. But the disadvantage is that the return value of this function is a string which cannot be used in further numerical calculations but only for displaying results. See how.
___
Private Sub Form_Load()
Dim X As Double, Y As Double
X = 5 / 100 / 12
Y = 360
MsgBox Power(X, Y)
End Sub

Function Power(X As Double, Y As Double) As String
Dim Z As Double, Exponent As Integer
Z = Log(X) / Log(10) 'Convert to log10
Z = Z * Y 'Multiply log with power
Exponent = Int(Z) 'Seperate exponent
'Compose the result combining base and exponent
Power = 10 ^ (Z - Exponent) & "E" & Exponent
End Function
 
>I obviously don't need that entire thing so I can round it but the problem is that I am still getting 0.

Will you need any part of it?

Just think of what this number represents!

Does anything that small exist in the universe?

For all practical, and probably all theorectical purposes, it is ZERO.

 
Is this by any chance a mortgage calc? If you tell us what you want to calculate we can maybe help....

I'm thinking 5% p.a. compounded monthly over 30 years


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
hsmalley, can we just confirm that you do appreciate what the "e-857" at the end of the result means?
 
I think you are getting close johnwm.

(strongm, I wanted to ask that also, but it's monday and I probably wouldn't have been that gentle, and would have asked....differently.)

hsmalley, as johnwm says, please let us know what you are after, and we'll try to help you out and get you going in the right direction!
 
Yes, this is a mortgage payment calc and the part that I posted was only a small part of it. I broke it up to make it easier to read and follow. I did get it working this morning, though.
I now need to calculate the APR but cannot find the formula to do this. Does anyone happen to have this?

Thanks for all your help.
 
VB has a set of functions designed for just this sort of thing. Find the MSDN entry for the Pmt function then click the See Also link to see the complete list.
 
Take note that in some countries (UK for example) the calculation for APR may or may not include various other sums such as front-loaded or rear-loaded lump sums, and that there are legal consequences in quoting (and calculating) APR appropriately.

I guess you've now worked out where the 1 should go in your original question:
X = 5/100/12
Should read :
X = 1 + (5/100/12)
so now Y = X^360 will give:
4.46774431400611



________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top