VB.NET中的部门 [英] Division in VB.NET

查看:115
本文介绍了VB.NET中的部门的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ \ 在VB.NET中进行划分有什么区别?

What's the difference between / and \ for division in VB.NET?

根据我使用的代码,我的代码给出了非常不同的答案。我以前见过,但我从来不知道两者之间的区别。

My code gives very different answers depending on which I use. I've seen both before, but I never knew the difference.

推荐答案

有两种除数方法。快速方法和缓慢方法。许多编译器试图欺骗您以快速的方式进行操作。 C#是其中之一,请尝试以下操作:

There are two ways to divide numbers. The fast way and the slow way. A lot of compilers try to trick you into doing it the fast way. C# is one of them, try this:

using System;

class Program {
    static void Main(string[] args) {
        Console.WriteLine(1 / 2);
        Console.ReadLine();
    }
}

输出:0

您对结果满意吗?当表达式的左侧和右侧为整数时,从技术上讲,这是正确的行为。这样可以快速进行整数除法。处理器上的IDIV指令,而不是(臭名昭著的)FDIV指令。也与所有花括号语言的工作方式完全一致。但是,在SO上肯定是 wtf发生问题的主要来源。要获得满意的结果,您必须执行以下操作:

Are you happy with that outcome? It is technically correct, documented behavior when the left side and the right side of the expression are integers. That does a fast integer division. The IDIV instruction on the processor, instead of the (infamous) FDIV instruction. Also entirely consistent with the way all curly brace languages work. But definitely a major source of "wtf happened" questions at SO. To get the happy outcome you would have to do something like this:

    Console.WriteLine(1.0 / 2);

输出:0.5

左侧现在为double,强制进行浮点除法。使用计算器显示的结果类型。调用FDIV的其他方法是,使右侧为浮点数,或将其中一个操作数显式转换为(double)。

The left side is now a double, forcing a floating point division. With the kind of result your calculator shows. Other ways to invoke FDIV is by making the right-side a floating point number or by explicitly casting one of the operands to (double).

VB.NET不会通过这种方式,/运算符始终都是浮点除法,而与类型无关。有时您真的要做想要整数除法。这就是 \ 所做的。

VB.NET doesn't work that way, the / operator is always a floating point division, irrespective of the types. Sometimes you really do want an integer division. That's what \ does.

这篇关于VB.NET中的部门的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆