how do i find a Reciprocal through Visual Basic?
thanks in advance
Printable View
how do i find a Reciprocal through Visual Basic?
thanks in advance
what do you mean by a reciprocal?
give us an example
What is wrong with dividing one by the number?Does "reciprocal" mean something else to you?Code:Dim Number As Double
Dim Reciprocal As Double
Reciprocal = 1/Number
Dividing the number by one will only work for an integer. For example dividing 2/3 by one won't give you 3/2.
Just a quick thought,
SunnyCode:Public Function MakeReciprocals(fraction As String) As String
Dim pos As Integer
Dim p1 As String
Dim p2 As String
pos = InStr(fraction, "/")
p1 = Mid(fraction, 1, Len(fraction) - pos)
p2 = Mid(fraction, pos + 1)
MakeReciprocals = p2 & "/" & p1
End Function
by a reciprocal i mean like this:
2/1 = 1/2
.25 = 4/1
3/2 = 2/3
etc.
and Sunnyl:
that code woun't always work like if its .25, it will not give 4/1 or 4
Dividing by one results in doing nothing. Divide one by the number to get reciprocal.
If number is expressed as single or double, the code I previously posted will work.
If you have numerator and denominator of a fraction, turn fraction upside down. Numerator becomes denominator and vice versa,, resulting in reciprocal.
1/4 = .25 (.25 is reciprocal of 4).
1/.25 = 4 (4 is reciprocal of .25).
1/(4/3) = 3/4 (3/4 is reciprocal of 4/3, and vice versa)
I understood what you meant, but statements like "3/2 = 2/3" give mathematicians a queasy feeling.
Similar to Guv's way this way will also work. It is the same mathematical function, but one may be faster.
Take the number to the -1 power.
Example:
(0.25)^-1 = 4
Hope that this helps!
I don't wish to contradict overhill, but I will. x^-1 is very much slower than 1/x. the ^ operator is very slow (because of the way the FPU works, nothing to do with VB)
Thanks for the comment Sam. I had a feeling that powers were probably slower, but you confirmed it. I just threw out my way of doing it when using a calculater. In that environment it is simpler just to take the number to the -1 power. Thanks.
thanks to all for your help