Conversion from C to VBA

I am attempting to convert the following C code (being run on Solaris) to VBA for use in Excel for the purposes of client presentation:

theta = r / (q * q * q)
if(fabs((double)theta > 1.)
theta = (theta > 0.) ? 1. : -1.;
theta = acos( theta ) / 3. ;

My VBA conversion:

Theta = R / (Q * Q * Q)
If Abs(Theta) > 1 Then
If Theta > 0 Then Theta = 1
If Theta <= 0 Then Theta = -1
End If
Theta = Application.WorksheetFunction.Acos(Theta) / 3

I am wondering first of all, why there is a (double) definition in the C code since theta has been defined previously as double in that code. I am assuming secondly that the VBA Abs is the same as the C code fabs. Lastly, I'm wondering if there is any reason to believe that a calculation of Acos on a Sun machine would be any different from that calculated on a windows PC. I ask because the answers I'm getting from my code are significantly different from the original C code. The section of code here is the only part about which I have doubts.

Thanks for any help
Paul Hudgens
Denver
What's with that stray End If?
The End If is necessary in VBA when there are 2 or more statements following the original if - then.
Oh. I didn't see the first if.

I don't understand your first question:
I am wondering first of all, why there is a (double) definition in the C code since theta has been defined previously as double in that code.


is any reason to believe that a calculation of Acos on a Sun machine would be any different from that calculated on a windows PC.
I think different IEEE standards define different results for floating point operations, but I'm not sure.
Why is the redefinition of theta ("(double)") being used in the following line of code when theta has been defined as double earlier in the C program?:

if(fabs((double)theta > 1.)


Thanks,
Paul H.
That's not a redefinition (there's no such thing as a legal redefinition), it's a cast. A redundant cast, but a cast nonetheless.

By the way, that C if is not valid. Is that an error in the copy you posted, or is that how the code actually looks?
Last edited on
closed account (z05DSL3A)
If you call if(fabs(theta > 1.))[*], you would probably get a error about an ambiguous call to an overloaded function. casting the result of theta > 1. to double removes the ambiguity.


[*] Your missing a closing parenthesis in you post.
The full if statement is as follows:

if(fabs((double)theta > 1.)
theta = (theta > 0.) ? 1. : -1.;


I assumed that the "(double)" was redundant and that my VBA code would therefore work:

If Abs(Theta) > 1 Then
If Theta > 0 Then Theta = 1
If Theta <= 0 Then Theta = -1
End If

As I say though, my results do not match with those coming from our Sun based software, if not because of these lines than because of some others. I may need to find a C expert to help with this - can anyone recommend someone in the Denver area? I'm only converting about 50 lines of C code.

Thanks,
Paul H.
closed account (z05DSL3A)
I can see the if statment from your fist post
1
2
3
4
theta = r / (q * q * q)
if(fabs((double)theta > 1.)
    theta = (theta > 0.) ? 1. : -1.;
theta = acos( theta ) / 3. ;


but the if stament that you have posted has 3 (s but only 2 )s, so one is missing somewhere.

My take on the VBA would be somthing like:
1
2
3
4
5
6
7
If Abs(theta) > 1 Then
    If theta > 0 Then
        theta = 1
    Else
        theta = -1
    End If
End If
Last edited on
You're right - there is an extra ) after theta in the original C code (of which I only have a hardcopy). C code should be:

if(fabs((double)theta) > 1.)
theta = (theta > 0.) ? 1. : -1.;
theta = acos( theta ) / 3. ;

My VBA code would have worked, but your's is technically better. Thanks.
Topic archived. No new replies allowed.