24小时热门版块排行榜    

查看: 447  |  回复: 2

astringent

铜虫 (著名写手)


[交流] 【求助】为什么两种写法得到的值不一样?

为什么把if语句放到不同的位置,结果就不一样了呢?请大家帮忙分析一下,多谢。
写法一(判断语句为h0(t)/=0):
t0=0
             do ntel=1, npoints
         
               neworig = .false.
               if (t0.eq.0) then
                   neworig = .true.
               elseif (ntel-time0(t0).eq.it0)then
                   neworig = .true.
               endif

               if (neworig) then
                  t0 = t0 + 1         ! update number of t=0
                  time0(t0) = ntel    ! store the time of t=0
                  h0(t0) = h(ntel)
                endif
         
                do t=1, t0             ! update tcf for t=0
                   delt=ntel-time0(t)+1 ! actual time minus t=0
                   if(h0(t)/=0.and.(delt.lt.npoints)) then
                      ntime(delt) = ntime(delt)+1d0
                      tcf(delt)=tcf(delt)+h(ntel)*h0(t)
                      tcf0(delt)=tcf0(delt)+h0(t)*h0(t)
                    endif
                 enddo               
              enddo
写法二(判断语句为if (h(ntel)/=0) then)
t0=0
             do ntel=1, npoints
         
               neworig = .false.
               if (h(ntel)/=0) then
               if (t0.eq.0) then
                   neworig = .true.
               elseif (ntel-time0(t0).eq.it0)then
                   neworig = .true.
               endif
              endif

               if (neworig) then
                  t0 = t0 + 1         ! update number of t=0
                  time0(t0) = ntel    ! store the time of t=0
                  h0(t0) = h(ntel)
                endif
         
                do t=1, t0             ! update tcf for t=0
                   delt=ntel-time0(t)+1 ! actual time minus t=0
                   if(delt.lt.npoints) then
                      ntime(delt) = ntime(delt)+1d0
                      tcf(delt)=tcf(delt)+h(ntel)*h0(t)
                      tcf0(delt)=tcf0(delt)+h0(t)*h0(t)
                    endif
                 enddo               
              enddo
回复此楼

» 猜你喜欢

» 抢金币啦!回帖就可以得到:

查看全部散金贴

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zhuwei04

木虫 (正式写手)


astringent(金币+10): 2011-02-13 20:36:36
h(1) 能保证/=0吗?
如果和h(1)=0,写法一比较好吧,至少能保证do t=1, t0 这句不出错
2楼2011-02-13 10:18:59
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
★ ★ ★
astringent(金币+10): 2011-02-13 20:36:39
余泽成(金币+3): 谢谢参与应助! 2011-02-20 18:22:58
你这个程序片断有太多的东西没有交待,所以不能从程序片断上来解读

在下面的这段代码中,你加了一个判断才能进行 neworig 的赋值,但在方法一中,没有这样的判断,因此,可能会导致 neworig 的值在两个程序中不一样,那么可能会直接影响最后的结果……
CODE:
             if (h(ntel)/=0) then
               if (t0.eq.0) then
                   neworig = .true.
               elseif (ntel-time0(t0).eq.it0)then
                   neworig = .true.
               endif
              endif

3楼2011-02-13 10:24:20
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 astringent 的主题更新
普通表情 高级回复 (可上传附件)
信息提示
请填处理意见