问题标签 [miscutils]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
4 回答
1159 浏览

c# - 通用 Sqrt 实现

我正在使用 MiscUtils 库(感谢 Marc G. 和 Jon S.)并试图向Sqrt它添加一个通用函数。这个问题可以很容易地重现:

现在,我意识到为什么会发生这种情况,但我还没有想出解决办法。作为参考,这里是当前的Sqrt实现。我几乎没有构建表达式树的经验。

编辑:好的,所以我自己解决了这个问题,但为了让问题尽可能简单,我显然走得太远了,花了太多时间回答困惑的人试图提供帮助的问题。

所以,这就是问题的全部。

我两班;一个执行转换,另一个执行图像数据(像素)的统计分析。让我们关注后者,因为问题是一样的:

像素数组可以是任何数字类型。在实践中,它将是floatintushortbyte。现在,因为泛型不能做这样的事情:

如果不转换为正确的数组类型,我无法对像素值本身进行任何类型的统计分析。所以,我需要有 N 个子类ImageProcessor来支持 N 个像素类型。

嗯,这很糟糕。我很想拥有一个具有像素数据的通用ImageProcessor<T>类。T[]因此,我查看了允许这样做的 MiscUtils 库。

0 投票
1 回答
127 浏览

c# - Is this a bug in MiscUtils?

Is this a bug in MiscUtils or am I missing something?

Fails on the last line:

Update

As Petr pointed out there is some coercion going on in the CreateExpression method which is causing the problem. When using the compiler we don't see such issues because the parameters are lifted to the type with the highest precision, which also becomes the return type.

The second 'Assert' should have failed anyway, because if consistent with normal C# behaviour, I would expect it to lift the first parameter (b) to a decimal and perform the operation. Normally, if we wanted to store the result to a variable of a type with lower precision we would need to do an explicit cast. However, since the method we are calling has a return type that may be of lower precision and the caller has explicitly invoked the method that returns a lower precision result - it seems justifiable to automatically perform a potentially truncating cast as part of the operation. In other words the expected result from the second expression would be 1.

So, we can change CreateExpression to reflect that behaviour as follows:

The second assertion therefore needs to be rewritten:

Now both assertions succeed. As before, depending on the order of the parameters, different results will be returned, but now the second invocation produces a result that is logically correct.

0 投票
1 回答
258 浏览

c# - C# Misc Utils - 运算符通用数学精度问题

我正在使用 Jon Skeet 的 Misc Util 库和 Marc 的 Operator 通用数学类。如果我正常进行数学运算,我会发现我不会发现的精度问题。

例如,与常规浮点类型运算符相比,在计算向量叉积时,我发现类似 .0001 的错误。

这是可以预料的吗?

输出:预期:-60.6999817f 但原为:-60.6999893f

0 投票
2 回答
55 浏览

c# - 为什么每次调用方法时 Compile() 都不运行?

这是来自 MiscUtil 库的一段代码 (由 Jon Skeet 和 Marc Gravell 编写)

它在代码下面说:

这不是很贵吗?

好吧,编译运算符并非易事,但静态构造函数确保我们只为每个签名执行一次。

Func<T, T, T>每次Add<T>(T a, T b)调用方法时都没有编译,但 insted 只编译一次的原因是什么?