实施类型“长双”与GCC和C ++ 11 [英] Implementation of type "long double" with GCC and C++11

查看:171
本文介绍了实施类型“长双”与GCC和C ++ 11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在Ubuntu(XUbuntu)Linux上使用GCC时,我已经尝试搜索关于long double的信息。到目前为止,我了解它是由编译器实现的。 12.10我得到这个:

  double PId = acos(-1) 
long double PIl = acos(-1);
std :: cout.precision(100);

std :: cout<< PId< sizeof(double)<< :<< PId < std :: endl;
std :: cout<< PI1 sizeof(long double)<< :<< PI1 < std :: endl;

输出:

 code> PId 8:3.141592653589793115997963468544185161590576171875 
PIl 16:3.141592653589793115997963468544185161590576171875

要获得正确的有效数字个数,请使用 std :: numeric_limits 。在C ++ 11中,我们有 digits10 (十进制有效数字与
  

#include< cmath>
#include< iostream>
#include< limits>

int
main()
{
std :: cout.precision(std :: numeric_limits< float> :: digits10);
double PIf = acos(-1.0F);
std :: cout<< PIf< sizeof(float)<< :<< PIf < std :: endl;

std :: cout.precision(std :: numeric_limits< double> :: digits10);
double PId = acos(-1.0);
std :: cout<< PId< sizeof(double)<< :<< PId < std :: endl;

std :: cout.precision(std :: numeric_limits< long double> :: digits10);
long double PIl = std :: acos(-1.0L);
std :: cout<< PI1 sizeof(long double)<< :<< PI1 < std :: endl;
}

在x86_64 linux我得到:

  PIf 4:3.14159 
PId 8:3.14159265358979
PIl 16:3.14159265358979324


I've tried searching for information on long double, and so far I understand it is implemented differently by compilers.

When using GCC on Ubuntu (XUbuntu) Linux 12.10 I get this:

double PId = acos(-1);
long double PIl = acos(-1);
std::cout.precision(100);

std::cout << "PId " << sizeof(double) << " : " << PId << std::endl;
std::cout << "PIl " << sizeof(long double)  << " : " << PIl << std::endl;

Output:

PId 8  : 3.141592653589793115997963468544185161590576171875
PIl 16 : 3.141592653589793115997963468544185161590576171875

Anyone understand why they output (almost) the same thing?

解决方案

To get the correct number of significant digits use std::numeric_limits. In C++11 we have digits10 for decimal significant digits (as opposed to digits which gives significant bits).

#include <cmath>
#include <iostream>
#include <limits>

int
main()
{
  std::cout.precision(std::numeric_limits<float>::digits10);
  double PIf = acos(-1.0F);
  std::cout << "PIf " << sizeof(float) << " :  " << PIf << std::endl;

  std::cout.precision(std::numeric_limits<double>::digits10);
  double PId = acos(-1.0);
  std::cout << "PId " << sizeof(double) << " :  " << PId << std::endl;

  std::cout.precision(std::numeric_limits<long double>::digits10);
  long double PIl = std::acos(-1.0L);
  std::cout << "PIl " << sizeof(long double)  << " : " << PIl << std::endl;
}

On x86_64 linux I get:

PIf 4 :  3.14159
PId 8 :  3.14159265358979
PIl 16 : 3.14159265358979324

这篇关于实施类型“长双”与GCC和C ++ 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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