vb.net中的绝对值

发布于 2024-08-03 03:52:10 字数 128 浏览 11 评论 0原文

在vb.net中如何求一个数的绝对值?

有内置的功能吗?我知道我可以自己简单地编写一个函数,但我想先知道是否已经有一个函数。它看起来很简单,我可能可以用三行完成,所以如果没有的话我会感到惊讶......

谢谢!

How do you get the absolute value of a number in vb.net?

Is there a function built in? I know I can simply code a function myself, but I want to know if there is one already there first. It seems so simple, I could probably make it in three lines, so I would be surprised if there isnt one....

Thanks!

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

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

发布评论

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

评论(4

倒数 2024-08-10 03:52:10

Math.Abs​​( val )

MSDN 链接

Math.Abs( val )

MSDN Link

拍不死你 2024-08-10 03:52:10

冒着被否决的风险,您可能需要编写自己的绝对值方法,具体取决于您的用途。以下代码片段(抱歉,它是用 C# 编写的,但适用相同的原则):

short i = -32768;
int iAbs = Math.Abs(i);

会愉快地编译,但运行时,第二行将抛出 OverflowException 并显示有用的消息“否定二进制补码数的最小值无效”。在这种情况下,因为 i 是短整型,所以编译器选择接受短整型并返回短整型的 Math.Abs​​ 重载,而 +32768 不是有效的短整型,因此即使您认为自己在预料到,该方法也会引发异常通过将 iAbs 设置为 int 来解决这个问题。

此代码片段:

short i = -32768;
int iAbs = Math.Abs((int)i);

将编译并执行而不会出现异常,但以这种方式编码有点笨拙。在我看来,这是一个非常狡猾的错误,因为它在现实世界中很少遇到(因为每种类型只有一个值会生成此异常)。不幸的是,每当我使用 Math.Abs​​ 标准化音频数据(通常是一个短[]数组)时,我都会遇到这个错误,所以我养成了围绕 Math.Abs​​ 编写自己的包装器来处理所有这对我来说只是返回一个 double:

public double AbsThatDoesntSuck(short value)
{
    return Math.Abs((double)value);
}

以及我需要处理的任何其他类型的重载。我有点理解为什么 Math.Abs​​ 被编写为这样的行为,但它肯定会咬伤不知情的人的屁股。

At the risk of being down-voted, you may want to write your own absolute value method, depending on what you're using it for. The following code snippet (sorry it's in C#, but the same principle applies):

short i = -32768;
int iAbs = Math.Abs(i);

will happily compile, but when run, the second line will throw an OverflowException with the helpful message "Negating the minimum value of a twos complement number is invalid." In this case, because i is type short, the compiler chooses the overload of Math.Abs that accepts a short and returns a short, and +32768 is not a valid short, so the method throws the exception even if you thought you were anticipating this problem by making iAbs an int.

This snippet:

short i = -32768;
int iAbs = Math.Abs((int)i);

will compile and execute without an exception, but it's kind of clunky to code this way. In my opinion, this is a very sneaky error because it's so rarely encountered in the real world (since there's only one value for each type that will generate this exception). I, unfortunately, run into this error whenever I use Math.Abs for normalizing audio data (which is usually a short[] array), so I've gotten in the habit of writing my own wrapper around Math.Abs that handles all of this for me and just returns a double:

public double AbsThatDoesntSuck(short value)
{
    return Math.Abs((double)value);
}

with overloads for whatever other type I need to handle. I kind of understand why Math.Abs was written to behave this way, but it can definitely bite the behinds of the unaware.

森罗 2024-08-10 03:52:10

该函数是Math.Abs

The function is Math.Abs

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