为什么我的浮动变量语句不断给我一个'不能隐式转换类型双浮动'错误? [英] why does my float variable statement keeps on giving me an 'Cannot implicitly convert type double to float' error?

查看:213
本文介绍了为什么我的浮动变量语句不断给我一个'不能隐式转换类型双浮动'错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,我总是收到一个错误,说'不能隐式转换类型双浮动'在我的代码。我粗体,并留下了一个评论,我的代码中,我正在得到的错误。我似乎无法弄清楚是什么问题。我把数据类型全部切换到double而不是float,并且程序运行平稳无误。有人可以告诉我我做错了什么或代码中缺少什么吗? (编辑:粗体似乎不工作在我的代码在这个网站上)。

For some reason I keep getting an error saying 'Cannot implicitly convert type double to float' All over my code. I bolded and left a comment exactly where in my code I'm getting the errors. I cant seem to figure out what the problem is. I switched the data types all to 'double' instead of 'float' and the program ran smoothly with no errors. Can some one please tell me what I am doing wrong or what is missing from the code? ( Bold does not seem to work on my code on this site).

推荐答案

问题是,当你执行乘法(或其他算术运算)你正在使用一个文字,如0.15。默认情况下,这个值将被编译器视为一个双精度值,所以当乘上一个浮点数时,会导致较大的双精度结果。为了解决这个问题,你需要将文字标记为.15f,以便将它们视为浮点文字。

The problem is that when you perform your multiplication (or other arithmetic operation) you are working with a literal such as 0.15. This value by default will be treated as a double by the compiler and so when multiplied against a float it results in the larger double result. To get around this you need to mark the literals as .15f so that they are treated as float literals.

这篇关于为什么我的浮动变量语句不断给我一个'不能隐式转换类型双浮动'错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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