Jensen's Device: Difference between revisions
Content added Content deleted
(Added various BASIC dialects (Applesoft BASIC, Chipmunk Basic, Gambas, GW-BASIC, Minimal BASIC, MSX Basic, QBasic, QB64, Quite BASIC, Run BASIC and True BASIC)) |
(Grouping BASIC dialects) |
||
Line 346: | Line 346: | ||
190 print temp |
190 print temp |
||
200 end sub</syntaxhighlight> |
200 end sub</syntaxhighlight> |
||
==={{header|Craft Basic}}=== |
|||
<syntaxhighlight lang="basic">precision 4 |
|||
define lo = 1, hi = 100, temp = 0 |
|||
for i = lo to hi |
|||
let temp = temp + (1 / i) |
|||
wait |
|||
next i |
|||
print temp</syntaxhighlight> |
|||
{{out| Output}}<pre>5.1873</pre> |
|||
==={{header|FreeBASIC}}=== |
|||
<syntaxhighlight lang="vbnet">Sub Evaluation |
|||
Dim As Integer i, lo = 1, hi = 100 |
|||
Dim As Double temp = 0 |
|||
For i = lo To hi |
|||
temp += (1/i) ''r(i) |
|||
Next i |
|||
Print temp |
|||
End Sub |
|||
Evaluation |
|||
Sleep</syntaxhighlight> |
|||
{{out}} |
|||
<pre>5.187377517639621</pre> |
|||
==={{header|FutureBasic}}=== |
|||
<syntaxhighlight lang="futurebasic">local fn JensensDevice( lo as long, hi as long ) as double |
|||
double i, temp = 0.0 |
|||
for i = lo to hi |
|||
temp = temp + (1/i) |
|||
next |
|||
end fn = temp |
|||
print fn JensensDevice( 1, 100 ) |
|||
HandleEvents</syntaxhighlight> |
|||
{{output}} |
|||
<pre>5.187377517639621</pre> |
|||
==={{header|Gambas}}=== |
==={{header|Gambas}}=== |
||
Line 405: | Line 449: | ||
{{works with|MSX BASIC|any}} |
{{works with|MSX BASIC|any}} |
||
Same code as [[#GW-BASIC|GW-BASIC]] |
Same code as [[#GW-BASIC|GW-BASIC]] |
||
==={{header|PureBasic}}=== |
|||
{{trans|C}} |
|||
<syntaxhighlight lang="purebasic">Prototype.d func() |
|||
Global i |
|||
Procedure.d Sum(*i.Integer, lo, hi, *term.func) |
|||
Protected Temp.d |
|||
For i=lo To hi |
|||
temp + *term() |
|||
Next |
|||
ProcedureReturn Temp |
|||
EndProcedure |
|||
Procedure.d term_func() |
|||
ProcedureReturn 1/i |
|||
EndProcedure |
|||
Answer.d = Sum(@i, 1, 100, @term_func())</syntaxhighlight> |
|||
==={{header|QBasic}}=== |
==={{header|QBasic}}=== |
||
Line 466: | Line 530: | ||
0 OK, 0:313 </pre> |
0 OK, 0:313 </pre> |
||
==={{header|Yabasic}}=== |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="yabasic">Evaluation() |
|||
end |
|||
sub Evaluation() |
|||
lo = 1 : hi = 100 : temp = 0 |
|||
for i = lo to hi |
|||
temp = temp + (1/i) //r(i) |
|||
next i |
|||
print temp |
|||
end sub</syntaxhighlight> |
|||
{{out}} |
|||
<pre>5.18738</pre> |
|||
==={{header|ZX Spectrum Basic}}=== |
|||
<syntaxhighlight lang="qbasic">10 DEF FN r(x)=1/x |
|||
20 LET f$="FN r(i)" |
|||
30 LET lo=1: LET hi=100 |
|||
40 GO SUB 1000 |
|||
50 PRINT temp |
|||
60 STOP |
|||
1000 REM Evaluation |
|||
1010 LET temp=0 |
|||
1020 FOR i=lo TO hi |
|||
1030 LET temp=temp+VAL f$ |
|||
1040 NEXT i |
|||
1050 RETURN </syntaxhighlight> |
|||
{{out}} |
|||
<pre>5.1873775</pre> |
|||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
Line 618: | Line 713: | ||
CL-USER> (float (sum i 1 100 (/ 1 i))) |
CL-USER> (float (sum i 1 100 (/ 1 i))) |
||
5.1873775</syntaxhighlight> |
5.1873775</syntaxhighlight> |
||
=={{header|Craft Basic}}== |
|||
<syntaxhighlight lang="basic">precision 4 |
|||
define lo = 1, hi = 100, temp = 0 |
|||
for i = lo to hi |
|||
let temp = temp + (1 / i) |
|||
wait |
|||
next i |
|||
print temp</syntaxhighlight> |
|||
{{out| Output}}<pre>5.1873</pre> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
Line 928: | Line 1,008: | ||
Incidentally, a subroutine such as TEST(A,B) invoked as TEST(X,X) enables the discovery of copy-in, copy-out parameter passing. Within the routine, modify the value of A and look to see if B suddenly has a new value also. |
Incidentally, a subroutine such as TEST(A,B) invoked as TEST(X,X) enables the discovery of copy-in, copy-out parameter passing. Within the routine, modify the value of A and look to see if B suddenly has a new value also. |
||
=={{header|FreeBASIC}}== |
|||
<syntaxhighlight lang="freebasic">Sub Evaluation |
|||
Dim As Integer i, lo = 1, hi = 100 |
|||
Dim As Double temp = 0 |
|||
For i = lo To hi |
|||
temp += (1/i) ''r(i) |
|||
Next i |
|||
Print temp |
|||
End Sub |
|||
Evaluation |
|||
Sleep</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
5.187377517639621 |
|||
</pre> |
|||
=={{header|FutureBasic}}== |
|||
<syntaxhighlight lang="futurebasic"> |
|||
local fn JensensDevice( lo as long, hi as long ) as double |
|||
double i, temp = 0.0 |
|||
for i = lo to hi |
|||
temp = temp + (1/i) |
|||
next |
|||
end fn = temp |
|||
print fn JensensDevice( 1, 100 ) |
|||
HandleEvents |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
<pre> |
|||
5.187377517639621 |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
Line 1,596: | Line 1,639: | ||
Output: |
Output: |
||
<pre>-> "5.187383"</pre> |
<pre>-> "5.187383"</pre> |
||
=={{header|PureBasic}}== |
|||
{{trans|C}} |
|||
<syntaxhighlight lang="purebasic">Prototype.d func() |
|||
Global i |
|||
Procedure.d Sum(*i.Integer, lo, hi, *term.func) |
|||
Protected Temp.d |
|||
For i=lo To hi |
|||
temp + *term() |
|||
Next |
|||
ProcedureReturn Temp |
|||
EndProcedure |
|||
Procedure.d term_func() |
|||
ProcedureReturn 1/i |
|||
EndProcedure |
|||
Answer.d = Sum(@i, 1, 100, @term_func())</syntaxhighlight> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 2,096: | Line 2,119: | ||
5.1873775176396 |
5.1873775176396 |
||
</pre> |
</pre> |
||
=={{header|Yabasic}}== |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="yabasic">sub Evaluation() |
|||
lo = 1 : hi = 100 : temp = 0 |
|||
for i = lo to hi |
|||
temp = temp + (1/i) //r(i) |
|||
next i |
|||
print temp |
|||
end sub |
|||
Evaluation()</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
5.18738 |
|||
</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
Line 2,140: | Line 2,146: | ||
5.187378 |
5.187378 |
||
5.187378 |
5.187378 |
||
</pre> |
|||
=={{header|ZX Spectrum Basic}}== |
|||
<syntaxhighlight lang="zxbasic">10 DEF FN r(x)=1/x |
|||
20 LET f$="FN r(i)" |
|||
30 LET lo=1: LET hi=100 |
|||
40 GO SUB 1000 |
|||
50 PRINT temp |
|||
60 STOP |
|||
1000 REM Evaluation |
|||
1010 LET temp=0 |
|||
1020 FOR i=lo TO hi |
|||
1030 LET temp=temp+VAL f$ |
|||
1040 NEXT i |
|||
1050 RETURN |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
5.1873775 |
|||
</pre> |
</pre> |
||