Fortran和Python之间的精度差异(sin函数) [英] Precision discrepancy between Fortran and Python (sin function)

查看:389
本文介绍了Fortran和Python之间的精度差异(sin函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用正弦函数时,我发现python和Fortran之间存在差异。任何人都可以谈谈这个问题吗?



in python:

  import math 
print(math.sin(6.28318530717959))
>> 3.9077843189710302e-15

在fortran90中:

  print *,sin(6.28318530717959d0)
>>编辑:
编辑:
因为它似乎是一个Fortran编译器问题,所以我使用g95和

$ p $ g $ -O3 test.f90 -o test.exe


解决方案

根据IEEE 754的浮点表示法:



pre $ In [7]:bin(3.3077720792452914e-15.view(np.uint64))
Out [7]:'0b11110011101101110010110011010000000000000000000000000000000000 '

显示截尾尾数,当

<$在[9]:bin(3.3077843189710302e-15.view(np.uint64))
Out [9]:'0b11110011101101110010110011101100111001100111010111010001111111'


可能是一个类型问题,其中float32在过程,甚至起源都是神秘的。

I see a discrepancy between python and Fortran when using the sinus function. Could anyone shed light on this, please?

in python:

      import math
      print(math.sin(6.28318530717959))
      >> 3.3077843189710302e-15

in fortran90:

      print*, sin(6.28318530717959d0)
      >> 3.3077720792452914E-15


EDIT: As it seems to be a Fortran compiler issue, I used g95 with

       g95 -O3 test.f90 -o test.exe

解决方案

According to IEEE 754 for float representation:

In [7]: bin(3.3077720792452914e-15.view(np.uint64))
Out[7]: '0b11110011101101110010110011010000000000000000000000000000000000'

shows a truncated mantissa, when

In [9]: bin(3.3077843189710302e-15.view(np.uint64))
Out[9]: '0b11110011101101110010110011101100111001100111010111010001111111'

shows a plain one.

Probably a type issue, with a float32 in the process, even the origin is mysterious.

这篇关于Fortran和Python之间的精度差异(sin函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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