在小数情况下,科学计数法表现与整数情况下不同
#include<iostream>
using namespace std;
int main()
{
float float_1 = 3e5;
cout << "float_1的值为" << float_1 << endl;
float float_2 = 3e-5;
cout << "float_2的值为" << float_2 << endl;
system("pause");
return 0;
}
运行结果如下
float_1的值为300000
float_2的值为3e-05
预期结果如下
float_1的值为300000
float_2的值为0.00003
请问,C++默认表示有效数字不是6位吗?为何会出现0.00003被表示成3e-05?
因为3e-5其实并不是6位有效数字,因为10进制浮点数转为2进制时不能准确转换
乍看起来c++的cin、cout、ifstream、ofstream、istringstream、ostringstream在输入、输出上比c的scanf、printf、fscanf、fprintf、fread、fwrite、sscanf、sprintf简单,不用格式控制符!
但是不用格式控制符,输入输出恰好是你期望的格式的时候好说;等到输入输出不是你期望的格式的时候,你就会觉得还是用格式控制符更方便、更靠谱。
摒弃cin、cout、ifstream、ofstream、istringstream、ostringstream!
使用scanf、printf、fscanf、fprintf、fread、fwrite、sscanf、sprintf。
File: "C:\Program Files (x86)\Windows Kits\10\Include\10.0.20348.0\ucrt\float.h"
71: #define DBL_DECIMAL_DIG 17 // # of decimal digits of rounding precision
72: #define DBL_DIG 15 // # of decimal digits of precision
73: #define DBL_EPSILON 2.2204460492503131e-016 // smallest such that 1.0+DBL_EPSILON != 1.0
74: #define DBL_HAS_SUBNORM 1 // type does support subnormal numbers
75: #define DBL_MANT_DIG 53 // # of bits in mantissa
76: #define DBL_MAX 1.7976931348623158e+308 // max value
77: #define DBL_MAX_10_EXP 308 // max decimal exponent
78: #define DBL_MAX_EXP 1024 // max binary exponent
79: #define DBL_MIN 2.2250738585072014e-308 // min positive value
80: #define DBL_MIN_10_EXP (-307) // min decimal exponent
81: #define DBL_MIN_EXP (-1021) // min binary exponent
82: #define _DBL_RADIX 2 // exponent radix
83: #define DBL_TRUE_MIN 4.9406564584124654e-324 // min positive value
84:
85: #define FLT_DECIMAL_DIG 9 // # of decimal digits of rounding precision
86: #define FLT_DIG 6 // # of decimal digits of precision
87: #define FLT_EPSILON 1.192092896e-07F // smallest such that 1.0+FLT_EPSILON != 1.0
88: #define FLT_HAS_SUBNORM 1 // type does support subnormal numbers
89: #define FLT_GUARD 0
90: #define FLT_MANT_DIG 24 // # of bits in mantissa
91: #define FLT_MAX 3.402823466e+38F // max value
92: #define FLT_MAX_10_EXP 38 // max decimal exponent
93: #define FLT_MAX_EXP 128 // max binary exponent
94: #define FLT_MIN 1.175494351e-38F // min normalized positive value
95: #define FLT_MIN_10_EXP (-37) // min decimal exponent
96: #define FLT_MIN_EXP (-125) // min binary exponent
97: #define FLT_NORMALIZE 0
98: #define FLT_RADIX 2 // exponent radix
99: #define FLT_TRUE_MIN 1.401298464e-45F // min positive value