Abt. Gliederzahl von Taylor-Reihen optimieren
=============================
braucht man äusserst selten. Allerdings ist heutzutage auf wenig Verlass, warum also der Computerarithmetik stets blind vertrauen? Stimmen Funktionswerte wirklich in allen Zahlenintervallen? Am Beispiel Sinus / Cosinus hier mal ein Vergleich. Zugegeben: Was für Freaks...;)
Gruss
P.S.: Vertrauen ist gut - Kontrolle ist besser! (N. Chrustschow)
windowtitle "Taylorreihen für Sin, Cos in Tausendstel-°; Stoppt bei Abweichungen > 1.111*10^-16"
' *** Beweis, daß Taylor-Reihen funktionieren ***
' (PL) Pointlessware, 2013-08 by P. Specht
' Eine SinCos-Funktion für DoublePrecision (14 Stellen),
' erzeugt aus den entsprechenden Taylorreihen
' Anm.: Durch Anwendung nachstehender Regeln könnte man einge hohe Glieder der Taylorreihe einsparen,
' weil sie nur dazu dienen, die Präzision auch bei ungünstigen Winkeln zu gewährleisten:
' sin(a) = cos(90°-a); sin(a) = -cos(a+90°); sin(a) = sin(180°-a) ; sin(a) = -sin(a+180°)
' cos(a) = sin(90°-a); cos(a) = sin(a+90°); cos(a) = -cos(180°-a); cos(a) = -cos(a + 180°)
' sin(2a)= 2*sin(a)*cos(a); cos(2a)=sqr(cos(a))^2-(sin(a))^2
'
Window 800,(%maxy-300):font 0
Appendmenubar 10," Funktionswert Profan-Sin/Cos"+\
" Ergebnis Taylorreihen Allf. Abweichung"
randomize:set("decimals",17)':set("numwidth",21)
declare x!,w!,sin!,cos!,startwert&
print "\n Tausendstelgrad-Bereich [00000-92000] prüfen ab: ";:input startwert&
var f!=pi()/180
WHILELOOP Startwert&,92000,1
w!= 0.001*&Loop*f! 'nach Bogenmaß wandeln
sincos w! 'retour via parameters
sin!=@!(1)
cos!=@!(2)
print "sin(";&Loop;"/1000°)=",sin(w!),sin!,
if abs(sin(w!)-sin!)>1.11*10^-16:print sin(w!)-sin!
if abs(sin(w!)-sin!)>1.111*10^-16
sound 2000,17:waitinput 2400
endif
else
print
endif
case &Loop=1000*int(&Loop/1000): sinustabelle(&Loop/1000)
print "cos(";&Loop;"/1000°)=",cos(w!),cos!,
if abs(cos(w!)-cos!)>1.11*10^-16:print cos(w!)-cos!
if abs(cos(w!)-cos!)>1.111*10^-16:
sound 1400,17:waitinput 2400
endif
else
print
endif
print
if %csrlin>36:waitinput 20:cls:endif
ENDWHILE
waitinput
END
proc retour
endproc
proc sincos
parameters x1! :declare sin!,cos!
declare x2!,x3!,x4!,x5!,x6!,x7!,x8!,x9!,x10!
declare x11!,x12!,x13!,x14!,x15!,x16!,x17!
declare x18!,x19!,x20!,x21!,x22!
x2!=sqr(x1!): x3!=x2!*x1! : x4!=sqr(x2!)
x5!=x3!*x2! : x6!=sqr(x3!): x7!=x4!*x3!
x8!=sqr(x4!): x9!=x5!*x4! :x10!=sqr(x5!)
x11!=x6!*x5! :x12!=sqr(x6!):x13!=x7!*x6!
x14!=sqr(x7!):x15!=x8!*x7! :x16!=sqr(x8!)
x17!=x9!*x8! :x18!=sqr(x9!):x19!=x10!*x9!
x20!=sqr(x10!):x21!=x11!*x10!:x22!=sqr(x11!)
cos!=1 -0.5*x2!\
+0.04166666666666666666666666666667*x4!\
-0.00138888888888888888888888888889*x6!\
+val("2.4801587301587301587301587301587E-5")*x8!\
-val("2.7557319223985890652557319223986E-7")*x10!\
+val("2.0876756987868098979210090321201E-9")*x12!\
-val("1.1470745597729724713851697978682E-11")*x14!\
+val("4.7794773323873852974382074911175E-14")*x16!\
-val("1.5619206968586226462216364350057e-16")*x18!\
+val("4.1103176233121648584779906184361e-19")*x20!
sin!=x1!\
-0.16666666666666666666666666666667*x3!\
+0.00833333333333333333333333333333*x5!\
-0.0001984126984126984126984126984127*x7!\
+val("2.7557319223985890652557319223986e-6")*x9!\
-val("2.5052108385441718775052108385442e-8")*x11!\
+val("1.6059043836821614599392377170155e-10")*x13!\
-val("7.6471637318198164759011319857881e-13")*x15!\
+val("2.8114572543455207631989455830103e-15")*x17!\
-val("8.2206352466243297169559812368723e-18")*x19!\
+val("1.9572941063391261230847574373505e-20")*x21!\
-val("8.8967913924505732867488974425025e-22")*x22!
retour sin!,cos!
endproc
proc sinustabelle
parameters x!
declare sinus$,grad!,old!,vor&,i&
var f!=pi()/180
sinus$=\
"-1.00,-0.01745240643728351281941897851632,"+\
"00°,0.00000000000000000000000000000000 "+\
"01,0.01745240643728351281941897851632 "+\
"02,0.03489949670250097164599518162533 "+\
"03,0.05233595624294383272211862960908 "+\
"04,0.06975647374412530077595883519414 "+\
"05,0.08715574274765817355806427083747 "+\
"06,0.10452846326765347139983415480250 "+\
"07,0.12186934340514748111289391923153 "+\
"08,0.13917310096006544411249666330111 "+\
"09,0.15643446504023086901010531946717 "+\
"10,0.17364817766693034885171662676931 "+\
"11,0.19080899537654481240514048795839 "+\
"12,0.20791169081775933710174228440513 "+\
"13,0.22495105434386499805110720834280 "+\
"14,0.24192189559966772256044237410035 "+\
"15,0.25881904510252076234889883762405 "+\
"16,0.27563735581699918564997157461130 "+\
"17,0.29237170472273672809746869537714 "+\
"18,0.30901699437494742410229341718282 "+\
"19,0.32556815445715666871400893579472 "+\
"20,0.34202014332566873304409961468226 "+\
"21,0.35836794954530027348413778941347 "+\
"22,0.37460659341591203541496377450120 "+\
"23,0.39073112848927375506208458888909 "+\
"24,0.40673664307580020775398599034150 "+\
"25,0.42261826174069943618697848964773 "+\
"26,0.43837114678907741745273454065827 "+\
"27,0.45399049973954679156040836635787 "+\
"28,0.46947156278589077595946228822784 "+\
"29,0.48480962024633702907537962241578 "+\
"30,0.5 "+\
"31,0.51503807491005421008163193639814 "+\
"32,0.52991926423320495404678115181609 "+\
"33,0.54463903501502708222408369208157 "+\
"34,0.55919290347074683016042813998599 "+\
"35,0.57357643635104609610803191282616 "+\
"36,0.58778525229247312916870595463907 "+\
"37,0.60181502315204827991797700044149 "+\
"38,0.61566147532565827966881109284366 "+\
"39,0.62932039104983745270590245827997 "+\
"40,0.64278760968653932632264340990726 "+\
"41,0.65605902899050728478249596402342 "+\
"42,0.66913060635885821382627333068678 "+\
"43,0.68199836006249850044222578471113 "+\
"44,0.69465837045899728665640629942269 "+\
"45,0.70710678118654752440084436210485 "+\
"46,0.71933980033865113935605467445671 "+\
"47,0.73135370161917048328754360827562 "+\
"48,0.74314482547739423501469704897426 "+\
"49,0.75470958022277199794298421956102 "+\
"50,0.76604444311897803520239265055542 "+\
"51,0.77714596145697087997993774367240 "+\
"52,0.78801075360672195669397778783585 "+\
"53,0.79863551004729284628400080406894 "+\
"54,0.80901699437494742410229341718282 "+\
"55,0.81915204428899178968448838591684 "+\
"56,0.82903757255504169200633684150164 "+\
"57,0.83867056794542402963759094180455 "+\
"58,0.84804809615642597038617617869039 "+\
"59,0.85716730070211228746521798014476 "+\
"60,0.86602540378443864676372317075294 "+\
"61,0.87461970713939580028463695866108 "+\
"62,0.88294759285892694203217136031572 "+\
"63,0.89100652418836786235970957141363 "+\
"64,0.89879404629916699278229567669579 "+\
"65,0.90630778703664996324255265675432 "+\
"66,0.91354545764260089550212757198532 "+\
"67,0.92050485345244032739689472330046 "+\
"68,0.92718385456678740080647445113696 "+\
"69,0.93358042649720174899004306313957 "+\
"70,0.93969262078590838405410927732473 "+\
"71,0.94551857559931681034812470751940 "+\
"72,0.95105651629515357211643933337938 "+\
"73,0.95630475596303548133865081661842 "+\
"74,0.96126169593831886191649704855706 "+\
"75,0.96592582628906828674974319972890 "+\
"76,0.97029572627599647230637787403399 "+\
"77,0.97437006478523522853969448008827 "+\
"78,0.97814760073380563792856674786960 "+\
"79,0.98162718344766395349650489981814 "+\
"80,0.98480775301220805936674302458952 "+\
"81,0.98768834059513772619004024769344 "+\
"82,0.99026806874157031508377486734485 "+\
"83,0.99254615164132203498006158933058 "+\
"84,0.99452189536827333692269194498057 "+\
"85,0.99619469809174553229501040247389 "+\
"86,0.99756405025982424761316268064426 "+\
"87,0.99862953475457387378449205843944 "+\
"88,0.99939082701909573000624344004393 "+\
"89,0.99984769515639123915701155881391 "+\
"90,1.0 "+\
"91,0.99984769515639123915701155881391 "+\
"92,0.99939082701909573000624344004393 "+\
"100,.98480775301220805936674302458952 "+\
"361,001745240643728351281941897851632"
sinus$=translate$(sinus$," ",",")
repeat :grad!=val(substr$(sinus$,2*i&+1,",")):inc i&
until x!<=grad!
font 2
print "Beide Werte mit Tabellen-Sinus überprüfen:",substr$(sinus$,2*i&,",")
font 0
sound 1000,100
waitinput 20000
endproc
Alles anzeigen