Compile-time calculation: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added solution for EDSAC)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(12 intermediate revisions by 4 users not shown)
Line 245:
 
In both cases, the identifier <code>factorial10</code> is given the value 3628800 without any runtime calculations, although in many (or perhaps most) BASICs the first one is handled similarly to C's <code>#define</code>: if it isn't used elsewhere in the code, it doesn't appear at all in the final executable.
 
=={{header|BASIC256}}==
<syntaxhighlight lang="basic">factorial = 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10
print "10! = "; factorial # 3628800</syntaxhighlight>
 
=={{header|C}}==
Line 832 ⟶ 836:
 
Note: Currently, the mediawiki implementation is corrupting the above display due to a cascading sequence of bad design decisions and mis-interpreted specifications on the part of someone "contributing" to that implementation. To work around this issue, and see the original display, you can currently use either the "Edit" or "View Source" option, depending on whether you are logged in to rosettacode with an account that has edit rights here. (Please don't actually save changes though.) If you are using View Source, you might want to do that in a new tab (so you also stay here with this view) and use your browser's search capability to quickly scroll to this location in the source view.
 
=={{header|Java}}==
<pre>
The Java compiler is able to calculate expressions that contain constant variables
and certain operators during code compilation.
As defined in the Java language specification,
the following operators and expressions may be used for constant expressions:
 
Unary operators: +, -, ~, !
Multiplicative operators: *, /, %
Additive operators: +, –
Shift operators: <<, >>, >>>
Relational operators: <, <=, >, >=
Equality operators: ==, !=
Bitwise and logical operators: &, ^, |
Conditional-and and the conditional-or operator: &&, ||
Ternary conditional operator: ?:
Parenthesized expressions whose contained expression is a constant expression
Simple names that refer to constant variables
</pre>
<syntaxhighlight lang="java">
 
public final class CompileTimeCalculation {
 
public static void main(String[] aArgs) {
System.out.println(tenFactorial);
}
private static int tenFactorial = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1;
 
}
</syntaxhighlight>
{{ out }}
<pre>
3628800
</pre>
 
=={{header|Julia}}==
Line 1,346 ⟶ 1,387:
 
Like Perl 5, we also have a BEGIN block, but it also works to introduce a blockless statement,
the value of which will be stored up to be used inas the surroundingan expression at run time:
 
<syntaxhighlight lang="raku" line> say( BEGIN [*] 2..10);</syntaxhighlight>
 
=={{header|REXX}}==
Line 1,393 ⟶ 1,434:
func factorial nr if nr = 1 return 1 else return nr * factorial(nr-1) ok
</syntaxhighlight>
 
=={{header|Run BASIC}}==
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
<syntaxhighlight lang="vb">factorial = 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10
print "10! = "; factorial ' 3628800</syntaxhighlight>
 
=={{header|Rust}}==
Line 1,412 ⟶ 1,459:
 
=={{header|Scala}}==
 
Scala 3 supports proper compile time evaluation
 
<syntaxhighlight lang="scala">
transparent inline def factorial(inline n: Int): Int =
inline n match
case 0 => 1
case _ => n * factorial(n - 1)
 
inline val factorial10/*: 3628800*/ = factorial(10)
</syntaxhighlight>
 
Alternative version that works with Scala 2:
 
<syntaxhighlight lang="scala">object Main extends {
val tenFactorial = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2
Line 1,670 ⟶ 1,731:
 
Not that it makes much difference in practice as the compiler which is written in C is so quick (at least with scripts of moderate length and on modern hardware) that the compile and runtime stages are indistinguishable to the user.
<syntaxhighlight lang="ecmascriptwren">var factorial10 = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2
 
System.print(factorial10)</syntaxhighlight>
Line 1,737 ⟶ 1,798:
RET
</pre>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="basic">factorial = 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10
print "10! = ", factorial // 3628800</syntaxhighlight>
 
=={{header|Z80 Assembly}}==
9,482

edits