如何在 gfortran 中使用和验证四精度? [英] How to use and verify quad precision in gfortran?

查看:13
本文介绍了如何在 gfortran 中使用和验证四精度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 gfortran 中使用四精度,但似乎 real*16 不起作用.经过一番摸索,我发现它可能实现为real*10.real*10 实际上是四精度吗?

如何测试我的代码的精度?是否有用于测试精度的标准简单算法?例如,当我想弄清楚计算机 0 是什么时,我会继续除以 2.0,直到达到 0.0.跟踪这些值让我知道计算机何时认为"我的非零数为零 - 给我计算机零.

有没有一种像我描述的那样的算法来计算精度的好方法?

解决方案

添加到现有答案... real*N 是语言的扩展,最好不要使用.real*10 不是四精度.被称为扩展"——它是英特尔处理器提供的 10 字节类型.real*16 在 gfortran 上可能可用也可能不可用,具体取决于编译器版本、硬件和 libquadmath 的可用性.如果在软件中提供,会很慢.

请求所需精度的 Fortran 方法是使用 selected_real_kind 函数为所需精度定义种类值.

整数,参数 :: QR_K = selected_real_kind (32)真实的(种类=QR_K)::MyReal

如果可用,将获得一个四元实数.或者,使用 Fortran 2008 或更高版本,您可以使用 ISO_FORTRAN_ENV",然后可以访问种类值 REAL128.如果精度不可用,则 kind 值为 -1.

一个相关问题:`real*8` 是什么意思?p>

I am trying to use quad precision in gfortran, but it seems like the real*16 does not work. After some fishing around, I have found that it may be implemented as real*10. Is real*10 actually quad precision?

How can I test the precision of my code? Is there a standard simple algorithm for testing precision? For example, when I want to figure out what computer zero is, I continue to divide by 2.0 until I reach 0.0. Keeping track of the values lets me know when the computer 'thinks' that my non-zero number is zero - giving me computer zero.

Is there a good way of figuring out the precision with a type of algorithm like I described?

解决方案

Adding to the existing answers... real*N is an extension to the language and is best not used. real*10 is not quad precision. Is is called "extended" -- it is a 10 byte type provided by Intel processors. real*16 may or may not be available on gfortran, depending on compiler version, hardware and availability of libquadmath. If provided in software, it will be slow.

The Fortran way of asking for the precision that you want is to use the selected_real_kind function to define a kind value for the precision that you want.

integer, parameter :: QR_K = selected_real_kind (32)
real (kind=QR_K) :: MyReal

Will obtain a quad real number, if it is available. Alternatively, with Fortran 2008 or later you can "use ISO_FORTRAN_ENV" and then have access to the kind value REAL128. The kind values will be -1 if the precision is unavailable.

A related question: What does `real*8` mean?

这篇关于如何在 gfortran 中使用和验证四精度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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