关于double转float

无意之间想到的问题,节选自C99标准:

6.3.1.5 Real floating types

1 When a float is promoted to double or long double, or a double is promoted
to long double, its value is unchanged.

2 When a double is demoted to float, a long double is demoted to double or
float, or a value being represented in greater precision and range than required by its
semantic type (see 6.3.1.8) is explicitly converted to its semantic type, if the value being
converted can be represented exactly in the new type, it is unchanged. If the value being
converted is in the range of values that can be represented but cannot be represented
exactly, the result is either the nearest higher or nearest lower representable value, chosen
in an implementation-defined manner. If the value being converted is outside the range of
values that can be represented, the behavior is undefined.

总体来说:
1.如果float存的下,那么存最接近的数
2.如果float存不下,那么就是未定义行为

再看看NOIP的某一题

#include<iostream>
using namespace std;
int main(){
  double x=pow(10,200);
  float y=x;
  cout<<y<<endl;
  return 0;
}

知道我想说什么=w=

posted @ 2022-06-10 21:20  计算机知识杂谈  阅读(525)  评论(0编辑  收藏  举报