我经常使用的扩展方法是
public static double Pi(double this x) { return Math.PI*x; }
为了能够访问2.0.Pi()
或0.5.Pi()
..等
人们经常使用的与数学相关的可拓方法还有哪些其他的例子?
PS。只是好奇而已。
发布于 2010-10-20 22:50:39
为了让创意源源不断,我将提供更多的例子……
public static double Raise(double this x, double exponent)
{
return Math.Pow(x, exponent);
}
public static bool NearZero(double this x, double tolerance)
{
return Math.Abs(x) <= tolerance;
}
public static double Cap(double this x, double threshold)
{
return Math.Abs(x)<threshold ? x : threshold*Math.Sign(x);
}
public static double SignOf(double this x, double sense)
{
return Math.Sign(sense)*Math.Abs(x);
}
public static double ToRadians(double this x) { return Math.PI*x/180; }
public static double ToDegrees(double this x) { return 180*x/Math.PI; }
PS。感谢@Aaron发布extensionmethods.net。不幸的是,他们几乎没有数学相关的帖子。
发布于 2010-10-20 03:38:11
public static double Squared(double this x) { return x*x; }
https://stackoverflow.com/questions/3971357
复制相似问题