如何在Fortran中以固定格式打印极小的双精度数字? [英] How do I print extremely small double precision numbers with fixed format in Fortran?

查看:155
本文介绍了如何在Fortran中以固定格式打印极小的双精度数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Fortran中以固定格式打印非常小的双精度数字 ?不知何故,"E"会被截断.

Is there a way to print extremely small double precision numbers with fixed format in Fortran? Somehow, the "E" gets truncated.

在下面的代码中将"ES15"更改为"ES18"无济于事:

Changing the 'ES15' to 'ES18' in the code below doesn't help:

program print_double
  implicit none
  double precision :: x
  x = 2.71818D-200
  write(*,*)                 "fmt=*     : x = ", x
  write(*,fmt='(A,ES15.3)') " fmt=ES15.3: x = ", x
  write(*,fmt='(A,E15.3)')  " fmt= E15.3: x = ", x
end program print_double

这是输出:

$ gfortran print_double.f90 -o print_double && ./print_double
 fmt=*     : x =    2.7181800000000000E-200
 fmt=ES15.3: x =       2.718-200
 fmt= E15.3: x =       0.272-199

$ ifort print_double.f90 -o print_double && ./print_double
 fmt=*     : x =   2.718180000000000E-200
 fmt=ES15.3: x =       2.718-200
 fmt= E15.3: x =       0.272-199

注意:这是针对我检查通过两种不同数学方法获得的解是否一致的代码.在大多数情况下,差异很小(即远低于机器精度),但并非总是如此.

Note: this is for a code in which I check that the solution obtained through two different mathematical approaches agree. Most of the the time, the differences are small (i.e. well below machine precision), but this may not always be the case.

推荐答案

您可以使用Ew.dEe描述符指定指数中的位数.它还适用于科学和工程格式.

You can specify the number of digits in the exponent using the Ew.dEe descriptor. It also works for Scientific and Engineering formats.

write(*,fmt='(A,ES15.3E3)') " fmt=ES15.3E3: x = ", x

给予

fmt=ES15.3E3: x = 2.718E-200

这篇关于如何在Fortran中以固定格式打印极小的双精度数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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