(4/4)%2 = 1? wtf?
-
Annoyingly enough, DataColumn.Expression doesn't support bitwise operators, so I decided to do it the long way. Also, annoyingly I can't modulo a double by an int, thus forcing me to convert to int. This leaves me with a column expression that looks like:
Convert((SomeColumn/4),'System.Int32')%2 = 1
Lo and behold my ire as I realize that the Convert function rounds the argument to its nearest whole value. Thus 3/4 = .75 Then: Convert(.75,Int32) = 1 and 1%2 = 1 This irritates me, especially since I'm trying to work through the DataSet Designer in VS 2k5 and subsequently trying to write as little code as possible. I've got a couple ideas for working around this - mostly involving writing in my own events or properties - but I'm curious to see if anyone has any other solutions. -
Annoyingly enough, DataColumn.Expression doesn't support bitwise operators, so I decided to do it the long way. Also, annoyingly I can't modulo a double by an int, thus forcing me to convert to int. This leaves me with a column expression that looks like:
Convert((SomeColumn/4),'System.Int32')%2 = 1
Lo and behold my ire as I realize that the Convert function rounds the argument to its nearest whole value. Thus 3/4 = .75 Then: Convert(.75,Int32) = 1 and 1%2 = 1 This irritates me, especially since I'm trying to work through the DataSet Designer in VS 2k5 and subsequently trying to write as little code as possible. I've got a couple ideas for working around this - mostly involving writing in my own events or properties - but I'm curious to see if anyone has any other solutions.jchalfant wrote:
I can't modulo a double by an int, thus forcing me to convert to int
just convert the int to a double then... I am not sure I understand the problem. (4/4)%2 = 1 is correct. What are you trying to do?
xacc.ide - now with IronScheme support
IronScheme - 1.0 alpha 2 out now -
jchalfant wrote:
I can't modulo a double by an int, thus forcing me to convert to int
just convert the int to a double then... I am not sure I understand the problem. (4/4)%2 = 1 is correct. What are you trying to do?
xacc.ide - now with IronScheme support
IronScheme - 1.0 alpha 2 out nowWhile C# can handle double % double, ADO.Net expressions can't. I've tried that, no love. Doing so produces a MessageBox at runtime saying Cannot perform 'Mod' operation on System.Double and System.Double. What I'm trying to do is get around the lack of bitwise operation support in ADO.Net expressions. In doing so I was hoping to be able to easily bind some control properties to boolean values in a datatable whose values are determined by an expression (somecolumn/somenumber)%2=1. Unfortunately I don't think this is possible. Oh, and the title, is an oops. What I meant was 3/4%2.
-
While C# can handle double % double, ADO.Net expressions can't. I've tried that, no love. Doing so produces a MessageBox at runtime saying Cannot perform 'Mod' operation on System.Double and System.Double. What I'm trying to do is get around the lack of bitwise operation support in ADO.Net expressions. In doing so I was hoping to be able to easily bind some control properties to boolean values in a datatable whose values are determined by an expression (somecolumn/somenumber)%2=1. Unfortunately I don't think this is possible. Oh, and the title, is an oops. What I meant was 3/4%2.
-
jchalfant wrote:
What I meant was 3/4%2.
That makes sense. Maybe you can return the boolean values directly in your result set or however you retrieve them.
xacc.ide - now with IronScheme support
IronScheme - 1.0 alpha 2 out nowThat's one of my conclusions. But what I think I'll do instead is manually add the column to the datarow and evaluate the value based on other column values. I'm not pressed for time on this project, so I've got time to play around and do different things.
-
Annoyingly enough, DataColumn.Expression doesn't support bitwise operators, so I decided to do it the long way. Also, annoyingly I can't modulo a double by an int, thus forcing me to convert to int. This leaves me with a column expression that looks like:
Convert((SomeColumn/4),'System.Int32')%2 = 1
Lo and behold my ire as I realize that the Convert function rounds the argument to its nearest whole value. Thus 3/4 = .75 Then: Convert(.75,Int32) = 1 and 1%2 = 1 This irritates me, especially since I'm trying to work through the DataSet Designer in VS 2k5 and subsequently trying to write as little code as possible. I've got a couple ideas for working around this - mostly involving writing in my own events or properties - but I'm curious to see if anyone has any other solutions.Perhaps you should explain what it is that you are trying to accomplish, rather than asking about why the workaround that you think might be the solution doesn't work as expected...
Despite everything, the person most likely to be fooling you next is yourself.
-
Perhaps you should explain what it is that you are trying to accomplish, rather than asking about why the workaround that you think might be the solution doesn't work as expected...
Despite everything, the person most likely to be fooling you next is yourself.
Your words of encouragement enlighten me... To summize what I've already said: Bitwise operation on the DataColumn Expression property to be used with boolean properties on databound controls. At any rate, I've determined that it ain't gonna happen through the dataset designer. Any sentiments of wanting to assist are much appreciated.
-
Annoyingly enough, DataColumn.Expression doesn't support bitwise operators, so I decided to do it the long way. Also, annoyingly I can't modulo a double by an int, thus forcing me to convert to int. This leaves me with a column expression that looks like:
Convert((SomeColumn/4),'System.Int32')%2 = 1
Lo and behold my ire as I realize that the Convert function rounds the argument to its nearest whole value. Thus 3/4 = .75 Then: Convert(.75,Int32) = 1 and 1%2 = 1 This irritates me, especially since I'm trying to work through the DataSet Designer in VS 2k5 and subsequently trying to write as little code as possible. I've got a couple ideas for working around this - mostly involving writing in my own events or properties - but I'm curious to see if anyone has any other solutions.Hi, Not sure why SomeColumn would be a double to start with. assuming your SomeColumn actually holds an integer value, FIRST convert it to int, then start processing its bits. Probably a simple
if (intValueOfSomeColumn & 4 != 0) ...
does exactly what you want... :)Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
-
Hi, Not sure why SomeColumn would be a double to start with. assuming your SomeColumn actually holds an integer value, FIRST convert it to int, then start processing its bits. Probably a simple
if (intValueOfSomeColumn & 4 != 0) ...
does exactly what you want... :)Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
I wish it were so simple. Datacolumn.Expression, any expression in an activex data object actually, doesn't support bitwise operators. So, that's no big deal x/y%2 should work. Right? Nope. ADO.net objects return a double to the engine before computing the modulus. So int/int%2 turns into double%2, which isn't supported. Convert won't work because it performs a Round instead of a cast. In the end, what I wound up settling on was computing the boolean values as bit fields when I retrieve the data from the database.
-
I wish it were so simple. Datacolumn.Expression, any expression in an activex data object actually, doesn't support bitwise operators. So, that's no big deal x/y%2 should work. Right? Nope. ADO.net objects return a double to the engine before computing the modulus. So int/int%2 turns into double%2, which isn't supported. Convert won't work because it performs a Round instead of a cast. In the end, what I wound up settling on was computing the boolean values as bit fields when I retrieve the data from the database.
Hi, Seems to me one of us is not understanding what the other meant. Either you start with a TYPE double that holds an integer VALUE, then my advice is to convert it to a TYPE int; that will succeed without problems (unless you exceed the int's range). Or your TYPE double holds a non-integer VALUE, and then you will not be able to explain to me what the meaning of most of its bits is; your question does not make sense in this case. Did you at all try what I suggested? :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.