从“double”到“float”截断是什么意思

img

#include<iostream>
using namespace std;
int main()
{
    const float PI = 3.14;
    float r, s, v;
;
    cout << "输入球体的半径:r=" << endl;
    cin >> r;
    s = 4*PI * r * r;
    v = 4.0/3*PI * r * r * r;
    cout << "球的体积是:" << v << endl << "球的表面积是:" << s << endl;

    return 0;
}

已启动重新生成…
1>已启动全部重新生成: 项目: hello world, 配置: Debug x64
1>helloworld.cpp
1>D:\C++opencv_program\hello world\helloworld.cpp(5,23): warning C4305: “初始化”: 从“double”到“float”截断
1>D:\C++opencv_program\hello world\helloworld.cpp(11,23): warning C4244: “=”: 从“double”转换到“float”,可能丢失数据
1>hello world.vcxproj -> D:\C++opencv_program\hello world\x64\Debug\hello world.exe
1>已完成生成项目“hello world.vcxproj”的操作。
全部重新生成: 成功 1 个,失败 0 个,跳过 0 个

这个warning的意思是什么呢?

一个是8字节一个是4字节,8字节像4字节转化会丢弃一定数据
warning C4305: “初始化”: 从“double”到“float”截断_shenlan282的笔记-CSDN博客 float x;x=22.333;编译则会出现 warning C4305: “初始化”: 从“double”到“float”截断系统默认此浮点数是22.333是double型,对float型变量赋值,所以会出现警告。解决:1、就将其后面加上f,如2.3f,就告诉系统这是浮点数。      2、由于float是6位有效数字,double是15位,所以,如果有精度要求高的,就 https://blog.csdn.net/cgcoder/article/details/7367965

精度不一样,从double到float会丢失精度,float类型小数点后最多6位,double转float后,小数点第6位以后的会丢失