如何使用通用数学生成常数值

huangapple go评论61阅读模式
英文:

How to generate constant values with generic math

问题

以下是翻译的代码部分:

我有以下通用数学函数:

private static T Fade<T>(T t)
    where T : IFloatingPoint<T>
{
    return t * t * t * (t * (t * 6 - 15) + 10);
}

然而,这不会编译,因为 `6`、`15`  `10` 不是类型 `T` 的。

我能想到的最佳解决方案是定义一个静态类,如下所示:

private static class GenericValues<T>
    where T : IFloatingPoint<T>
{
    public static readonly T Two = T.One + T.One;
    public static readonly T Three = Two + T.One;
    public static readonly T Four = Three + T.One;
    public static readonly T Five = Four + T.One;

    public static readonly T Six = Five + T.One;
    public static readonly T Ten = Five * Two;
    public static readonly T Fifteen = Five * Three;
}

然后该函数变成这样:

private static T Fade<T>(T t)
    where T : IFloatingPoint<T>
{
    return t * t * t * (t * (t * GenericValues<T>.Six - GenericValues<T>.Fifteen) + GenericValues<T>.Ten);
}

这种方法感觉有点像一个小技巧,是否有更好的方法?
英文:

I have the following generic math function:

private static T Fade&lt;T&gt;(T t)
    where T : IFloatingPoint&lt;T&gt;
{
    return t * t * t * (t * (t * 6 - 15) + 10);
}

This doesn't compile however, since 6, 15 and 10 are not of type T.

The best solution I could come up with was to define a static class like this:

private static class GenericValues&lt;T&gt;
    where T : IFloatingPoint&lt;T&gt;
{
    public static readonly T Two = T.One + T.One;
    public static readonly T Three = Two + T.One;
    public static readonly T Four = Three + T.One;
    public static readonly T Five = Four + T.One;

    public static readonly T Six = Five + T.One;
    public static readonly T Ten = Five * Two;
    public static readonly T Fifteen = Five * Three;
}

And then the function becomes this:

private static T Fade&lt;T&gt;(T t)
    where T : IFloatingPoint&lt;T&gt;
{
    return t * t * t * (t * (t * GenericValues&lt;T&gt;.Six - GenericValues&lt;T&gt;.Fifteen) + GenericValues&lt;T&gt;.Ten);
}

This feels a bit like a hack though, is there a nicer way to do this?

答案1

得分: 9

You can use one of the INumberBase&lt;T&gt;.CreateX methods, for example INumberBase&lt;TSelf&gt;.CreateChecked&lt;TOther&gt;(TOther) to convert the number literals to the generic number type:

private static T Fade&lt;T&gt;(T t)
    where T : IFloatingPoint&lt;T&gt;
{
    return t * t * t * 
        (t * (t * T.CreateChecked(6) - T.CreateChecked(15)) + T.CreateChecked(10));
}
英文:

You can use one of the INumberBase&lt;T&gt;.CreateX methods, for example INumberBase&lt;TSelf&gt;.CreateChecked&lt;TOther&gt;(TOther) to convert the number literals to the generic number type:

private static T Fade&lt;T&gt;(T t)
    where T : IFloatingPoint&lt;T&gt;
{
    return t * t * t * 
        (t * (t * T.CreateChecked(6) - T.CreateChecked(15)) + T.CreateChecked(10));
}

huangapple
  • 本文由 发表于 2023年2月7日 02:35:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定