473,385 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

ternary operator and casting

In the following code

//add to bool or double Dictionary
this.m_unit.Add((unittype == "b")? unitnum:(double)unitnum);

The bool dictionary uses an integer index, the double uses a double
index, which some may take issue with, but that's a different
discussion. The problem is that unitnum is always cast to a double, even
when unittype is "b". That seems like a bug to me.

If I change it to

this.m_unit.Add((unittype == "d")? (double)unitnum:unitnum);

it still always casts it to a double. I'm annoyed that the ternary
operator always casts unitnum to a double.

(parenthetical discussion)

The object is to later be able to set values, like so

//1234 is a bool
Grp.Unit[1234] = true;
//4321 is a double
Grp.Unit[4321d] = 1.0;
//there is also a uint index for conversion between bool and double
//set bool from double
Grp.Unit[1234u] = 1.0;
//set double from bool
double myvalue = Grp.Unit[1234u];

but, like I said, that's not the discussion I want to have. There is a
lot of code, and it minimizes it, especially when dealing with
bool-to-double conversions.

*** Sent via Developersdex http://www.developersdex.com ***
Jul 16 '08 #1
8 2095
On Jul 16, 4:22*pm, Bob Hoeppner <boba...@hotmail.comwrote:
In the following code

//add to bool or double Dictionary
this.m_unit.Add((unittype == "b")? unitnum:(double)unitnum);

The bool dictionary uses an integer index, the double uses a double
index, which some may take issue with, but that's a different
discussion. The problem is that unitnum is always cast to a double, even
when unittype is "b". That seems like a bug to me.
No - it's a flaw in your understanding of the conditional operator.

The conditional operator is (as you know) an expression of the form
a ? b : c. That expression is of a single type, in the end. Both the
subexpressions "b" and "c" have to be convertible to the overall type
of the expression, which (IIRC) is always either the type of b or the
type of c.

In your case, you end up with b and c being unitnum (type=int) and
(double)unitnum (type=double). int is implicitly convertible to
double, but not vice versa - so the overall type of the expression is
double. That means you always get the Add(double) overload being
called, and there is indeed always a conversion.

Assuming the Add method is overloaded (once for int and once for
double) then you need to have two different method calls to get it to
work. Any one method call will only resolve to a single overload.

Then again, it's quite strange to see an overload like that, so it's
possible that my assumptions about the type of m_unit are incorrect.
If the explanation above hasn't helped you, could you provide a short
but complete program which demonstrates the problem?

Jon
Jul 16 '08 #2
On Wed, 16 Jul 2008 08:22:32 -0700, Bob Hoeppner <bo*****@hotmail.com
wrote:
In the following code

//add to bool or double Dictionary
this.m_unit.Add((unittype == "b")? unitnum:(double)unitnum);

The bool dictionary uses an integer index, the double uses a double
index, which some may take issue with, but that's a different
discussion. The problem is that unitnum is always cast to a double, even
when unittype is "b". That seems like a bug to me.
It's not a bug. The type of the expression has to be determined at
compile-time, so the compiler cannot take into account possible
differences in the two possible outcomes of the operator. It has to
resolve the entire expression into a single, compile-time type. It does
this by choosing the type that both possible outcomes can be converted
to. There's no implicit conversion from double to int, but there's an
implicit conversion from int to double, so double wins.

Assuming "unitnum" is an integer variable, then I'm not clear on why
you're casting in the first place. Where the expression needs to resolve
to a double, the implicit conversion should handle that for you.

Beyond that, I'm not entirely sure that the use of a double is "a
different discussion". To some extent, this is an issue for you because
you are trying to treat two different collections as the same, so there's
at least that. The other issue is that a dictionary is going to use
equality for matching keys, but floating point values are poor candidates
for equality comparisons.

In other words, you do in fact have a potential design issue that has led
to this dilemma, and fixing that design issue may in fact cause your
immediate question to become moot.

Pete
Jul 16 '08 #3
Yes, I see my understanding was flawed. It makes sense the one ternary
operator would have to return one datatype. This demonstrates that the
ternary operator is not an exact shorthand equivalent for an if/else
statement. Thanks.

*** Sent via Developersdex http://www.developersdex.com ***
Jul 16 '08 #4
Yes, I see my understanding was flawed. It makes sense the one ternary
operator would have to return one datatype. This demonstrates that the
ternary operator is not an exact shorthand equivalent for an if/else
statement. Thanks.

Yes, I'm treating two collections as similarly as possible. There are
many bools and fewer doubles. Also, there is a lot of assigning of bools
to doubles and doubles to bools, due to the nature of the two systems
I'm interfacing. I'm aware it's a quirky design decision, and it's not
one I made on my own. The upside is that there is less code and more
convenience; the downside is that it may be trickier for humans to read
the code, and there is a slight performance penalty for using doubles as
an index. If it proves to be insupportable we can refactor it. Thanks
again.

*** Sent via Developersdex http://www.developersdex.com ***
Jul 16 '08 #5
On Jul 16, 1:29*pm, Bob Hoeppner <boba...@hotmail.comwrote:
Yes, I see my understanding was flawed. It makes sense the one ternary
operator would have to return one datatype. This demonstrates that the
ternary operator is not an exact shorthand equivalent for an if/else
statement. Thanks.

Yes, I'm treating two collections as similarly as possible. There are
many bools and fewer doubles. Also, there is a lot of assigning of bools
to doubles and doubles to bools, due to the nature of the two systems
I'm interfacing. I'm aware it's a quirky design decision, and it's not
one I made on my own. The upside is that there is less code and more
convenience; the downside is that it may be trickier for humans to read
the code, and there is a slight performance penalty for using doubles as
an index. If it proves to be insupportable we can refactor it. Thanks
again.

*** Sent via Developersdexhttp://www.developersdex.com***
I find a little weird that double to bool (and viceversa) asignation.
considering that bool as only two possible values.

could you explain a little more what are you doing?
Jul 16 '08 #6
this.m_unit.Add((unittype == "b")? unitnum:(double)unitnum);
>
The bool dictionary uses an integer index, the double uses a double
index, which some may take issue with, but that's a different
discussion. The problem is that unitnum is always cast to a double, even
when unittype is "b". That seems like a bug to me.
I just looked in the C# Annotated Standard...

The semantics of the ternary operator requires the type of the expression to
be determined at compiled time. So (condensing several rules into one
sentence) it is whatever is compatible with both of the expressions after
the ? .
Jul 16 '08 #7
I've entered into that discussion on this thread

http://www.developersdex.com/csharp/...1111&r=6224121
Suffice it to say that the overloading of the indexers allows some more
concise code by people programming to the class, which is how those
coding to it prefer. We went over the various ways it could be
implemented. Some lines can get quite long. Here's a shorter one:

Grp.Unit[2770] = FUN(Grp.Unit[772] && !Grp.Unit[2771], ref
Grp.Unit[1800d], Grp.Unit[773d]); // Line 1290

which isn't much of an advantage over, say,

Grp.UnitB[2770] = FUN(Grp.UnitB[772] && !Grp.UnitB[2771], ref
Grp.UnitD[1800], Grp.UnitD[773]); // Line 1290

but when going between booleans and doubles, is, as described in my post
in the other thread.


*** Sent via Developersdex http://www.developersdex.com ***
Jul 16 '08 #8
Bob Hoeppner wrote:
Yes, I see my understanding was flawed. It makes sense the one ternary
operator would have to return one datatype. This demonstrates that the
ternary operator is not an exact shorthand equivalent for an if/else
statement.
I think you in general should avoid ?: with different data typesto keep
the code easy to read.

Arne
Jul 17 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: praba kar | last post by:
Dear All, I am new to Python. I want to know how to work with ternary operator in Python. I cannot find any ternary operator in Python. So Kindly clear my doubt regarding this ...
6
by: glongword | last post by:
As the assert macro should evaluate to a void expression, it should not have an 'if statement' in its definition. Does this necessitate the existence of a ternary conditional operator? Is there a...
6
by: Robert Zurer | last post by:
In his paper on Coding Standard found on http://www.idesign.net/idesign/DesktopDefault.aspx Juval Lowy discourages the use of the ternary conditional operator with no specific reason given. ...
2
by: Darren | last post by:
Hi there, Recently I have changed languages from VB.NET to C#. While in VB.NET, I wrote the below code to determine if a specific product in a repeater control had an image. If so, it would...
48
by: Daniel Crespo | last post by:
Hi! I would like to know how can I do the PHP ternary operator/statement (... ? ... : ...) in Python... I want to something like: a = {'Huge': (quantity>90) ? True : False} Any...
15
by: Arthur Dent | last post by:
Hi all, im just curious if anyone knows..... With .NET 2, VB didnt happen to get a true ternary operator, did it? Stuck away in a corner somewhere? By ternary, i mean something like C's a?b:c...
5
by: PerlPhi | last post by:
hi,,, while ago i was wondering why do some programmers rarely uses the ternary operator. wherein it is less typing indeed. i believe in the classic virtue of Perl which is laziness. well let me show...
4
by: raiderdav | last post by:
I understand how the ternary operator (question mark - ?) works in an if/else setting, but what does it mean when used as a type? For instance, I'm trying to add a get/set in some existing code,...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.