为什么FORTRAN输出数组作为零的元素时,它们是由1/2或1/3相乘? [英] why is fortran outputting the elements of an array as zeros when they are multiplied by 1/2 or 1/3?

查看:347
本文介绍了为什么FORTRAN输出数组作为零的元素时,它们是由1/2或1/3相乘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我写出的folows数组中的元素

So I'm writing out the elements of an array as folows

write(6,'(i4,200(1x,e15.7))')Jtot0, (a*PJjv(i,Jtot0,j,iv),i=1,nenerdif,100)

其中 是一个常量,然而,当这个常数等于 1/2 1/3 输出零,如果它等于1,每一件事情顺利的话,数组元素的实际* 8
我怎样才能解决这个问题,让我是有义务通过倍的 1/3

where a is a constant, however, when this constant is equal to 1/2 or 1/3 the output is zeros, and if it's equal to 1, every thing goes well, the array elements are real*8. how can i overcome this, giving that i'm obligated to multiply by a factor of 1/3?

推荐答案

在的Fortran 1/2 是一个整数除法操作,这将向下舍入到,在这种情况下, 0 。同为 1/3 。如果你想要一个真实的结果,做一个真正的除法运算,如 1.0 / 2.0 。需要注意的是分配 1/2 的结果到一个真正的变量将真正的变量设置为 0.0 ,即整数除法会导致 0 和分配,这恰好接下来,将这个值转换为与其最接近的实际再presentation。

In Fortran 1/2 is an integer division operation which will round down to, in this case, 0. Same for 1/3. If you want a real result, do a real division operation, such as 1.0/2.0. Note that assigning the result of 1/2 to a real variable will set the real variable to 0.0, that is the integer division will result in 0 and the assignment, which happens next, will cast that value to its nearest real representation.

的整数除法产生整数结果这项业务是很常见的编程语言。

This business of integer division producing integer results is very common in programming languages.

这篇关于为什么FORTRAN输出数组作为零的元素时,它们是由1/2或1/3相乘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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