在C ++中以'D'作为指数前缀读取双重科学符号 [英] Read double scientific notation with 'D' as exponent prefix in C++

查看:129
本文介绍了在C ++中以'D'作为指数前缀读取双重科学符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取写为-0.49430978D-02的值,但它们仅以-0.49430978的形式出现.我将字符保存在字符串vs中,并且都尝试过

myval = stod(vs)

以及

sscanf(vs.c_str(), "%le", &myval)

但是他们两个都没有读懂科学计数法中的指数.是因为它是D而不是E吗?如何在c++中正确读取此值?

D格式是某些Fortran实现常见的不常见浮点表示形式,用于表示双精度(或更高精度). AFAIK,所有其他环境都将E用作浮点,并且不表示底层精度.

我认为您唯一的选择是将值读取为字符串,用E替换任何D字符,然后用strtod()或同等功能完成处理.

I am trying to read values that are written as -0.49430978D-02, but they are only coming in as -0.49430978. I have the characters saved in a string vs and have attempted both

myval = stod(vs)

as well as

sscanf(vs.c_str(), "%le", &myval)

but both of them fail to read the exponent in the scientific notation. Is it because it is D and not E? How do I read this value correctly in c++?

解决方案

The D format is an uncommon floating point representation common to some Fortran implementations to indicate double precision (or greater). AFAIK, all other environments use E for floating point and do not indicate the underlying precision.

I think your only choice is to read the value as a string, replace any D characters with E and then finish processing with strtod() or equivalent.

这篇关于在C ++中以'D'作为指数前缀读取双重科学符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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