处理数学和计算的扩展方法有哪些有用的例子?

发布于 2024-09-28 08:07:28 字数 220 浏览 2 评论 0原文

我经常使用的扩展方法是

public static double Pi(double this x) { return Math.PI*x; }

为了访问 2.0.Pi()0.5.Pi() .. 等等

数学相关扩展的其他示例是什么人们经常使用的方法?

附言。只是好奇。

My very often used extension method is

public static double Pi(double this x) { return Math.PI*x; }

in order to have access to 2.0.Pi() or 0.5.Pi() .. etc

What are some other examples of mathematics related extension methods the people use often?

PS. Just curious.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫罗兰の梦幻 2024-10-05 08:07:28
public static double Squared(double this x) { return x*x; }
public static double Squared(double this x) { return x*x; }
半透明的墙 2024-10-05 08:07:28

为了让创意源源不断,我将提供更多例子......

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。不幸的是,他们几乎没有与数学相关的帖子。

In order to get the creative juices flowing I will offer some more examples...

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. Thanks @Aaron for posting extensionmethods.net. Unfortunately they have little in terms of math related postings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文