Simplifying Nested Ternary Conditionals

Started by
5 comments, last by sooner123 12 years ago
I'm writing some VBA that does calculations with complex numbers. Here is my code that prints a complex number a+bi:

IIf(b = 0, a, IIf(a <> 0, a & " + ", "") & b & "i")

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)
Advertisement
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.
IIf(a <> 0, a & " + ", "")
IIf(b <> 0, b & "i", "")

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!

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"


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)


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.
IIf(a <> 0, a & " + ", "")
IIf(b <> 0, b & "i", "")

[/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"
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:
IIf(b = 0, a, IIf(a = 0, b & "i", a & " + " & b & "i"))

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?
Gotcha thanks. I prefer your way too.
Oh, I just realized that this wouldn't look too good if a is not zero and b is negative... I remember having written code for this in the past and it not being pretty.
Yeah nor does it look great if |b| = 1 imo. It looks overall like it's best to handle several cases.

This topic is closed to new replies.

Advertisement