vb script to javascript - vba

Can anyone help me to convert this VB script to equivalent Javascript please.
PMT = ((PV - FV) * rate/ (1 - (1 + rate) ^ -(nper)))

Probably
var PMT = ((PV - FV) * rate / (1 - Math.pow(1 + rate, -nper)));
JavaScript numbers are always (at heart) floating-point values, so when you're dealing with money things can get somewhat weird.

Related

Vb maths calculations incorrect results

I am trying to calculate the expected "score" for a given player in an elo system*.
The problematic line of code is as follows:
expected(0) = (1 / (1 + (10 ^ (1000 - 1000) / 400)))
When I return the value of expected(0) directly after this line as a msgbox in a windows forms application, it states the value to be 11 even though it should be 1 (expected(1) is declared as an array of integers).
When I run this same line of code in a console application, it returns 1.
Is there any reason why this line of code is instead returning 11?
Edit: The exact code of the subroutines for both the console application that I tested (which returns correct value) and the forms application:
Forms:
Dim expected(1) As Integer
expected(0) = (1 / (1 + (10 ^ (1000 - 1000) / 400)))
Msgbox(expected(0))
Console:
Dim t(1) As Integer
t(0) = (1 / (1 + (10 ^ (1000 - 1000) / 400)))
Console.WriteLine(t(0))
Console.ReadLine()
The above numeric values (e.g. 400, 1000) are literally written in to the program like that, as I was just testing whether they work.
*you can look up the equation on the wikipedia page "Elo rating system" but I don't think it is important in this situation. In this situation I am simulating both players Elo being 1000.
Apologies to anyone who was scratching their head at this, but the solution was very simple and the problem was 100% my fault. As it turns out msgbox(expected(0) was actually written as msgbox("expected 1" & expected(0)) which came out in the box as "expected 11" rather than "1" or "expected 1 1". I thought 11 was the value. Apologies again.
Your formula from Wikipidia is:
One way to code this accurately:
Dim d1 As Double = (Rb - Ra) / 400R '0
Dim d2 As Double = 1 + System.Math.Pow(10, d1) '2
Dim EA As Double = 1 / d2 '0.5
Your expression:
(1 / (1 + (10 ^ (1000 - 1000) / 400)))
Is not an accurate translation of the source forumul above, your expression above returns zero instead of 0.5. If EA is an integer, value you will get the same result from both calculations, but I think you should always get the correct value then explicitly manipulate it rather manipulating it implicitly (by the compiler, for instance).

Difference between these 2 functions?

I have 2 degree-to-radian functions pre-defined using #define:
Function 1:
#define degreesToRadians(degrees) (M_PI * degrees / 180.0)
Function 2:
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
Only the 2nd function returns correct answer, while the first one provides weird answer. What are the differences between them?
Non of the two "functions" mentioned above is a functions, they are macros, and the first macro is not safe, for example, expanding the macro degreesToRadians(10 + 10) gives (M_PI * 10 + 10 / 180.0), which is interpreted as ((M_PI * 10) + (10 / 180.0)) and this is clearly wrong. While expanding DEGREES_TO_RADIANS(10 + 10) gives ((10 + 10 ) / 180.0 * M_PI) which is correct.
The other difference is that M_PI * degreess might overflow the double boundaries, and thus give a wrong answer (but this requires a rather high value in degrees)
The calculations are pretty much identical, notwithstanding floating point limitations. However, you have angle surrounded with parentheses in the second macro, which is the right thing to do.
In the first macro, if you do:
x = degreesToRadians(a + 45);
then, remembering that macros are simple text substitutions, you'll end up with:
x = (M_PI * a + 45 / 180.0);
which will not end well, since it will be calculated as if you'd written:
x = (M_PI * a) + (45 / 180.0);
In other words, you simply multiply the angle by PI and add a constant 0.25.
If instead you change the first one to be:
#define degreesToRadians(degrees) (M_PI * (degrees) / 180.0)
then it should begin to behave a little better.
The other difference has to do with either large or small values of the angle. A divide-then-multiply on a small angle (and I mean really small like 10-308, approaching the IEEE754 limits) may result in a zero result while a multiply-then-divide on a large angle (like 10308) may give you overflow.
My advice would be to ensure you use "normal" angles (or normalise them before conversion). Provided you do that, the different edge conditions of each method shouldn't matter.
And, in all honesty, you probably shouldn't even be using macros for this. With insanely optimising compilers and enumerations, macros should pretty much be relegated to conditional compilation nowadays. I'd simply rewrite it as a function along the lines of:
double degreesToRadians(double d) {
return M_PI * d / 180.0;
}
Or, you could even adjust the code so as to not worry about small or large angles (if you're paranoid):
double degreesToRadians(double d) {
if ((d > -1) && (d < 1))
return (M_PI * d) / 180.0;
return (d / 180.0) * M_PI;
}

vb.net cube root gives wrong value?

I was simply looking for a way to get cube roots in vb.net. The consensus online is to use the formula:
<number> ^ (1 / 3)
I tried punching a few of these into the immediate window and here's what I get:
?1 ^ (1 / 3)
1.0
?8 ^ (1 / 3)
2.0
?27 ^ (1 / 3)
3.0
?64 ^ (1 / 3)
3.9999999999999996
Wait a minute.. Shouldn't the answer be 4.0? What happened? 4 * 4 * 4 = 64, not 3.9999999999999996 * 3.9999999999999996 * 3.9999999999999996 = 64. I'm usually good with math problems but I've spent too much time with this and I could use some help. I'm not as interested in finding out why this failed as much as I am interested in how to make this work given the number 64 and trying to get the cube root to equal 4.
This works for me:
Private Function CubedRoot(ByVal dNum As Double) As Double
Return CType(CType(dNum ^ (1 / 3), Decimal), Double)
End Function

Objective-c power operator not clear

Im working on a small calculation app and I'm using a formula I created in PHP and now trying to translate to Objective-C however, the power operator is not clear to me.
Im looking at the following code:
float value = ((((x)*i)/12)/(1-(1+i/12)^-((x*12))))-i;
The power operator is non existent in Objective-C.
How should I apply the power operator in Objective-C and could some assist me by telling me where it should be?
(Too many parentheses! You don't need parentheses around x or (x*12), for instance.)
There is no power operator. The standard function powf() will do the job, however (pow() if you wanted a double result):
float value = x * i / 12 / (1 - powf(1 + i / 12, -12 * x)) - i;
^ is the bitwise XOR operator both in C (and so in Objective-C as well) and in PHP.
To perform a power operator use the C pow (which returns a double) or powf (which returns a float) functions
float result = powf(5, 2); // => 25
Your expression will then become (stripping away all the redundant parenthesis and leaving some for readability) :
float value = (x*i/12) / (1 - powf(1 + i/12, -x*12)) - i;

Objective C - Creating Angles From Current Time

I'm trying to write code to draw a clock on the screen of an iOS device. I need to get the angle of a line (seconds, minutes, hours hands of clock) from the current time. My code accurately grabs the time, but for some reason, all of the angles I receive end up being the same (no matter what time it is).
If it helps, the angle I am constantly receiving is:
-1.5707963267948966
Here is the code I use to get the angles:
secondsTheta = ((seconds/60) * (2 * M_PI)) - (M_PI / 2);
minutesTheta = ((minutes/60) + (seconds/3600)) * (2 * M_PI) - (M_PI / 2);
hoursTheta = ((hours/12) + (minutes/720) + (seconds/43200)) * (2 * M_PI) - (M_PI / 2);
My thought is that something is funky with M_PI, but I don't know what would be...but as I said, the seconds, minutes, and hours variables are correct. They are declared in my header file as ints, and I know that [NSDateComponents seconds](etc) returns an NSInteger, but I don't think that should matter for this basic math.
Since the seconds, minutes, and hours variables are declared as ints the division will not give you the correct values. An int divided by another init will result in an int, what is needed for the result is a float. In order to have the compiler use floating point arithmetic it is necessary that one of the operands be a floating point format number (float).
Example: 10 seconds divided by 60 (10/60) will use integer math and result in 0.
Example: 10.0 seconds divided by 60 (10/60) will use floating point math and result in 0.1.66666667.
Example:
secondsTheta = ((seconds/60.0) * (2 * M_PI)) - (M_PI / 2);
or
secondsTheta = (((float)seconds/60) * (2 * M_PI)) - (M_PI / 2);
Your seconds, minutes and hours are ints. Dividing ints by ints does integer arithmetic and truncates the values, so
seconds/60
will always give you 0. Objective C inherits this behavior from C and this is fairly common behavior among programming languages.