gpt4 book ai didi

floating-point - unityscript中的 float 除法

转载 作者:行者123 更新时间:2023-12-02 00:10:36 25 4
gpt4 key购买 nike

为什么这段代码输出的是 536870912 而不是 536870911.5?

var z : double = 1073741823.0 / 2.0;
Debug.Log(z.ToString("F15"));

我怎样才能让它输出 536870911.5?这对我来说似乎很奇怪......

最佳答案

您可以使用 C# 通过以下方式获取它:

double test = 1073741823.0d / 2.0d; // = 536870911.5
Debug.Log(test);

而在 UnityScript 中,您只需添加 d 即可

var test : double = 1073741823.0d / 2.0d; // = 536870911.5
Debug.Log(test);

如果没有双重表示法,UnityScript 会将数字解析为其他类型。 (很可能是一个整数)

以下内容不起作用,因为解释器没有正确转换它们:

var test : double = 1073741823.0 / 2.0; // = 536870912
Debug.Log(test);

在我看来它正在阅读:double = int/int;


非常有趣。这是我鼓励开发人员转向 C# 的另一个原因。

关于floating-point - unityscript中的 float 除法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15649676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com