"+=" operator, comments appreciated...

A

almurph

Hi,

Just wondwering is a difference between the "+=" operator between C
and C#?


I ask as I have reverse engineered some C code to C# and am receiving
some differing results. I have traced it down to a line involving a
"+=" operator. The C clode uses floats but I am using double, but this
should not make a difference right?

Just wondering if anyone has any comments or suggestions or user
experiences that thay would like to share.

Thank,
Al.
 
J

Jeroen Mostert

Just wondwering is a difference between the "+=" operator between C
and C#?


I ask as I have reverse engineered some C code to C# and am receiving
some differing results. I have traced it down to a line involving a
"+=" operator. The C clode uses floats but I am using double, but this
should not make a difference right?
Let's see. "float" is not the same as "double", so how on earth would you
reach the conclusion that using different types could make no difference?

Without seeing your code it's impossible to say anything conclusive. That
said, "float" and "double" are both floating-point types (in both C and C#),
but "double" has more precision (in C#; in C it *usually* has more
precision, but this is platform-dependent). Floating-point addition with
different precision *is* going to give different results.
 
P

Peter Morris

Just wondering if anyone has any comments or suggestions or user
experiences that thay would like to share.

My experience suggests that questions like "I get differing code when I use
+=, can anyone tell me why" generally get ripped apart for not providing any
useful information from which to even understand the question let alone
produce an answer :)
 
J

J.B. Moreno

Peter Morris said:
My experience suggests that questions like "I get differing code when I use
+=, can anyone tell me why" generally get ripped apart for not providing any
useful information from which to even understand the question let alone
produce an answer :)

And since the variable being used in this += line is a floating point
variable, the very first thing that should be suspected is that the
floating point value(s) aren't what you think they are.
 
T

Tim Roberts

I ask as I have reverse engineered some C code to C# and am receiving
some differing results. I have traced it down to a line involving a
"+=" operator. The C clode uses floats but I am using double, but this
should not make a difference right?

x = 0.000000001;
y = 4.0;
y += x;

That will definitely produce different results with singles and doubles.
 
G

Göran Andersson

Hi,

Just wondwering is a difference between the "+=" operator between C
and C#?


I ask as I have reverse engineered some C code to C# and am receiving
some differing results. I have traced it down to a line involving a
"+=" operator. The C clode uses floats but I am using double, but this
should not make a difference right?

Just wondering if anyone has any comments or suggestions or user
experiences that thay would like to share.

Thank,
Al.

In C++ the += operator has it's own meaning and can be overloaded, in C#
it's just defined to use the + operator.

In C# this code:

d += 1.0;

is exactly the same as:

d = d + 1.0;

As the += is a bit more predictable in C# than C++, it's not likely that
it is the += operation in itself that is the problem. Perhaps you have
some other expression that is handled differently. What else is there on
the line?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top