vb.net cube root gives wrong value? - vb.net

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

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).

round up to next multiple of 10 vb

How can I round a value UP to the next multiple of 10 in VB ?
e.g.
19 -> 20
35 -> 40
21 -> 30
I found so things saying use a round function but when i type it in to my IDE (Microsoft Visual Studio Express 2015) it doesn't recognise it.
Thanks in advance
To round up use the ceiling function. (info here)
myNumber = Math.Ceiling(myNumber / 10) * 10
Deviding it first by 10 and then multiplying it again with 10 will do the trick.
Update: in case you are wondering, there are no problems with Integer
This is an Integer-based solution.
myNumber = If(myNumber Mod 10 = 0, myNumber, If(myNumber > 0, 10, 0) + 10 * (myNumber \ 10))
It doesn't suffer from rounding and is also is at least 2x faster than using Math.Ceiling.

How to see what the is being compared in a if statement

I'm having a problem with some vba code.
I have a if statement that doesn't treat the same content equally.
e.g: 0,1 equals 0,1, but a re-run 0,1 does not equal 0,1
(this values are shown by MVBA)
The code is long so before posting it i would like to know if it's possible to see the machine perspective in a if statement (hex, ascii...). This because, although the debug is telling me they are the same (through msgbox, vartype, etc), the if statement is not activated.
pseudo code:
x = 0,0000001 * 1*10^6 (which equals 0,1)
y = 0,0001 * 1*10^3 (which also equals 0,1)
if statement:
x doesn't enter
y does
end if
This is because the floating-point implementation may not be able to represent those number accurately due to the fact that they are encoded in a base 2 representation.
If you want to compare them, I would suggest using Cdec (wich converts to Decimal, a VBA custom base 10 floating-point)
Debug.Print (0.0000001 * 1 * 10 ^ 6) = (0.0001 * 1 * 10 ^ 3) ' False
Debug.Print CDec(0.0000001 * 1 * 10 ^ 6) = CDec(0.0001 * 1 * 10 ^ 3) ' True
While they both display 0.1, in fact 0.0000001 * 1 * 10 ^ 6 flaoting-point value is 0x3FB9999999999999 whereas 0.0001 * 1 * 10 ^ 3 returns 0x3FB999999999999A.
I'd recommend reading What Every Computer Scientist Should Know About Floating-Point Arithmetic

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;

vb script to javascript

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.