如何防止双值被截断
How to prevent double values from getting truncated?
本文关键字:何防止 更新时间:2023-10-16
这是一个可复制的C++程序:
#include <RInside.h>
#include <Rcpp.h>
int main (int argc, char *argv[])
{
RInside R (argc, argv);
SEXP ans = R.parseEval ("m <- 40.702147");
Rcpp::NumericVector v (ans);
for (int i=0; i< v.size(); i++)
{
std::cout << "In C++ element " << i << " is " << v[i] << std::endl;
}
return 0;
}
Makefile在这里:
## -*- mode: make; tab-width: 8; -*-
##
## Simple Makefile
##
## TODO:
## proper configure for non-Debian file locations, [ Done ]
## allow RHOME to be set for non-default R etc
## comment this out if you need a different version of R,
## and set set R_HOME accordingly as an environment variable
R_HOME := $(shell R RHOME)
sources := $(wildcard *.cpp)
programs := $(sources:.cpp=)
## include headers and libraries for R
RCPPFLAGS := $(shell $(R_HOME)/bin/R CMD config --cppflags)
RLDFLAGS := $(shell $(R_HOME)/bin/R CMD config --ldflags)
RBLAS := $(shell $(R_HOME)/bin/R CMD config BLAS_LIBS)
RLAPACK := $(shell $(R_HOME)/bin/R CMD config LAPACK_LIBS)
## if you need to set an rpath to R itself, also uncomment
#RRPATH := -Wl,-rpath,$(R_HOME)/lib
## include headers and libraries for Rcpp interface classes
RCPPINCL := $(shell echo 'Rcpp:::CxxFlags()' | $(R_HOME)/bin/R --vanilla --slave)
RCPPLIBS := $(shell echo 'Rcpp:::LdFlags()' | $(R_HOME)/bin/R --vanilla --slave)
## include headers and libraries for RInside embedding classes
RINSIDEINCL := $(shell echo 'RInside:::CxxFlags()' | $(R_HOME)/bin/R --vanilla --slave)
RINSIDELIBS := $(shell echo 'RInside:::LdFlags()' | $(R_HOME)/bin/R --vanilla --slave)
## compiler etc settings used in default make rules
CXX := $(shell $(R_HOME)/bin/R CMD config CXX)
CPPFLAGS := -Wall $(shell $(R_HOME)/bin/R CMD config CPPFLAGS)
CXXFLAGS := $(RCPPFLAGS) $(RCPPINCL) $(RINSIDEINCL) $(shell $(R_HOME)/bin/R CMD config CXXFLAGS)
LDLIBS := $(RLDFLAGS) $(RRPATH) $(RBLAS) $(RLAPACK) $(RCPPLIBS) $(RINSIDELIBS)
all: $(programs)
@test -x /usr/bin/strip && strip $^
run: $(programs)
@for p in $(programs); do echo; echo "Running $$p:"; ./$$p; done
clean:
rm -vf $(programs)
rm -vrf *.dSYM
runAll:
for p in $(programs); do echo "Running $$p"; ./$$p; done
该程序的输出:
In C++ element 0 is 40.7021
问题是值40.702147
被"截断"为40.7021
我要满的
出路是什么?
值本身没有被截断(a),cout
输出流使用默认值进行输出。
您可以查看iomanip
标头以找到强制使用特定输出格式的方法,例如:
std::cout << std::setprecision (9) << v[i] << 'n';
这个完整的程序在这里:
#include <iostream>
#include <iomanip>
int main (void) {
double d = 40.702147;
std::cout << d << 'n';
std::cout << std::setprecision (9) << d << 'n';
return 0;
}
输出:
40.7021
40.702147
(a):请记住,如果不能准确地表示为IEEE754双精度值,那么它可能会被截断,但这里的情况并非如此-双精度大约有十五位小数,而你只看到六位。
您可以通过以下方式指定std::cout
精度:
std::cout.precision(16);
或使用
std::cout << std::setprecision (9) << val;
正如paxdiablo和Joachim建议的
相关文章:
- 防止主数据类型C++的隐式转换
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 将指针设置为"nullptr"并不能防止双重删除?
- 如何防止C++遗留代码中的挂起指针
- Python中的for循环与C++有何不同
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 如何防止初始值设定项列表中的错误值?
- OpenCV 我应该使用智能指针来防止内存泄漏吗?
- C++ 防止在映射中放置()时调用析构函数
- 是否可以防止省略聚合初始化成员?
- 在C++中释放内存期间,迭代器与指针有何不同
- 如何在多个线程中创建 QSql数据库连接时防止名称冲突
- 防止在C++中选择错误文件时提升 xml 解析器崩溃
- std::mutex 如何防止线程修改?
- 防止C++中的硬编码数字
- 防止在复制构造函数中隐式调用基构造函数
- 如何防止 CUDA-GDB 中的<优化输出>值
- 防止组合框被关闭
- 按下特定键时快速按此键.如何防止按下扳机键?(C++)