Jensen's Device: Difference between revisions

Added Dart
(add lambdatalk code)
(Added Dart)
 
(25 intermediate revisions by 13 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 296 ⟶ 305:
</pre>
 
=={{header|BASIC256BASIC}}==
==={{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 304 ⟶ 321:
next i
print temp
end subroutine</syntaxhighlight>
 
call Evaluation()
end</syntaxhighlight>
{{out}}
<pre>5.18737751764</pre>
5.18737751764
</pre>
 
==={{header|BBC BASIC}}===
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> PRINT FNsum(j, 1, 100, FNreciprocal)
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
 
let i = lo
 
do
 
 
let temp = temp + ( 1 / i )
let i = i + 1
 
wait
 
loop i < hi + 1
 
print temp
 
end</syntaxhighlight>
 
=={{header|D}}==
Line 521 ⟶ 739:
sum(i, 1, 100, 1.0/i).writeln;
}</syntaxhighlight>
{{out}}
<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}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
 
type TTerm = function(i: integer): real;
 
function Term(I: integer): double;
begin
Term := 1 / I;
end;
 
 
function Sum(var I: integer; Lo, Hi: integer; Term: TTerm): double;
begin
Result := 0;
I := Lo;
while I <= Hi do
begin
Result := Result + Term(I);
Inc(I);
end;
end;
 
 
procedure ShowJensenDevice(Memo: TMemo);
var I: LongInt;
begin
Memo.Lines.Add(FloatToStrF(Sum(I, 1, 100, @Term), ffFixed,18,15));
end;
 
 
 
 
</syntaxhighlight>
{{out}}
<pre>
5.187377517639621
5.18738</pre>
 
Elapsed Time: 1.037 ms.
 
</pre>
 
 
=={{header|DWScript}}==
Line 592 ⟶ 875:
<pre>
5.1873775176396215
</pre>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
fun sum = real by int lo, int hi, fun term
real temp = 0.0
for int i = lo; i <= hi; ++i do temp += term(i) end
return temp
end
writeLine(sum(1, 100, real by int i do return 1.0/i end))
</syntaxhighlight>
{{out}}
<pre>
5.1873775176396202608051176755
</pre>
 
Line 619 ⟶ 916:
=={{header|Euler}}==
{{Trans|ALGOL 60}}
'''begin'''
<syntaxhighlight lang="euler">
'''new''' i; '''new''' sum;
begin
sum &lt;- ` '''formal''' i; '''formal''' lo; '''formal''' hi; '''formal''' term;
new i; new sum;
sum <- ` formal i; formal lo; formal hi; formal term;'''begin'''
begin '''new''' temp; '''label''' loop;
new temp; label&lt;- loop0;
temp <i &lt;- 0lo;
loop: i <- lo;'''begin'''
loop: begin temp &lt;- temp + term;
temp <'''if''' [ i &lt;- tempi + term1 ] &lt;= hi '''then''' '''goto''' loop '''else''' 0
if [ i <- i + 1 ] <= hi then goto loop else 0'''end''';
end; temp
temp'''end'''
end&apos;;
';
'''out''' sum( @i, 1, 100, `1/i&apos; )
 
'''end''' $
out sum( @i, 1, 100, `1/i' )
end $
</syntaxhighlight>
{{out}}
<pre>
Line 680 ⟶ 975:
The following version passes i and 1/i as execution tokens and is thus closer to the original, but less idiomatic:
 
<syntaxhighlight lang="forth">fvariable: iisum \( i-xt islo ahi Forthterm-xt word-- thatr we need)
\ stack effects: sumi-xt ( xt1-- loaddr hi); xt2term-xt ( -- rr1 )
0e swap 1+ rot ?do ( addrr1 xtxt1 r1xt2 )
i s>f2 overpick execute f! dup execute f+
loop 2drop ;
 
' ii 1 100 :noname 1e ii f@ f/ ; sum f.</syntaxhighlight>
variable i1 \ avoid conflict with Forth word I
' i1 1 100 :noname 1e i1 @ s>f f/ ; sum f.</syntaxhighlight>
 
Inspired by the macro-based versions here's a more idiomatic approach that is closer to the original than the first version above (Forth-2012 code):
 
<syntaxhighlight lang="forth">: sum< ( run-time: hi+1 lo -- 0e )
0e0 postpone fliteral postpone ?do ; immediate
 
: >sum ( run-time: r1 r2 -- r3 )
postpone f+ postpone loop ; immediate
 
: main ( -- )
101 1 sum< 1e0 i s>f f/ >sum f. ;
main</syntaxhighlight>
 
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 722 ⟶ 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|Go}}==
Line 782 ⟶ 1,076:
import Data.STRef
 
sum_ :: STRef s Double -> Double -> Double -> ST s Double -> ST s Double
-> ST s Double -> ST s Double
sum_ ref_i lo hi term = sum <$> mapM ((>> term) . writeSTRef ref_i) [lo .. hi]
sum_ ref lo hi term =
do
vs <- forM [lo .. hi]
(\k -> do { writeSTRef ref k
; term } )
return $ sum vs
 
foo :: Double
foo =
runST $
do iref <- newSTRef undefined -- initial value doesn't matter
sum_ i 1 100 $ recip-- <$>initial readSTRefvalue idoesn't matter
sum_ ref 1 100 $
do
k <- readSTRef ref
return $ recip k
 
main :: IO ()
Line 965 ⟶ 1,269:
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def jensen
{+ {S.map {lambda {:i} {/ 1 :i}}
{lambda {:n}
{S.serie 1 100}}}
{+ {S.map {lambda {:i} {/ 1 :i}}
{S.serie 1 :n}} }}}
-> jensen
 
{jensen 100}
-> 5.187377517639621
</syntaxhighlight>
I probably didn't understand this task, what's going on ...
 
=={{header|Lua}}==
Line 1,356 ⟶ 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,709 ⟶ 1,999:
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var i;
func sum (i, lo, hi, term) {
var temp = 0;
for (*i = lo; *i <= hi; (*i)++) {
temp += term.run;
};
return temp;
};
say sum(\i, 1, 100, { 1 / i });</syntaxhighlight>
{{out}}
<pre>5.1873775176396202608051176756582531579089918737751763962026080511767565825315790897212671</pre>
 
=={{header|Simula}}==
Line 1,831 ⟶ 2,121:
=={{header|Wren}}==
As Wren doesn't support call by name, call by reference nor pointers we need to 'box' the global numeric variable 'i' and use a function for 'term' to simulate Jensen's device. This works because all user defined types are reference types and functions can capture external variables.
<syntaxhighlight lang="ecmascriptwren">class Box {
construct new(v) { _v = v }
v { _v }
Line 1,856 ⟶ 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 1,901 ⟶ 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