Jensen's Device: Difference between revisions
Added Dart
Basicgames (talk | contribs) |
(Added Dart) |
||
(21 intermediate revisions by 12 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|
==={{header|Applesoft BASIC}}===
Same code as [[#GW-BASIC|GW-BASIC]]
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">
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>
{{out}}
<pre>5.18737751764</pre>
==={{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>
==={{header|Chipmunk Basic}}===
{{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|D}}==
Line 516 ⟶ 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
Elapsed Time: 1.037 ms.
</pre>
=={{header|DWScript}}==
Line 587 ⟶ 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 614 ⟶ 916:
=={{header|Euler}}==
{{Trans|ALGOL 60}}
'''begin'''
'''new''' i; '''new''' sum;
sum <- ` '''formal''' i; '''formal''' lo; '''formal''' hi; '''formal''' term;
loop:
'''out''' sum( @i, 1, 100, `1/i' )
'''end''' $
{{out}}
<pre>
Line 675 ⟶ 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">
\ stack effects:
0e swap 1+ rot ?do (
i
loop 2drop ;
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<</code> and <code>>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<</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<</code> ... <code>>sum</code>, as shown in <code>main</code>.
=={{header|Fortran}}==
Line 717 ⟶ 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|Go}}==
Line 777 ⟶ 1,076:
import Data.STRef
sum_ :: STRef s Double -> Double -> Double
-> ST s Double -> ST s Double
sum_ ref lo hi term =
do
vs <- forM [lo .. hi]
(\k -> do { writeSTRef ref k
; term } )
return $ sum vs
foo :: Double
foo =
runST $
do
sum_ ref 1 100 $
do
k <- readSTRef ref
return $ recip k
main :: IO ()
Line 1,357 ⟶ 1,666:
Output:
<pre>-> "5.187383"</pre>
=={{header|Python}}==
Line 1,710 ⟶ 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 })
{{out}}
<pre>5.
=={{header|Simula}}==
Line 1,832 ⟶ 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="
construct new(v) { _v = v }
v { _v }
Line 1,857 ⟶ 2,146:
5.1873775176396
</pre>
=={{header|zkl}}==
Line 1,902 ⟶ 2,173:
5.187378
5.187378
</pre>
|