#1   Report Post  
Posted to microsoft.public.excel.misc
Jeff
 
Posts: n/a
Default using a function

This problem is driving me crazy - please help.

The problem in a nutshell is as follows:

Sub Macro2()
Dim xxx As Double
xxx = 0.05
Call NewFuntio(xxx)
End Sub

Sub NewFuntio(temp)
Dim t1,t2 As Double
t1 = (temp + 1) ^ (1 / 12)
t2 = 1.05^(1/12)
End Sub

t1 = 1.00407412390531
t2 = 1.00407412378365

t1 is the wrong answer and t2 is the correct answer.

Why is the function giving the wrong answer?

Thanks

  #2   Report Post  
Posted to microsoft.public.excel.misc
Bernie Deitrick
 
Posts: n/a
Default using a function

Jeff,

t1 and t2 are identical for me - perhaps try dimensioning better....

Sub Macro2()
Dim xxx As Double
xxx = 0.05
Call NewFuntio(xxx)
End Sub

Sub NewFuntio(temp)
Dim t1 As Double
Dim t2 As Double
t1 = (temp + 1) ^ (1 / 12)
t2 = 1.05 ^ (1 / 12)
MsgBox t1 & Chr(10) & t2

End Sub

HTH,
Bernie
MS Excel MVP


"Jeff" wrote in message
...
This problem is driving me crazy - please help.

The problem in a nutshell is as follows:

Sub Macro2()
Dim xxx As Double
xxx = 0.05
Call NewFuntio(xxx)
End Sub

Sub NewFuntio(temp)
Dim t1,t2 As Double
t1 = (temp + 1) ^ (1 / 12)
t2 = 1.05^(1/12)
End Sub

t1 = 1.00407412390531
t2 = 1.00407412378365

t1 is the wrong answer and t2 is the correct answer.

Why is the function giving the wrong answer?

Thanks



  #3   Report Post  
Posted to microsoft.public.excel.misc
Bob Phillips
 
Posts: n/a
Default using a function

When you declare two variables like that

Dim t1, t2 as Double

you are not declaring two doubles as you think, but one variant, one double.

Use

Sub Macro2()
Dim xxx As Double
xxx = 0.05
Call NewFuntio(xxx)
End Sub

Sub NewFuntio(temp)
Dim t1 As Double, t2 As Double
t1 = (temp + 1) ^ (1 / 12)
t2 = 1.05 ^ (1 / 12)
End Sub

--

HTH

Bob Phillips

(replace xxxx in the email address with gmail if mailing direct)

"Jeff" wrote in message
...
This problem is driving me crazy - please help.

The problem in a nutshell is as follows:

Sub Macro2()
Dim xxx As Double
xxx = 0.05
Call NewFuntio(xxx)
End Sub

Sub NewFuntio(temp)
Dim t1,t2 As Double
t1 = (temp + 1) ^ (1 / 12)
t2 = 1.05^(1/12)
End Sub

t1 = 1.00407412390531
t2 = 1.00407412378365

t1 is the wrong answer and t2 is the correct answer.

Why is the function giving the wrong answer?

Thanks



  #4   Report Post  
Posted to microsoft.public.excel.misc
 
Posts: n/a
Default using a function

"Jeff" wrote:
t1 = 1.00407412390531
t2 = 1.00407412378365
t1 is the wrong answer and t2 is the correct answer.
Why is the function giving the wrong answer?


First, you should identify the version of Excel that you are using. I see
no difference with my version of Excel, Office Excel 2003, at least when I
use msgbox to look at t1 and t2.

Sub NewFuntio(temp)
Dim t1,t2 As Double
t1 = (temp + 1) ^ (1 / 12)
t2 = 1.05^(1/12)


Try changing the "dim" declaration to:

dim t1 as double, t2 as double

which I suspect is your intent.

As you have written, t1 is declared a variant, not a double. I suspect that
in your version of Excel, variants that contain floating point numbers are
treated as "single", double. I was unable to duplicate your results exactly
by forcing t1 to be type single, but that might be because I am not being
careful with the type of intermediate computations. Nonetheless, I do get a
difference when I store t1 into a double (t3).

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a Custom Excel Function to Calculate Gini Coefficients [email protected] Excel Worksheet Functions 3 February 21st 06 11:15 PM
Date & Time mully New Users to Excel 4 May 23rd 05 12:56 PM
Hyperlinks using R[1]C[1] and offset function in its cell referenc Elijah-Dadda Excel Worksheet Functions 0 March 5th 05 04:31 AM
Conversion SVC Excel Worksheet Functions 9 February 28th 05 03:29 PM
HOW CAN I GET OFFICE 2003 EXCEL BASIC TO NEST FUNCTIONS LIKE EXCE. Robert AS Excel Worksheet Functions 4 December 2nd 04 11:49 AM


All times are GMT +1. The time now is 08:21 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"