Using multiplication and division signs

Using multiplication and division signs

BenoitE&A
Collaborator Collaborator
562 Views
5 Replies
Message 1 of 6

Using multiplication and division signs

BenoitE&A
Collaborator
Collaborator

Hi,

I just came through a weird result : 

I tested the line :

myMessage += "\n Essai priorisation signes " + (1 / 2 * 4) + " essai 2 " + (4 / 2);

Normally this should return 2 for each of the results, right ?

It doesn't. It returns 0 and 2, and the same when you use it in pieces of code.

Could anybody explain me why ?

Benoit


Benoit FAVRE
CEO of etudes & automates
www.etudesetautomates.com/
0 Likes
Accepted solutions (1)
563 Views
5 Replies
Replies (5)
Message 2 of 6

jeremytammik
Autodesk
Autodesk

Dear Benoit,

 

Thank you for your query.

 

What is the context, please?

 

Is this in C# code?

 

If so, am I correct in assuming it has nothing specific whatsoever to do with the Revit API?

 

Apparently, the numbers you entered are being handled as integer terms and then converted to strings.

 

I would have expected an error message on compilation.

 

The result (4 / 2) --> 2 should not come as a huge surprise.

 

The first term can be interpreted as (1 / 2) * 4. Using integer arithmetic, the result is obviously 0 * 4 = 0.

 

It can also be interpreted as 1 / (2 * 4). Using integer arithmetic, the result is again obviously 1/8 --> 0.

 

So, I wonder why you are asking.

 

Best regards,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 3 of 6

BenoitE&A
Collaborator
Collaborator

Hi Jeremy,

Yes I use C#. To get you understand the whole idea here is my piece of code :

monMur is a wall and contour is a Curve in the main Plane (in fact it is a portion of one of the faces of the wall monMur)

 

// Find the offset vector at the middle of the curve
                        XYZ milieu = contour.Evaluate(0.5, true);
                        IntersectionResult ir = locMur.Project(milieu);
                        XYZ vectOffset = ir.XYZPoint - milieu;
                        // Add 1/2 width of current wall to vectOffset
                        XYZ vectOffset = vectOffset * (vectOffset.GetLength() + monMur.WallType.Width/2) / vectOffset.GetLength(); 

 

But this code gives a very different result as the following if I change the last line :

                        XYZ vectOffset = vectOffset * (vectOffset.GetLength() + 1/2*monMur.WallType.Width) / vectOffset.GetLength();

Because in this line the term 1/2*monMur.WallType.Width is evaluated as 0 by Revit.

So I Wonder where this comes from...

Benoit

 

 


Benoit FAVRE
CEO of etudes & automates
www.etudesetautomates.com/
0 Likes
Message 4 of 6

aignatovich
Advisor
Advisor
Accepted solution

Hi!

 

Do not blame Revit, it is innocent :). Read about how number constants are presented in .Net.

 

Hint: the result is absolutely different from 1 / 2 and 1.0 / 2 or 1 / 2.0

0 Likes
Message 5 of 6

BenoitE&A
Collaborator
Collaborator

Thanks Aignatovitch.

Indeed it can only comes from the way C# deals with numbers. I should get the habbit of coding 1.0 instead of 1 when using numbers.

Still it is... annoying...

Thanks anyway and sorry for the stupid question.

Benoit


Benoit FAVRE
CEO of etudes & automates
www.etudesetautomates.com/
0 Likes
Message 6 of 6

aignatovich
Advisor
Advisor

Don't worry.

 

This behavior is described in standards and it is practically the same in all C-styled languages. It is also good to avoid use of "magic constants" in the code, the better way is to define named constants (const double halfWidth = 0.5;) or just use 0.5*wall.Width*XYZ... in such simple case (but only if the case is really simple)

0 Likes