Arithmetic/Integer: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 315: | Line 315: | ||
remainder :resultat : +1 |
remainder :resultat : +1 |
||
</PRE> |
</PRE> |
||
=={{header|ABAP}}== |
=={{header|ABAP}}== |
||
<lang ABAP>report zz_arithmetic no standard page heading. |
<lang ABAP>report zz_arithmetic no standard page heading. |
||
Line 465: | Line 466: | ||
WriteF('A mod B =\d\n', Mod(a,b)) |
WriteF('A mod B =\d\n', Mod(a,b)) |
||
ENDPROC</lang> |
ENDPROC</lang> |
||
=={{header|APL}}== |
=={{header|APL}}== |
||
<lang apl>∇res ← integer_arithmetic; l; r |
<lang apl>∇res ← integer_arithmetic; l; r |
||
Line 994: | Line 996: | ||
printf("a/b = %d\n", a/b); /* truncates towards 0 (in C99) */ |
printf("a/b = %d\n", a/b); /* truncates towards 0 (in C99) */ |
||
printf("a%%b = %d\n", a%b); /* same sign as first operand (in C99) */ |
printf("a%%b = %d\n", a%b); /* same sign as first operand (in C99) */ |
||
return 0; |
|||
}</lang> |
|||
=={{header|C++}}== |
|||
<lang cpp>#include <iostream> |
|||
int main() |
|||
{ |
|||
int a, b; |
|||
std::cin >> a >> b; |
|||
std::cout << "a+b = " << a+b << "\n"; |
|||
std::cout << "a-b = " << a-b << "\n"; |
|||
std::cout << "a*b = " << a*b << "\n"; |
|||
std::cout << "a/b = " << a/b << ", remainder " << a%b << "\n"; |
|||
return 0; |
return 0; |
||
}</lang> |
}</lang> |
||
Line 1,036: | Line 1,024: | ||
5 % 3 = 2 |
5 % 3 = 2 |
||
5 to the power of 3 = 125</pre> |
5 to the power of 3 = 125</pre> |
||
=={{header|C++}}== |
|||
<lang cpp>#include <iostream> |
|||
int main() |
|||
{ |
|||
int a, b; |
|||
std::cin >> a >> b; |
|||
std::cout << "a+b = " << a+b << "\n"; |
|||
std::cout << "a-b = " << a-b << "\n"; |
|||
std::cout << "a*b = " << a*b << "\n"; |
|||
std::cout << "a/b = " << a/b << ", remainder " << a%b << "\n"; |
|||
return 0; |
|||
}</lang> |
|||
=={{header|Chef}}== |
=={{header|Chef}}== |
||
Line 1,272: | Line 1,274: | ||
x MOD y > 12 |
x MOD y > 12 |
||
</pre> |
</pre> |
||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d>import std.stdio, std.string, std.conv; |
<lang d>import std.stdio, std.string, std.conv; |
||
Line 1,327: | Line 1,330: | ||
[mod: ]P la lb % p sz [sign matches first operand]sz |
[mod: ]P la lb % p sz [sign matches first operand]sz |
||
[pow: ]P la lb ^ p sz</lang> |
[pow: ]P la lb ^ p sz</lang> |
||
=={{header|DCL}}== |
=={{header|DCL}}== |
||
<lang DCL>$ inquire a "Enter first number" |
<lang DCL>$ inquire a "Enter first number" |
||
Line 1,727: | Line 1,731: | ||
<lang excel> |
<lang excel> |
||
=$A1^$B1 |
=$A1^$B1 |
||
</lang> |
|||
=={{header|F_Sharp|F#}}== |
|||
As F# is a functional language, we can easily create a list of pairs of the string name of a function and the function itself to iterate over printing the operation and applying the function to obtain the result: |
|||
<lang fsharp> |
|||
do |
|||
let a, b = int Sys.argv.[1], int Sys.argv.[2] |
|||
for str, f in ["+", ( + ); "-", ( - ); "*", ( * ); "/", ( / ); "%", ( % )] do |
|||
printf "%d %s %d = %d\n" a str b (f a b) |
|||
</lang> |
|||
For example, the output with the arguments 4 and 3 is: |
|||
<lang fsharp> |
|||
4 + 3 = 7 |
|||
4 - 3 = 1 |
|||
4 * 3 = 12 |
|||
4 / 3 = 1 |
|||
4 % 3 = 1 |
|||
</lang> |
</lang> |
||
Line 1,843: | Line 1,864: | ||
-12 ^ 7 = -35831808 |
-12 ^ 7 = -35831808 |
||
</pre> |
</pre> |
||
=={{header|F_Sharp|F#}}== |
|||
As F# is a functional language, we can easily create a list of pairs of the string name of a function and the function itself to iterate over printing the operation and applying the function to obtain the result: |
|||
<lang fsharp> |
|||
do |
|||
let a, b = int Sys.argv.[1], int Sys.argv.[2] |
|||
for str, f in ["+", ( + ); "-", ( - ); "*", ( * ); "/", ( / ); "%", ( % )] do |
|||
printf "%d %s %d = %d\n" a str b (f a b) |
|||
</lang> |
|||
For example, the output with the arguments 4 and 3 is: |
|||
<lang fsharp> |
|||
4 + 3 = 7 |
|||
4 - 3 = 1 |
|||
4 * 3 = 12 |
|||
4 / 3 = 1 |
|||
4 % 3 = 1 |
|||
</lang> |
|||
=={{header|friendly interactive shell}}== |
=={{header|friendly interactive shell}}== |
||
Line 1,896: | Line 1,899: | ||
10 ^ 20 = 100000000000000000000 |
10 ^ 20 = 100000000000000000000 |
||
</lang> |
</lang> |
||
=={{header|FutureBasic}}== |
=={{header|FutureBasic}}== |
||
Line 2,534: | Line 2,536: | ||
{{VI snippet}}<br/> |
{{VI snippet}}<br/> |
||
[[File:LabVIEW_Arithmetic_Integer.png]] |
[[File:LabVIEW_Arithmetic_Integer.png]] |
||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
Line 3,086: | Line 3,086: | ||
And 0&2 = 0 |
And 0&2 = 0 |
||
Or 0!2 = 1</lang> |
Or 0!2 = 1</lang> |
||
=={{header|Nanoquery}}== |
=={{header|Nanoquery}}== |
||
Line 3,112: | Line 3,111: | ||
Remainder: 5 |
Remainder: 5 |
||
Power: 15625</pre> |
Power: 15625</pre> |
||
=={{header|Nemerle}}== |
=={{header|Nemerle}}== |
||
Line 3,319: | Line 3,317: | ||
x MOD y > 12 |
x MOD y > 12 |
||
</pre> |
</pre> |
||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
<lang objeck>bundle Default { |
<lang objeck>bundle Default { |
||
Line 3,502: | Line 3,501: | ||
] |
] |
||
System.showInfo}</lang> |
System.showInfo}</lang> |
||
=={{header|PARI/GP}}== |
|||
Integer division with <code>\</code> rounds to <math>-\infty</math>. There also exists the <code>\/</code> round-to-nearest (ties to <math>+\infty</math>) operator. Ordinary division <code>/</code> does not round but returns rationals if given integers with a non-integral quotient. |
|||
<lang parigp>arith(a,b)={ |
|||
print(a+b); |
|||
print(a-b); |
|||
print(a*b); |
|||
print(a\b); |
|||
print(a%b); |
|||
print(a^b); |
|||
};</lang> |
|||
=={{header|Panda}}== |
=={{header|Panda}}== |
||
Line 3,531: | Line 3,519: | ||
plus ( 3 7 ) => 10 |
plus ( 3 7 ) => 10 |
||
pow ( 3 7 ) => 2187</pre> |
pow ( 3 7 ) => 2187</pre> |
||
=={{header|PARI/GP}}== |
|||
Integer division with <code>\</code> rounds to <math>-\infty</math>. There also exists the <code>\/</code> round-to-nearest (ties to <math>+\infty</math>) operator. Ordinary division <code>/</code> does not round but returns rationals if given integers with a non-integral quotient. |
|||
<lang parigp>arith(a,b)={ |
|||
print(a+b); |
|||
print(a-b); |
|||
print(a*b); |
|||
print(a\b); |
|||
print(a%b); |
|||
print(a^b); |
|||
};</lang> |
|||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
Line 3,559: | Line 3,558: | ||
"exponent: ", $a ** $b, "\n" |
"exponent: ", $a ** $b, "\n" |
||
;</lang> |
;</lang> |
||
=={{header|Perl 6}}== |
|||
{{works with|Rakudo|2015.09}} |
|||
<lang perl6>my Int $a = get.floor; |
|||
my Int $b = get.floor; |
|||
say 'sum: ', $a + $b; |
|||
say 'difference: ', $a - $b; |
|||
say 'product: ', $a * $b; |
|||
say 'integer quotient: ', $a div $b; |
|||
say 'remainder: ', $a % $b; |
|||
say 'exponentiation: ', $a**$b;</lang> |
|||
Note that <code>div</code> doesn't always do integer division; it performs the operation "most appropriate to the |
|||
operand types". [http://perlcabal.org/syn/S03.html#line_729 Synopsis 3] guarantees that <code>div</code> "on built-in integer types is equivalent to taking the floor of a real division". If you want integer division with other types, say <code>floor($a/$b)</code>. |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 3,643: | Line 3,627: | ||
(prinl "Modulus " (% A B)) # Sign of the first operand |
(prinl "Modulus " (% A B)) # Sign of the first operand |
||
(prinl "Power " (** A B)) )</lang> |
(prinl "Power " (** A B)) )</lang> |
||
=={{header|Piet}}== |
=={{header|Piet}}== |
||
[[File:PietArithmaticInteger.png]]<br> |
[[File:PietArithmaticInteger.png]]<br> |
||
Line 3,718: | Line 3,703: | ||
x y exp = |
x y exp = |
||
} def</lang> |
} def</lang> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<lang powershell>$a = [int] (Read-Host First Number) |
<lang powershell>$a = [int] (Read-Host First Number) |
||
Line 3,734: | Line 3,720: | ||
No exponentiation operator exists, but can be worked around with the .NET BCL: |
No exponentiation operator exists, but can be worked around with the .NET BCL: |
||
<lang powershell>[Math]::Pow($a, $b)</lang> |
<lang powershell>[Math]::Pow($a, $b)</lang> |
||
=={{header|ProDOS}}== |
=={{header|ProDOS}}== |
||
<lang ProDOS>IGNORELINE Note: This example includes the math module. |
<lang ProDOS>IGNORELINE Note: This example includes the math module. |
||
Line 3,949: | Line 3,936: | ||
(lcm 8 12) => 24 |
(lcm 8 12) => 24 |
||
</pre> |
</pre> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
{{works with|Rakudo|2015.09}} |
|||
<lang perl6>my Int $a = get.floor; |
|||
my Int $b = get.floor; |
|||
say 'sum: ', $a + $b; |
|||
say 'difference: ', $a - $b; |
|||
say 'product: ', $a * $b; |
|||
say 'integer quotient: ', $a div $b; |
|||
say 'remainder: ', $a % $b; |
|||
say 'exponentiation: ', $a**$b;</lang> |
|||
Note that <code>div</code> doesn't always do integer division; it performs the operation "most appropriate to the |
|||
operand types". [http://perlcabal.org/syn/S03.html#line_729 Synopsis 3] guarantees that <code>div</code> "on built-in integer types is equivalent to taking the floor of a real division". If you want integer division with other types, say <code>floor($a/$b)</code>. |
|||
=={{header|Raven}}== |
=={{header|Raven}}== |
||
Line 4,523: | Line 4,526: | ||
print ("~a = " ^ Int.toString (~a) ^ "\n") (* unary negation, unusual notation compared to other languages *) |
print ("~a = " ^ Int.toString (~a) ^ "\n") (* unary negation, unusual notation compared to other languages *) |
||
end</lang> |
end</lang> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
<lang swift> |
<lang swift> |
||
Line 4,898: | Line 4,902: | ||
println("x%y = ",x % y); // remainder; matches sign of first operand when operands' signs differ |
println("x%y = ",x % y); // remainder; matches sign of first operand when operands' signs differ |
||
println("x.divr(y) = ",x.divr(y)); // (x/y,remainder); sign as above</lang> |
println("x.divr(y) = ",x.divr(y)); // (x/y,remainder); sign as above</lang> |
||
=={{header|zonnon}}== |
=={{header|zonnon}}== |
||
<lang zonnon> |
<lang zonnon> |
||
Line 4,923: | Line 4,928: | ||
remainder: 2 |
remainder: 2 |
||
</pre> |
</pre> |
||
=={{header|ZX Spectrum Basic}}== |
=={{header|ZX Spectrum Basic}}== |
||
<lang zxbasic>5 LET a=5: LET b=3 |
<lang zxbasic>5 LET a=5: LET b=3 |