草庐IT

double-elimination

全部标签

C++:将 double 转换为字符串的最佳方法是什么?

实现这一目标的最最佳方法是什么?voidfoo(doublefloatValue,char*stringResult){sprintf(stringResult,"%f",floatValue);} 最佳答案 我相信有人会说boost::lexical_cast,所以如果你使用boost,那就去吧,但无论如何它基本上是一样的:#include#includestd::stringdoubleToString(doubled){std::ostringstreamss;ss请注意,您可以轻松地将其制作成一个模板,该模板适用于任何可以流

c++ - 用 const : Different treatment for int and double 初始化 constexpr

这个问题在这里已经有了答案:Constantexpressioninitializerforstaticclassmemberoftypedouble(2个回答)关闭2年前。以下代码编译失败liveonIdeone:#includeusingnamespacestd;intmain(){constdoublekPi=3.14;constexprdoublekPi2=2.0*kPi;cout错误信息是:prog.cpp:Infunction'intmain()':prog.cpp:6:30:error:thevalueof'kPi'isnotusableinaconstantexpres

c++ - 如何在避免未定义行为的同时将任意 double 转换为整数?

假设我有一个接受64位整数的函数,我想调用它与double具有任意数值(即它可能非常大量级,甚至无限):voidDoSomething(int64_tx);doubled=[...];DoSomething(d);C++11标准中[conv.fpint]的第1段是这样说的:Aprvalueofafloatingpointtypecanbeconvertedtoaprvalueofanintegertype.Theconversiontrun-cates;thatis,thefractionalpartisdiscarded.Thebehaviorisundefinedifthetrun

c++ - 为什么 Clang std::ostream 会写入 std::istream 无法读取的 double 值?

我正在使用一个应用程序,它使用std::stringstream从文本文件中读取空格分隔的double矩阵。该应用程序使用的代码有点像:std::ifstreamfile{"data.dat"};constautoheader=read_header(file);constautonum_columns=header.size();std::stringline;while(std::getline(file,line)){std::istringstreamss{line};doubleval;std::size_ttokens{0};while(ss>>val){//dostuff

c++ - 使用 boost::program_options 接受负 double

我需要能够拥有boost::program_options解析一个double组在命令行上传递。对于正double,这没问题,当然(在add_options中使用带有std::vector的多token),但是对于否定的,我知道这些都是模棱两可的论点。以下是我想要学习的演示:mycommand--extent-1.0-2.0-3.01.02.03.0--some-other-argumentsomevalueextentistobebackedbyaBoundsclasswithatleastoneconstructorthattakesinsixindividualTargument

c++ cout << [double] 不打印小数位

我有一个简单的C++程序,我认为它会将f和g中定义的double值打印为double值……但C++将它们打印为整数。我检查了cout的默认精度,当我运行它时,输出显示默认精度应该是6,所以我很困惑为什么double不能正确打印。#include#includeintmain(){doublef=735416.416898;doubleg=f+0.3;std::cout我看到的输出是:kiran@kiran-VirtualBox:~/test$g++-otesttest.cppkiran@kiran-VirtualBox:~/test$./test[c++]f=735416[c++]g=

c++ - 在clang++与g++中除以复数<double>

当我使用g++(4.8.1或4.9.0)或clang++(3.4)编译以下代码时,我得到不同的输出。#include#includeintmain(){std::complexc={1.e-162,0};std::coutg++:(1e+162,0)叮当++:(inf,-nan)这是clang的错误吗?更新:感谢您的回答!我报告了这个错误:http://llvm.org/bugs/show_bug.cgi?id=19820 最佳答案 标准在[complex.numbers]中说(26.4/3):Iftheresultofafunct

c++ - 如果您之前将其初始化为零,那么将 double 值与零进行比较是否正确?

我了解到,使用==比较double并不是一个明智的做法。但是我想知道检查是否已初始化double可能是危险的。例如,如果知道变量doubleVar已经初始化后不能为零,那么这样做是否安全?Foo::Foo(){doubleVar=0.0;//oftypedouble}voidFoo::Bar(){if(doubleVar==0){//hasbeeninitialized?//...}else{//...}} 最佳答案 在IEEE-754中,长话短说:doubled;d=0.0;d==0.0//guaranteedtoevaluate

C++ 为什么我可以在类定义中初始化静态 const char 但不能初始化静态 const double?

这里有两行代码:staticconstdoubleRYDBERG_CONST_EV=13.6056953;staticconstcharCHAR_H_EDGE='-';第二行编译没有错误,第一行没有编译。(错误:静态数据成员的类内初始化需要'constexpr'...)解决方案显然是在类型之前添加关键字constexpr。这是必需的,因为double不是“整数类型”。但是为什么整数和浮点类型的行为不同呢? 最佳答案 我不认为这是有充分理由的,除非它在历史上有所发展。整数类型的异常(exception)在C++11之前是可取的,因为人

c++ - 接受给定数量的 double 的可变参数模板方法?

templateclassmyclass{public:templatevoidmymethod(Args...args){//Dointerestingstuff}};我希望mymethod仅使用恰好N个double来调用。那可能吗?也就是说,假设我有:myclassx;x.mymethod(3.,4.,5.);//Thisworksx.mymethod('q',1.,7.);//Thisdoesn'tworkx.mymethod(1.,2.);//Thisdoesn'twork我怎样才能完成这项工作? 最佳答案 对于参数数量限制