Jensen's Device: Difference between revisions

Added Dart
(→‎{{header|Forth}}: added macro-based variant)
(Added Dart)
 
(5 intermediate revisions by 3 users not shown)
Line 260:
 
</syntaxhighlight>
 
=={{header|Arturo}}==
{{trans|Ruby}}
Line 269 ⟶ 270:
]
print ["harmonicSum 1->100:" harmonicSum 'i 1 100 {1.0 / i}]</syntaxhighlight>
 
{{out}}
 
<pre>harmonicSum 1->100: 5.187377517639621</pre>
 
=={{header|Asymptote}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="Asymptote">real temp = 0;
for(int i = 1; i <= 100; ++i) {
temp += 1/i;
}
write(temp);</syntaxhighlight>
{{out}}
<pre>5.18737751763962</pre>
 
=={{header|AWK}}==
Line 297 ⟶ 306:
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
Same code as [[#GW-BASIC|GW-BASIC]]
 
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">subroutine Evaluation()
call Evaluation()
end
 
subroutine Evaluation()
lo = 1 : hi = 100 : temp = 0
for i = lo to hi
Line 305 ⟶ 321:
next i
print temp
end subroutine</syntaxhighlight>
 
call Evaluation()
end</syntaxhighlight>
{{out}}
<pre>5.18737751764</pre>
5.18737751764
</pre>
 
==={{header|BBC BASIC}}===
Line 328 ⟶ 339:
DEF FNreciprocal = 1/i</syntaxhighlight>
Output:
<pre>5.18737752</pre>
 
5.18737752
==={{header|Chipmunk Basic}}===
</pre>
{{trans|QBasic}}
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="vbnet">100 call evaluation
110 end
120 sub evaluation()
130 lo = 1
140 hi = 100
150 temp = 0
160 for i = lo to hi
170 temp = temp+(1/i)
180 next i
190 print temp
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}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Sub Evaluation()
 
Dim i As Integer, lo As Integer = 1, hi As Integer = 100
Dim tmp As Float = 0
 
For i = lo To hi
tmp += (1 / i)
Next
Print tmp
 
End Sub
 
Public Sub Main()
 
Evaluation
 
End </syntaxhighlight>
 
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
{{works with|Applesoft BASIC}}
{{works with|Chipmunk Basic}}
{{works with|QBasic}}
{{works with|QB64}}
{{works with|Quite BASIC}}
{{works with|MSX BASIC}}
<syntaxhighlight lang="qbasic">100 GOSUB 120
110 END
120 REM Evaluation
130 LET A = 1
140 LET B = 100
150 LET T = 0
160 FOR I = A TO B
170 LET T = T + (1/I)
180 NEXT I
190 PRINT T
200 RETURN</syntaxhighlight>
 
==={{header|Minimal BASIC}}===
<syntaxhighlight lang="qbasic">100 GOSUB 120
110 GOTO 210
120 REM Evaluation
130 LET A = 1
140 LET B = 100
150 LET T = 0
160 FOR I = A TO B
170 LET T = T+(1/I)
180 NEXT I
190 PRINT T
200 RETURN
210 END</syntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
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}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|Run BASIC}}
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
{{works with|True BASIC}}
<syntaxhighlight lang="qbasic">CALL EVALUATION
END
 
SUB Evaluation
LET lo = 1
LET hi = 100
LET temp = 0
FOR i = lo TO hi
LET temp = temp + (1 / i)
NEXT i
PRINT temp
END SUB</syntaxhighlight>
 
==={{header|QB64}}===
Same code as [[#QBasic|QBasic]]
 
==={{header|Quite BASIC}}===
Same code as [[#GW-BASIC|GW-BASIC]]
 
==={{header|Run BASIC}}===
Same code as [[#QBasic|QBasic]]
 
==={{header|True BASIC}}===
Same code as [[#QBasic|QBasic]]
 
==={{header|uBasic/4tH}}===
Since uBasic/4tH does not support floating point numbers, fixed point has to be used. Of course, precision suffers significantly.
<syntaxhighlight lang="qbasic">' ** NOTE: it requires a 64-bit uBasic; number ranges are limited. **
 
If Info("wordsize") < 64 Then Print "This program requires a 64-bit uBasic" : End
 
Dim @i(1)
i = 0 ' fake something that resembles a pointer
 
Print Using "+?.####";FUNC(_Ftoi(FUNC(_Sum(i, 1, 100, _Term))))
End
 
_Sum
Param (4)
Local (1)
 
e@ = 0
For @i(a@) = b@ To c@ : e@ = e@ + FUNC(d@) : Next
 
Return (e@)
 
_Term Return (FUNC(_Fdiv(1, @i(i))))
_Fdiv Param (2) : Return ((a@*16384)/b@)
_Ftoi Param (1) : Return ((10000*a@)/16384)</syntaxhighlight>
{{Out}}
<pre>5.1850
 
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}}==
Line 483 ⟶ 722:
CL-USER> (float (sum i 1 100 (/ 1 i)))
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}}==
Line 516 ⟶ 740:
}</syntaxhighlight>
{{out}}
<pre>5.18738</pre>
 
5.18738</pre>
=={{header|Dart}}==
{{trans|C++}}
<syntaxhighlight lang="dart">double i = 0;
double sum(int lo, int hi, double Function() term) {
double temp = 0;
for (i = lo.toDouble(); i <= hi; i++) temp += term();
return temp;
}
 
double termFunc() {
return 1.0 / i;
}
 
void main() {
print(sum(1, 100, termFunc));
}</syntaxhighlight>
{{out}}
<pre>5.187377517639621</pre>
 
=={{header|Delphi}}==
Line 756 ⟶ 998:
This splits <code>sum</code> in two macros: <code>sum&lt;</code> and <code>&gt;sum</code>; in <code>main</code> these two words surround the code corresponding to <code>1/i</code> in the Algol 60 version. The loop limits are <code>101 1</code>, passed on the stack to <code>sum&lt;</code> in the order and semantics (upper bound is excluded) idiomatic in Forth.
 
Concerning the <code>i</code> parameter of the Algol 60 version, that is an artifact of the role of variables for storing data and passing it around in Algol-family languages. Forth's counted loops can access the current loop counter of the innermost loop with <code>i</code> (which is not a variable) without setting a variable, and that is also what one uses inside <code>sum&lt;</code> ... <code>&gt;sum</code>, as shown in <code>main</code>.
 
=={{header|Fortran}}==
Line 793 ⟶ 1,035:
 
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}}==
Line 1,461 ⟶ 1,666:
Output:
<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}}==
Line 1,961 ⟶ 2,146:
5.1873775176396
</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}}==
Line 2,005 ⟶ 2,173:
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>
 
2,122

edits