如何强制执行表达式的评估类型为双重

发布于 2025-02-07 08:22:14 字数 672 浏览 1 评论 0原文

我如何确保表达式始终将其评估为双重评估,即使它没有双重,我都会遇到此网站 https://lealen.microsoft.com/en-en-us/dotnet/csharp/csharp/language-reganp/language-reference-reference-reference-reference/builtin-reference/builtin-types/浮动点数 - 型,它说:

“从这里检查相关部分” 因此,我该如何覆盖这个问题并确保表达式始终被评估为双重,

我试图使用逃离图书馆,并且每当我将其放在其中(4/3)中时,它将其作为一个整数,甚至不是浮子,这都破坏了整个计算,因此我必须解决这个问题,

如果我尝试使用以下表达式

(4.0/3)或(4/3.0)或(4/3.0)= 1.333333 故障案件如下 (4/3)= 1 希望我的解释很清楚

how could I make sure that the expression is always evaluated as a double even if it has no double in it, I came across this website https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types, It says :

check the relevant part from here
so how can I override this problem and make sure that the expression is always evaluated as a double,

I am trying to use the library of Flee, and whenever I put a fraction in it like(4/3) it deals with it as an integer, not even a float, this ruins the whole calculation so I have to get around this problem,

If I try and use the following expression it works

(4.0/3) or (4/3.0) = 1.33333
the faulty case is as follows
(4/3) = 1
hopefully my explanation is clear

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

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

发布评论

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

评论(3

浅黛梨妆こ 2025-02-14 08:22:14

如文档中所述,这是该语言的特征。您始终可以将分子或分母施放成浮子或双double,该浮点数将以相同类型的结果结尾:

int a = 4;
int b = 3;
double result = (double)a / b;

As mentioned in the docs, that is a feature of the language. You can always cast either the numerator or the denominator into a float or double which will end with a result of the same type:

int a = 4;
int b = 3;
double result = (double)a / b;
送君千里 2025-02-14 08:22:14

我设法通过强迫逃离图书馆不再将任何给定的输入视为整数来解决问题,我通过编写以下代码来做到这一点:

ExpressionContext context = new ExpressionContext();
context.Options.IntegersAsDoubles = true;

祝大家好运

I managed to solve the problem by forcing Flee Library to not consider any given input as Integer anymore, I did that by writing the following code:

ExpressionContext context = new ExpressionContext();
context.Options.IntegersAsDoubles = true;

Best of luck to you all

江挽川 2025-02-14 08:22:14

解决方案1

使用双重注释,该注释告诉编译器该数字实际上是双重的

4d / 3

(D double,也适用于Float使用F)

解决方案2

使用明确的铸造

(double)4 / 3

Solution 1

Use the double annotation which tells to the compiler that the number is actually a double

4d / 3

(d for double, also works for float using f)

Solution 2

Use explicit casting

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