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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Implementing the Natural Logarithm in my Visual Basic v6.0 code . . .

Status
Not open for further replies.

Phalken

Programmer
Jan 8, 2004
24
US
I am having a difficult time finding information on how to use the Natural Logarithm in my Visual Basic v6.0 code. If anyone is familiar using the Natural Logarithm with Visual Basic, please give me a hand.

Here is my code example:

txtTime.Text = LN((sngT_Coffee - sngT_Ambient) / (75)) / (-0.1)

Note: LN is where the Natural Logarithm is to be included for my calculation

Thank you all for any help to come!
 
Natural log (often known as Napierian logarithms) are logarithms to base e

The VB function Log(myNumber) returns the natural log of myNumber

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

'If we're supposed to work in Hex, why have we only got A fingers?'
 
I am sorry johnwm. I understand how to calculate my problem and cancel out everything perfect while using the natural logarithm, but I am unfamiliar with substituting the LN with log because of the cancelation differences.

Code example:

txtTime.Text = LN((sngT_Coffee - sngT_Ambient) / (75)) / (-0.1)

Note for my code example:
sngT_Coffee = 25
sngT_Ambient = 20

While using the Natural Logarithm the answer results:

txtTime.Text = LN((25 - 20) / (75)) / (-0.1)

Thus txtTime.Text = ~27.1 minutes

So is there actually not a Natural Logarithm function built into Visual Basic v6.0 ? If there is not, can you please suggest and help me with an alternative other than using the log function? Again I am sorry, but I am unfamiliar with solving many problems using log compared to LN!

Thanks for any help.
 
Sorry all!

I just needed to divide my solution with log(e)

Wohoo! Applied Learning!

Although I beleive Visual Basic should include a Natural Logarithm function!
 
I am still having a problem . . . [Grrrrr] ;)


Here is my code:

Private Sub cmdCalculate_Click()

Dim sngT_Coffee As Single
Dim sngT_Ambient As Single
'Dim sngTime As Single

If optT_Coffee_C = True Then
sngT_Coffee = (txtT_Coffee.Text) / (1) 'Keeps the celsius temperature inputted into textbox "Temperature of Coffee" the same in the formula calculation.

If optT_Coffee_F = True Then
sngT_Coffee = (5 / 9) * (txtT_Coffee.Text - 32) 'Converts fahrenheit temperature inputted into textbox "Temperature of Coffee" to celsius in the formula calculation.

If optT_Ambient_C = True Then
sngT_Ambient = (txtT_Ambient.Text) / (1) 'Keeps the celsius temperature inputted into textbox "Ambient Temperature" the same in the formula calculation.

If optT_Ambient_F = True Then
sngT_Ambient = (5 / 9) * (txtT_Ambient.Text - 32) 'Converts fahrenheit temperature inputted into textbox "Ambient Temperature" to celsius in the formula calculation.

'If optTime_s = True Then
'sngTime = (txtTime.Text) * (60) 'Converts the time in seconds inputted into textbox "Time" to minutes in the formula calculation.
'
'If optTime_m = True Then
'sngTime = (txtTime.Text) / (1) 'Keeps the time in minutes inputted into textbox "Time" the same in the formula calculation.
'
'If optTime_h = True Then
'sngTime = (txtTime.Text) / (60) 'Converts the time in hours inputted into textbox "Time" to minutes in the formula calculation.

txtTime.Text = (Log((sngT_Coffee - sngT_Ambient) / (75)) / (-0.1)) / Exp(1) 'This may not convert the solution in seconds, minutes, and hours

End Sub


Private Sub cmdExit_Click()
' Unloads the form from system memory and terminates the application.
Unload frmMain
End
End Sub

txtTime.Text should = ~27.1 minutes
and I am using Exp(1) as an identity of log(e) in Visual Basic v6.0 language

'HERE IS THE ERROR I RECEIVED
'Compile Error: Block If without End If
'Private Sub cmdCalculate_Click() is highlighted in yellow
'End Sub is highlighted in dark blue

Please help me understand why I received this error. I beleive it has something to do with me introducing the log into my calculation. Everything else in my code and form are solid.
 
SORRY AGAIN! I forgot to include the End If
 
Thank you all for your responses.
Below is my working code [and it works - wohoo! ;)]

Private Sub cmdCalculate_Click()

Dim sngT_Coffee As Single
Dim sngT_Ambient As Single
Dim sngTime As Single

If optT_Coffee_C = True Then
sngT_Coffee = (txtT_Coffee.Text) / (1) 'Keeps the celsius temperature inputted into textbox "Temperature of Coffee" the same in the formula calculation.
End If

If optT_Coffee_F = True Then
sngT_Coffee = (5 / 9) * (txtT_Coffee.Text - 32) 'Converts fahrenheit temperature inputted into textbox "Temperature of Coffee" to celsius in the formula calculation.
End If

If optT_Ambient_C = True Then
sngT_Ambient = (txtT_Ambient.Text) / (1) 'Keeps the celsius temperature inputted into textbox "Ambient Temperature" the same in the formula calculation.
End If

If optT_Ambient_F = True Then
sngT_Ambient = (5 / 9) * (txtT_Ambient.Text - 32) 'Converts fahrenheit temperature inputted into textbox "Ambient Temperature" to celsius in the formula calculation.
End If

sngTime = Format(Log((sngT_Coffee - sngT_Ambient) / (75)) / (-0.1), "###,##0.000")

If optTime_s = True Then
txtTime.Text = sngTime * 60 'Converts the time in minutes that was calculated as sngTime to time in seconds into textbox "Time". Note, you must click the "Calculate" command button again to see this conversion.
End If

If optTime_m = True Then
txtTime.Text = sngTime / 1 'Keeps the time in minutes that was calculated as sngTime unchanged. This value is inputted into textbox "Time".
End If

If optTime_h = True Then
txtTime.Text = sngTime / 60 'Converts the time in minutes that was calculated as sngTime to time in hours into textbox "Time". Note, you must click the "Calculate" command button again to see this conversion.
End If

End Sub


Private Sub cmdExit_Click()
' Unloads the form from system memory and terminates the application.
Unload frmMain
End
End Sub
 
It might 'work', but it does not generally conform to 'good practice' programming.

It is possible that some of this is accomplished in the form set upm but:

There is NO error Check, so a error in entering hte tempratures will stop the procedure w/ an error message.

There is no provision for either option (set / group) to not have one of the controls selected. It is more common to place the collection(s) of options in an option group ('frame'), setting one of the options as the default and using the option group value as the determinant in the calculation selection - usually threough the use af a select case statement.

The use of multipls IF blocks for the possible conversion of the tempratures is not only unnecessary, but could (in a longer procedure) cause additional calculation time to be expended.

The repition of the division of the 'constant' (5/9) could also cause additional calculation delay, although this specific procedure is short enough that it would hardly be noticable.

Formatting the calculation oif the time in the initial calculation is totally without merit, as the format is removed on the subsquent conversion (to Sec, Min or Hrs). where (again) trhe use of an option group value with a default would be more appropiate than three seperate If blocks.

Hopefully, you are a beginning student (at least of programming) somewhere and are not perpetuating these practices in a commercial product.




MichaelRed
mlred@verizon.net

 
Actually I am not a student at all - I am self-taught and lack many skills when it comes to efficient programming.

However I must mention that the options in my form are grouped into seperate frames (three seperate frames, one for each textbox).

If you feel so inclined I would greatly appreciate it if you happened to have some free time and would show me how you would handle writing this code to be more efficient. I am a very quick learner and maybe I will gain some much needed insight in my coding.

Thanks for the advice either way!

If you would like me to Upload the VB6.0 project to you please ask. Possibly you will not need the form and can visualize the project. It is simple with three textboxes, three frames (one for each textbox for the option buttons), one calculate command button, and one exit command button.

Thanks again.
 
w/o any knowlege of the overall app, or the intended use of the procedure(s), it is not really pratical to provide a 'complete' critique of your approach. I did 'translate' your procedure to a minimally modified independent procedure, incorporating some of the issues noted in my previous post:

Code:
Public Function basCalc(opgUnits As Integer, _
                         sngTmpCoffee As Single, _
                         sngTmpAmbient As Single, _
                         opgTime As Integer) As Single

    Dim sngT_Coffee As Single
    Dim sngT_Ambient As Single
    Dim sngTime As Single

    Const sngConv = 5 / 9

    On Error GoTo ErrExit

    Select Case opgUnits

        Case Is = 1                             'Keep the temps AS Deg C
            sngT_Coffee = sngTmpCoffee
            sngT_Ambient = sngTmpAmbient

        Case Is = 2                             'Convert to  Deg F
            sngT_Coffee = sngConv * (sngTmpCoffee - 32#)
            sngT_Ambient = sngConv * (sngTmpAmbient - 32#)

    End Select

    sngTime = Log((sngT_Coffee - sngT_Ambient) / (75)) / (-0.1)

    Select Case opgTime

        Case Is = 1                               'Convert to Seconds
            basCalc = sngTime * 60

        Case Is = 2                               'Keeps in minutes
            basCalc = sngTime

        Case Is = 3                               'Converts the time to hours
            basCalc = sngTime / 60

    End Select

    GoTo NormExit

ErrExit:
    basCalc = -1

NormExit:
    Exit Function

End Function
[code]

Of course, this represents only some very basic changes, and would require some adjustment in your processing:

This procedure would normally be placed in a 'General' module, not a forms module.

The input arguments 'arguments' would be taken from the controls on the form, with the Gption Group selections replacing the individual options and the two temprature values from the textboxes.

It would be left to the calling function (your command button) to assign the output to the test box used for your results.

The call to this procedure (in the immediate window) might look like:

[code]
? basCalc(1, 25, 30, 1)
-1 

? basCalc(1, 25, 20, 1)
 1624.83 

? basCalc(1, 25, 20, 2)
 27.0805


Your calling procedure (cmd button) would of course assign the return to thte text box for the results.


Additional issues would be:

Clear the results when ANY of the input args change, as failure to do so leaves an aparent incorrect result displayed.

Modify the label / caption of the various controls to CLEARLY indicate the units of the variables and the results.



MichaelRed
mlred@verizon.net

 
I briefly read through all the post so I may have missed it. The Natural Logarithm of any number x is defined as follows:

ln x = log10(x)/log10(e)
where e = 2.71828182...
Also logn(x) = log(x)/log(n)

Therefore

Lets say x = 3; then ln3 = log10(3)/log10(2.71)
ln3 = (log(3)/log(10))/(log(2.71)/log(10))
 
The VB 6 LOG() function already returns the Natural Logarithm.

Log Function

Returns a Double specifying the natural logarithm of a number.

Syntax

Log(number)

The required number argument is a Double or any valid numeric expression greater than zero.

Remarks

The natural logarithm is the logarithm to the base e. The constant e is approximately 2.718282.



Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Thanks for the confirmation JohnYingLing - that's what I said in my first reply!

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

'If we're supposed to work in Hex, why have we only got A fingers?'
 
strongm and JohnY,
[smile]

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

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top