真实的数据类型fortran 90 [英] Real data type fortran 90

查看:229
本文介绍了真实的数据类型fortran 90的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题。还有一个类似的问题,但我没有找到确切的问题。



我只是在fortran 90中检查实际数据类型的限制(使用ifort编译器),理由是我的实际代码可能会达到此限制。为了测试,我只给了一个参数 mval1 (见代码)并乘以2.在ifort手册中,它表示它可以采用的最大值是10E38,我的值比这个小得多。但在打印时,它会随机输出数字(输出在底部给出)。任何人都可以帮助我解决这个问题

 程序测试

IMPLICIT NONE
REAL(KIND = 8),PARAMETER :: mval1 = 1073E12
REAL(KIND = 8):: j
j = real(2 * mval1)
PRINT *,j,mval1

结束程序测试

输出

  2.146000005234688E + 015 1.073000002617344E + 015 


解决方案

添加到您已有的评论中的两点。

其中:Fortran real(kind = 8)变量与IEEE-754双精度数字,最大正值为 1.797693134862316E + 308 。对于单精度IEEE浮点数,英特尔Fortran real(kind = 4),最大值为 3.4028235E + 38 它大概是你的 10E38



二:您的声​​明

  REAL(KIND = 8),PARAMETER :: mval1 = 1073E12 

将默认的实际值赋值给 mval1 。我不知道你的默认编译器设置是什么,但检查会发生什么,如果你将该行更改为

  REAL(KIND = 8 ),PARAMETER :: mval1 = 1073E12_8 

明确声明文字是 kind = 8



您对这个问题的回答并不完全问,而您对其他评论没有给出的答案是:浮点算法是棘手的,而使用它的程序员应该熟悉至少基本的技巧。 维基百科关于浮点数的文章数字和算术是您自学的好起点。不管别人告诉你什么,直到你开始试图实现你自己的浮点数和算术时,忽略Goldberg的论文每个计算机科学家应该知道的有关浮点算术的知识。


A simple question. A similar question was there, but I did not get the exact one I am looking for.

I was just checking the limits of real data type in fortran 90 (using ifort compiler), reason being my actual code might reach that limit. To test, I simply gave a parameter mval1 (see code) and multiplied by 2. In ifort manual it says that the maximum value it can take is 10E38 and my value is much smaller than this. But while printing it is taking random digits towards the end (output is given at the bottom). Could anyone help me through this

PROGRAM TEST

  IMPLICIT NONE
  REAL(KIND=8), PARAMETER :: mval1 = 1073E12
  REAL(KIND=8) :: j
  j = real(2*mval1)
  PRINT *, j,mval1

END PROGRAM TEST

Output

 2.146000005234688E+015  1.073000002617344E+015

解决方案

Two points to add to the commentary you've already had.

One: In Intel Fortran real(kind=8) variables are the same as IEEE-754 double precision numbers, and the maximum positive value is 1.797693134862316E+308. For single precision IEEE floating-point numbers, Intel Fortran real(kind=4), the maximum value is 3.4028235E+38 which is approximately your 10E38.

Two: Your declaration

REAL(KIND=8), PARAMETER :: mval1 = 1073E12

assigns a value of default real kind to mval1. I don't know what your default compiler settings are but check what happens if you change that line to

REAL(KIND=8), PARAMETER :: mval1 = 1073E12_8

which explicitly declares that the literal is of kind=8.

The answer to the question you don't quite ask, and which you are not quite given by the other comments is: floating-point arithmetic is tricky and programmers who would use it ought to familiarise themselves with at least the basic tricks. The Wikipedia article on floating-point numbers and arithmetic is a good starting point for your self-education. Whatever anyone else tells you ignore Goldberg's paper What every computer scientist should know about floating-point arithmetic until you start trying to implement your own floating-point numbers and arithmetic.

这篇关于真实的数据类型fortran 90的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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