Can links to Math be shortened in C #?

In VB.NET, I could make my math code much cleaner by importing System.Math and directly referencing its methods:

Imports System.Math [...] Return New Vector3(Sin(az) * Cos(el), Cos(az) * Cos(el), Sin(el)) 

But I don't think C # can use classes to access their methods implicitly, so I need to do something like:

 using System; [...] return new Vector3(Math.Sin(az) * Math.Cos(el), Math.Cos(az) * Math.Cos(el), Math.Sin(el)); 

But it is ugly; he needs his own scrollbar !. Is there a way to write something in C # that looks like my VB.NET code? Could I write local wrapper methods for Sin and Cos, but wouldn't slow down (due to the overhead of function calls)? And for this, you will need to write wrapper functions for each mathematical function that I use in every class in which I use them; which is also not very desirable.

+7
source share
7 answers

You can specify a System.Math alias using the use directive:

 using M = System.Math; return new Vector3(M.Sin(az) * M.Cos(el), M.Cos(az) * M.Cos(el), M.Sin(el)); 

This is the best I got.

I can write local wrapper methods for Sin and Cos, but won't it slow down (due to the overhead of function calls)?

You could use generics and other subtleties at that moment to make it even more convenient, but you will still reference a library like this, unless all the math happens in the same class where this code happens as methods.

The question "performance from overhead" answers "additional stack frames for something that is simple, not noticeable for standard applications." If you were in a closed loop, then yes, that would be a problem.

+12
source

With C # 6.0, you can shorten math links by adding a declaration using static :

 using static System.Math; 

This allows you to use static members of type Math without specifying a type name:

 public void Foo() { var bar = Sin(8); } 

It’s not possible to do this “globally,” since it’s currently not possible at all to use global usage of an ad.

For those who do not use C # 6

but will this not reduce performance (due to the overhead of function calls)?

I would not worry about that. First write your code to read. We can write a method in several ways. First, just add a space:

 return new Vector3( Math.Sin(az) * Math.Cos(el), Math.Cos(az) * Math.Cos(el), Math.Sin(el) ); 

You can also put this in a helper method.

jcolebrand answer is also nice to do this, but you need to add using m = System.Math; everywhere.

+9
source

You can add

 using M = System.Math; 

and then just

 return new Vector3(M.Sin(az) * M.Cos(el), M.Cos(az) * M.Cos(el), M.Sin(el)); 

but .... I do not see the actual value

In fact, I think each of these variables should be stored with better names to represent what they are. Looking at this, I do not see what is being done.

 var sinAz = Math.Sin(az); //Could these be named better? Perhaps with words in the correct domain var cosAz = Math.Cos(az); var sinEl = Math.Sin(el); var cosEl = Math.Cos(el); return new Vector3(sinAz * cosEl, cosAz * cosEl, sinEl); 

Or better, what are the three parameters that are actually used by the Vector3 vector?

 var parm1 = Math.Sin(Az) * Math.Cos(El); //What should parm1, parm2, parm3 be called? var parm2 = Math.Cos(Az) * Math.Cos(El); var parm3 = Math.Sin(Az); return new Vector3(parm1, parm2, parm3); 

This will FULLY improve readability, as the next person will better understand what the formula is.

+7
source

But it is ugly; he needs his own scrollbar!

Yes, when overflowing a stack that has a short width. You should put it on several lines instead:

 return new Vector3(Math.Sin(az) * Math.Cos(el), Math.Cos(az) * Math.Cos(el), Math.Sin(el)); 

Personally, I think this is more readable, because now three different meanings are more clearly distinguished. An empty line is a clearer separator than just a comma, IMO. When you have several arguments that are quite similar to each other, they can get lost together, for clarity, use a few lines.

Yes, in a sense, it would be nice if you could just write Sin(az) , etc., but there is nothing in C # that allows you to do this, so I would reformat the code instead for readability .

+7
source

You can create a class that wraps calls as extension methods:

 public static class MathExtensions { public static double Sin(this double value) { return Math.Sin(value); } public static double Cos(this double value) { return Math.Cos(value); } } return new Vector3(az.Sin() * el.Cos(), az.Cos() * el.Cos(), el.Sin()); 

The compiler must embed these calls so that the performance does not differ much.

+5
source

You can also use extension methods, but the result will look more unnatural (with a potential additional function call, it can also be detuned):

 static double Sin(this double value) { return Math.Sin(value);} 

And use:

 return new Vector3(az.Sin() * el.Cos(),.... 
+1
source

You cannot define an alias for a method in C #, just for a class.

0
source

All Articles