Sign in to follow this  

Simplifying Nested Ternary Conditionals

This topic is 2081 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm writing some VBA that does calculations with complex numbers. Here is my code that prints a complex number a+bi:

[code]IIf(b = 0, a, IIf(a <> 0, a & " + ", "") & b & "i")[/code]

My question is twofold:

1. How can I simplify this further? 'a' is included in the expression twice and I'm sure there is a better way/order to wrap the conditionals so that one of those 'a's is removed without adding another term.

2. What method did you use to simplify this? Is there a mathematical technique for simplifying these things? (if the outputs were binary I know I could use sum-of-products as in circuits and some boolean logic to simplify it)

Share this post


Link to post
Share on other sites
I'm not familiar with VBA, but I see a couple ways to improve this:

1. I see you are aiming for a number not to appear if it equals 0. In this regard, your code is flawed. Take for example a = 0 and b = 0, or 0. I know, silly, but: b = 0 is true, then print a => output is "0".
2. The else statement seems actually independent from the exterior conditional, so those two should actually be two distinct conditionals. It's good practice and will help the code to be more clear.
[code]IIf(a <> 0, a & " + ", "")
IIf(b <> 0, b & "i", "")[/code]
3. Like I stated, I do not know VBA, but it would be good if you could save and accumulate the characters that you want to print in a String or array of characters in case you need to modify it further on.
4. Even though the use of the 'a' twice is gone, I can't come up with any more clever, mathematical solution to simplify the code. Sorry!

Share this post


Link to post
Share on other sites
[quote name='Daniel_Whisper' timestamp='1333486606' post='4928001']
1. I see you are aiming for a number not to appear if it equals 0. In this regard, your code is flawed. Take for example a = 0 and b = 0, or 0. I know, silly, but: b = 0 is true, then print a => output is "0"[/quote]

Well actually I wanted it to output 0 if a and b are both 0. So I think my code is correct in this case. That's why I had it print 0. I was trying to isolate all the combinations and realized the ones where b=0, the final output = a (whether a is zero or not)

[quote]
2. The else statement seems actually independent from the exterior conditional, so those two should actually be two distinct conditionals. It's good practice and will help the code to be more clear.
[code]IIf(a <> 0, a & " + ", "")
IIf(b <> 0, b & "i", "")[/code]
[/quote]

Hmm but is it independent? It seems like they are dependent since the whole thing evaluates to 0 if and only if both the real and imaginary components are zero. For this reason, I think it requires a nested conditional.

The code isn't really meant to be language specific, it was just a ternary operator. Here it is in C++:

b = 0 ? a : (a <> 0 ? a & " + " : "") & b & "i"

Seems like this may be able to be done in one less operand, but I'm not sure how because I don't know how to optimize these things.

Here is the table:

a ZERO, b ZERO ==> 0 (or a in order to reduce the number of possibilities from four to three)
a ZERO, b NONZERO ==> b & "i"
a NONZERO, b ZERO ==> a
a NONZERO, b NONZERO ==> a & " + " & b & "i"

Share this post


Link to post
Share on other sites
Your code seems perfectly efficient to me. I don't think you are doing any extra operations. I wouldn't worry too much about repeating something in the expression. You should worry more about the expression being easy to understand. I would probably do it like this:
[code]IIf(b = 0, a, IIf(a = 0, b & "i", a & " + " & b & "i"))[/code]

I find that easier to read because I can see all three forms (`a', `b & "i"' and `a & " + " & b & "i"') clearly in the code. And yes, I now also repeat `b & "i"', but who cares?

Share this post


Link to post
Share on other sites

This topic is 2081 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this