我正在Fortran 90中做一些数值练习。当我试图用交替的登录参数做一个总和时,我注意到(以我做的方式)Fortran不知道怎么做去做。
例如,我想在k(1(1)^ k)/ 2k的1到10之间做一个总和,所以我做的是
sumk = 0
do k = 1,10
sumk = ((-1)**k)/(2*k) + sumk
end do
但是输出是sumk = 1.我做错了什么?
答案 0 :(得分:4)
如果k
是整数,则表示您正在执行整数运算。这些可能无法达到预期效果,例如: 1/2 = 0
。当然,使用浮点数会产生0.5
,但转换为整数会产生0
。
因此,基本上,您添加到sumk
的部分在您的情况下始终为零,最终会导致sumk=0
。为防止这种情况,您需要使用浮点数:
sumk = real(((-1)**k))/real(2*k) + sumk
然后,结果是-0.322817445
(我使用Wolfram Alpha验证)。
当然,有几种方法可以改进这一点,例如迭代计算(-1)**k
,或者通过模运算替换它。
答案 1 :(得分:0)
我在Fortran中的最佳经验总是声明您希望它们在您的代码中使用的数字,就像您使用变量一样。
当您想要使用Double use 1.0e0
时,如果您想使用Single use 1
,并且想要使用Integer时使用sumk = 0.0d0
do k = 1,10
sumk = ((-1.0d0)**dble(k))/(2.0d0*dble(k)) + sumk
end do
。
所以在你的情况下,我建议像:
Configuration file: /Users/MyName/Projects/blog/_config.yml
/Users/MyName/.rvm/rubies/ruby-2.1.1/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- typogruby (LoadError)
from /Users/MyName/.rvm/rubies/ruby-2.1.1/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/MyName/Projects/blog/_plugins/enhanced_kramdown.rb:3:in `<top (required)>'
from /Users/MyName/.rvm/rubies/ruby-2.1.1/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/MyName/.rvm/rubies/ruby-2.1.1/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/MyName/.rvm/gems/ruby-2.1.1/gems/jekyll-2.5.3/lib/jekyll/plugin_manager.rb:74:in `block (2 levels) in require_plugin_files'
from /Users/MyName/.rvm/gems/ruby-2.1.1/gems/jekyll-2.5.3/lib/jekyll/plugin_manager.rb:73:in `each'
from /Users/MyName/.rvm/gems/ruby-2.1.1/gems/jekyll-2.5.3/lib/jekyll/plugin_manager.rb:73:in `block in require_plugin_files'
from /Users/MyName/.rvm/gems/ruby-2.1.1/gems/jekyll-2.5.3/lib/jekyll/plugin_manager.rb:72:in `each'
from /Users/MyName/.rvm/gems/ruby-2.1.1/gems/jekyll-2.5.3/lib/jekyll/plugin_manager.rb:72:in `require_plugin_files'
from /Users/MyName/.rvm/gems/ruby-2.1.1/gems/jekyll-2.5.3/lib/jekyll/plugin_manager.rb:18:in `conscientious_require'
from /Users/MyName/.rvm/gems/ruby-2.1.1/gems/jekyll-2.5.3/lib/jekyll/site.rb:79:in `setup'
from /Users/MyName/.rvm/gems/ruby-2.1.1/gems/jekyll-2.5.3/lib/jekyll/site.rb:41:in `initialize'
from /Users/MyName/.rvm/gems/ruby-2.1.1/gems/jekyll-2.5.3/lib/jekyll/commands/build.rb:29:in `new'
from /Users/MyName/.rvm/gems/ruby-2.1.1/gems/jekyll-2.5.3/lib/jekyll/commands/build.rb:29:in `process'
from /Users/MyName/.rvm/gems/ruby-2.1.1/gems/jekyll-2.5.3/lib/jekyll/commands/serve.rb:26:in `block (2 levels) in init_with_program'
from /Users/MyName/.rvm/gems/ruby-2.1.1/gems/mercenary-0.3.5/lib/mercenary/command.rb:220:in `call'
from /Users/MyName/.rvm/gems/ruby-2.1.1/gems/mercenary-0.3.5/lib/mercenary/command.rb:220:in `block in execute'
from /Users/MyName/.rvm/gems/ruby-2.1.1/gems/mercenary-0.3.5/lib/mercenary/command.rb:220:in `each'
from /Users/MyName/.rvm/gems/ruby-2.1.1/gems/mercenary-0.3.5/lib/mercenary/command.rb:220:in `execute'
from /Users/MyName/.rvm/gems/ruby-2.1.1/gems/mercenary-0.3.5/lib/mercenary/program.rb:42:in `go'
from /Users/MyName/.rvm/gems/ruby-2.1.1/gems/mercenary-0.3.5/lib/mercenary.rb:19:in `program'
from /Users/MyName/.rvm/gems/ruby-2.1.1/gems/jekyll-2.5.3/bin/jekyll:20:in `<top (required)>'
from /Users/MyName/.rvm/gems/ruby-2.1.1/bin/jekyll:23:in `load'
from /Users/MyName/.rvm/gems/ruby-2.1.1/bin/jekyll:23:in `<main>'
from /Users/MyName/.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `eval'
from /Users/MyName/.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `<main>'
顺便说一句,如果你没有任何内存问题,我总是会使用双精度。