标题: [数值计算] 批处理如何更准确地计算e的值? [打印本页]
作者: chineseman2 时间: 2013-1-13 13:33 标题: 批处理如何更准确地计算e的值?
- setlocal delayexpansion
- set a=1
- set b=1
- set c=10000000
- @echo off
- @cls
- :loop
- set /a c=c+(10000000/b)
- set /a a=a+1
- set /a b=b*a
- if %a% equ 500 goto :ex
- goto :loop
- :ex
- set /a c=c-20000000
- @echo E is 2.%c%
- @echo off
- @pause>nul
复制代码
e本来等于2.718281828...
但是它只给出e=2.7182814和一大堆Divide by zero error.
如何更准确地计算e?(bat计算...)
作者: chineseman2 时间: 2013-1-13 13:37
http://zh.wikipedia.org/wiki/E_(%E6%95%B0%E5%AD%A6%E5%B8%B8%E6%95%B0)
作者: batman 时间: 2013-1-13 14:35
本帖最后由 batman 于 2013-1-13 14:51 编辑
批无法直接计算浮点同时cmd只能计算2^31内的值(-2147483648-2147483647),用VBS计算循环到171次也就是b=171!时溢出:- Dim a, b, c
- b = 1 : c = 10000000
- For i = 1 To 500
- On Error Resume Next
- a = a + 1
- b = b * a
- c = c + 10000000 / b
- If Num = "" And Err.Number <> 0 Then Num = i
- Err.Clear
- Next
- c = c - 20000000
- WScript.Echo "E=2." & c & vbCrLf & Num
复制代码
作者: terse 时间: 2013-1-16 16:48
本帖最后由 terse 于 2013-1-16 17:40 编辑
再简化一行
一处笔误 貌似没变化- @echo off&setlocal enabledelayedexpansion
- set lp=100
- set /a A=10000,M=1000
- set/aLp=(Lp+3)/4*14
- for /l %%i in (%LP% -1 1) do set/a "S+=M*A,R_%%i=S%%%%i,S/=%%i"
- set /a "E=S/A+M,M=S%%A,Lp-=14"
- set _E=%_E%%E:~,4%
- for /l %%i in (!Lp! -14 1) do (
- for /l %%j in (%%i -1 1) do set/a "S+=R_%%j*A,R_%%j=S%%%%j,S/=%%j"
- set /a "E=S/A+M,M=S%%A+A"
- set _E=!_E!!E:~-4!
- )
- )
- echo;!_E!
- pause
复制代码
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |