Arithmetic/Integer: Difference between revisions

m
m (Automated syntax highlighting fixup (second round - minor fixes))
imported>Arakov
 
(21 intermediate revisions by 15 users not shown)
Line 333:
.include "../includeARM64.inc"
</syntaxhighlight>
{{Output : Out}}
<PRE>
pi@debian-buster-64:~/asm64/rosetta/asm3 $ arith64 101 25
Line 978:
PRINT "MOD(", a%, ", ", b%, ") is ", MOD(a%, b%), ", same sign as first operand"
PRINT "POW(", a%, ", ", b%, ") is ", INT(POW(a%, b%))</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">10 input "Enter two integers separated by a comma: ";a,b
20 print " Sum: ";a+b
30 print "Difference: ";a-b
40 print " Product: ";a*b
50 print " Quontent: ";int(a/b)
60 print " Remainder: ";a mod b
70 print " Power: ";a^b</syntaxhighlight>
 
==={{header|Commodore BASIC}}===
Line 988 ⟶ 998:
70 PRINT "REMAINDER OR MODULO:";A%;"%";B%;"=";A%-INT(A%/B%)*B%
80 PRINT "POWER:";A%;"^";B%;"=";A%^B%</syntaxhighlight>
 
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
<syntaxhighlight lang="qbasic">10 INPUT "Enter two integers separated by a comma: ";A, B
20 PRINT " Sum:"; A + B
30 PRINT "Difference:"; A - B
40 PRINT " Product:"; A * B
50 PRINT " Quontent:"; A \ B
60 PRINT " Remainder:"; A MOD B
70 PRINT " Power:"; A ^ B</syntaxhighlight>
 
==={{header|Minimal BASIC}}===
<syntaxhighlight lang="qbasic">10 PRINT "ENTER A INTEGER"
20 INPUT A
30 PRINT "ENTER ANOTHER INTEGER"
40 INPUT B
50 PRINT " SUM: "; A + B
60 PRINT "DIFFERENCE: "; A - B
70 PRINT " PRODUCT: "; A * B
80 PRINT " QUONTENT: "; INT(A / B)
90 PRINT " REMAINDER: "; A - INT(A / B ) * B
100 PRINT " POWER:"; A ^ B
110 END</syntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
<syntaxhighlight lang="qbasic">10 INPUT "Enter two integers separated by a comma: ";A, B
20 PRINT " Sum:"; A + B
30 PRINT "Difference:"; A - B
40 PRINT " Product:"; A * B
50 PRINT " Quontent:"; A \ B
60 PRINT " Remainder:"; A MOD B
70 PRINT " Power:"; A ^ B</syntaxhighlight>
 
==={{header|Quite BASIC}}===
<syntaxhighlight lang="qbasic">10 INPUT "enter a integer"; A
20 INPUT "enter another integer"; B
30 PRINT " Sum: "; A + B
40 PRINT "Difference: "; A - B
50 PRINT " Product: "; A * B
60 PRINT " Quontent: "; INT(A / B)
70 PRINT " Remainder: "; A - INT(A / B ) * B</syntaxhighlight>
 
==={{Header|Tiny BASIC}}===
<syntaxhighlight lang="Tiny BASIC"> LET A = 5
LET B = 3
PRINT "A = ", A, ", B = ", B
PRINT ""
PRINT A," + ",B," = ", A+B
PRINT A," - ",B," = ", A-B
PRINT A," * ",B," = ", A*B
PRINT A," / ",B," = ", A/B
PRINT A," % ",B," = ", A-(A/B)*B
REM Exponent calculation
LET X = 1
LET E = 0
10 IF X >= B THEN GOTO 30
LET T = E
IF E < A THEN LET E = A*A
IF T < A THEN GOTO 20
IF E >= A THEN LET E = E*A
20 LET X = X+1
GOTO 10
30 PRINT A," ^ ",B," = ", E
END</syntaxhighlight>
 
==={{header|True BASIC}}===
Line 1,005 ⟶ 1,081:
PRINT "POW(";a;", ";b;") is ";INT(a^b)
GET KEY done
END</syntaxhighlight>
END
 
</syntaxhighlight>
==={{header|QBasic}}===
{{works with|QuickBasic|4.5}}
Line 1,019 ⟶ 1,095:
 
Remainder sign matches: first operand
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "IntegerArithmetic"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
 
FUNCTION Entry ()
a$ = INLINE$("Enter integer A: ")
a = SLONG(a$)
b$ = INLINE$("Enter integer B: ")
b = SLONG(b$)
PRINT
PRINT " Sum:"; a + b
PRINT "Difference:"; a - b
PRINT " Product:"; a * b
PRINT " Quontent:"; a / b
PRINT " Remainder:"; a MOD b
PRINT " Power:"; a ** b
END FUNCTION
END PROGRAM</syntaxhighlight>
 
=={{header|BASIC256}}==
Line 1,477 ⟶ 1,575:
}</syntaxhighlight>
Division and modulus are defined as in C99.
 
=={{header|Dart}}==
<syntaxhighlight lang="dart">import 'dart:io';
import 'dart:math' show pow;
 
void main() {
print('enter a integer: ');
int a = int.parse(stdin.readLineSync());
print('enter another integer: ');
int b = int.parse(stdin.readLineSync());
 
print('a + b = ${a + b}');
print('a - b = ${a - b}');
print('a * b = ${a * b}');
print('a / b = ${a ~/ b}');
print('a % b = ${a % b}');
print('a ^ b = ${pow(a, b)}');
 
//Integer division uses the '~/' operator
}</syntaxhighlight>
 
=={{header|dc}}==
Line 1,672 ⟶ 1,790:
 
=={{header|Elena}}==
ELENA 46.x :
<syntaxhighlight lang="elena">import system'math;
import extensions;
Line 1,685 ⟶ 1,803:
console.printLine(a," * ",b," = ",a * b);
console.printLine(a," / ",b," = ",a / b); // truncates towards 0
console.printLine(a," % ",b," = ",a.mod:(b)); // matches sign of first operand
console.printLine(a," ^ ",b," = ",a ^ b);
}</syntaxhighlight>
Line 1,778 ⟶ 1,896:
Modulo: -1
Exponent: -0.0029154518950437317
</pre>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
^|EMal has no divmod operator or built-in function,
|its interface can be easily emulated as shown below.
|The performace is worse than using / and % operators.
|^
fun divmod = Pair by int dividend, int divisor
Pair result = int%int().named("quotient", "remainder")
result.quotient = dividend / divisor
result.remainder = dividend % divisor
return result
end
fun main = int by List args
int a, b
if args.length == 2
a = int!args[0]
b = int!args[1]
else
a = ask(int, "first number: ")
b = ask(int, "second number: ")
end
writeLine("sum: " + (a + b))
writeLine("difference: " + (a - b))
writeLine("product: " + (a * b))
writeLine("integer quotient: " + (a / b)) # truncates towards 0
writeLine("remainder: " + (a % b)) # matches sign of first operand
writeLine("exponentiation: " + (a ** b))
writeLine("logarithm: " + (a // b))
writeLine("divmod: " + divmod(a, b))
return 0
end
exit main(Runtime.args)
</syntaxhighlight>
{{out}}
<pre>
emal.exe Org\RosettaCode\AritmeticInteger.emal
first number: 19
second number: 7
sum: 26
difference: 12
product: 133
integer quotient: 2
remainder: 5
exponentiation: 893871739
logarithm: 2
divmod: [2,5]
</pre>
 
Line 2,303 ⟶ 2,469:
CloseStream(f);
end;</syntaxhighlight>
 
=={{header|GDScript}}==
Requires Godot 4.
 
<syntaxhighlight lang="gdscript">
@tool
extends Node
 
@export var a: int:
set(value):
a = value
refresh()
 
@export var b: int:
set(value):
b = value
refresh()
 
# Output properties
@export var sum: int
@export var difference: int
@export var product: int
@export var integer_quotient: int
@export var remainder: int
@export var exponentiation: int
@export var divmod: int
 
func refresh():
sum = a + b
difference = a - b
product = a * b
integer_quotient = a / b # Rounds towards 0
remainder = a % b # Matches the sign of a
exponentiation = pow(a, b)
</syntaxhighlight>
 
=={{header|Genie}}==
Line 2,416 ⟶ 2,617:
-5 mod 3 = 1
</pre>
 
=={{header|Golfscript}}==
Quotients round towards negative infinity. Remainders match the sign of the second operand.
<syntaxhighlight lang="golfscript">n/~~:b;~:a;a b+n a b-n a b*n a b/n a b%n a b?</syntaxhighlight>
 
=={{header|Groovy}}==
Line 2,812 ⟶ 3,017:
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scalakotlin">// version 1.1
import kotlin.math.pow // not an operator but in the standard library
 
fun main(args: Array<String>) {
val r = Regex("""-?\d+[ 0-9]+\s+-?\d[0-9]+""")
print("Enter two integers separated by space(s): ")
while(true) {
val input: String = readLine()!!.trim()
print("Enter two integers separated by space(s) or q to quit: ")
val input: Stringindex = readLine()!!input.trimlastIndexOf(' ')
val a = input.substring(0, index).trimEnd().toLong()
if (input == "q" || input == "Q") break
val b = input.substring(index if+ (!input1).matchestoLong(r)) {
println("$a + $b = ${a + b}")
println("Invalid input, try again")
println("$a - $b = ${a - continueb}")
println("$a * $b = ${a * b}")
println("$a / $b = ${a / b}") // rounds towards zero
val index = input.lastIndexOf(' ')
println("$a % $b = ${a % b}") // if non-zero, matches sign of first operand
val a = input.substring(0, index).trimEnd().toLong()
println("$a ^ val $b = input${a.substringtoDouble(index + 1).toLongpow(b.toDouble())}")
}
println("$a + $b = ${a + b}")
println("$a - $b = ${a - b}")
println("$a * $b = ${a * b}")
if (b != 0L) {
println("$a / $b = ${a / b}") // rounds towards zero
println("$a % $b = ${a % b}") // if non-zero, matches sign of first operand
}
else {
println("$a / $b = undefined")
println("$a % $b = undefined")
}
val d = Math.pow(a.toDouble(), b.toDouble())
print("$a ^ $b = ")
if (d % 1.0 == 0.0) {
if (d >= Long.MIN_VALUE.toDouble() && d <= Long.MAX_VALUE.toDouble())
println("${d.toLong()}")
else
println("out of range")
}
else if (!d.isFinite())
println("not finite")
else
println("not integral")
println()
}
}</syntaxhighlight>
 
{{out}}
<pre>
Enter two integers separated by space(s) or q to quit: 2 63
2 + 63 = 65
2 - 63 = -61
Line 2,862 ⟶ 3,044:
2 / 63 = 0
2 % 63 = 2
2 ^ 63 = 92233720368547758079.223372036854776E18
 
Enter two integers separated by space(s) or q to quit: -3 50
-3 + 50 = 47
-3 - 50 = -53
-3 * 50 = -150
-3 / 50 = 0
-3 % 50 = -3
-3 ^ 50 = out of range
 
Enter two integers separated by space(s) or q to quit: q
</pre>
 
Line 2,913 ⟶ 3,085:
math_pow(#a,#b) // 1296
math_pow(#b,#a) // 4096</syntaxhighlight>
 
=={{header|LDPL}}==
<syntaxhighlight lang="ldpl">data:
x is number
y is number
result is number
 
procedure:
display "Enter x: "
accept x
display "Enter y: "
accept y
add x and y in result
display "x + y = " result lf
subtract y from x in result
display "x - y = " result lf
multiply x by y in result
display "x * y = " result lf
divide x by y in result # There is no integer division but
floor result # floor rounds toward negative infinity
display "x / y = " result lf
modulo x by y in result
display "x % y = " result lf # Returns the sign of the 2nd argument
raise x to y in result
display "x ^ y = " result lf</syntaxhighlight>
{{out}}
<pre>
Enter x: 13
Enter y: 4
x + y = 17
x - y = 9
x * y = 52
x / y = 3
x % y = 1
x ^ y = 28561
</pre>
 
=={{header|LFE}}==
Line 3,358 ⟶ 3,566:
 
=={{header|min}}==
{{works with|min|0.1937.30}}
<syntaxhighlight lang="min">(concat dup'+ '-> '* prepend'div "$1 -> $2" swap % puts!'mod) :show
(("Enter an integer" ask integer) 2 times) quote-map =>
 
("Enter$1 an-> integer$2" askrollup int)concat 2dup times-> 'quote prepend %) prepend
('+ '- '* 'div 'mod) quote-map ('show"\n" concat) mapjoin cleaveputs!</syntaxhighlight>
{{out}}
<pre>
Line 3,711 ⟶ 3,919:
Pop $0
FunctionEnd</syntaxhighlight>
 
=={{header|Nu}}==
Division rounds towards -infinity. Modulus will match the sign of the first number.
 
<syntaxhighlight lang="nu">
input | parse "{a} {b}" | first | values | into int | do {|a b|
{
Sum: ($a + $b)
Difference: ($a - $b)
Product: ($a * $b)
Quotient: ($a // $b)
Remainder: ($a mod $b)
Exponent: ($a ** $b)
}
} $in.0 $in.1
</syntaxhighlight>
{{out}}
<pre>
-1 2
╭────────────┬────╮
│ Sum │ 1 │
│ Difference │ -3 │
│ Product │ -2 │
│ Quotient │ -1 │
│ Remainder │ -1 │
│ Exponent │ 1 │
╰────────────┴────╯
</pre>
 
=={{header|Nutt}}==
<syntaxhighlight lang="Nutt">
module main
imports native.io{input.hear,output.say}
 
vals a=hear(Numerable),b=hear(Numerable)
say("a+b="+(a+b))
say("a-b="+(a-b))
say("a*b="+(a*b))
say("a//b="+(a//b))
say("a%b="+(a%b))
say("a^b="+(a^b))
 
end
</syntaxhighlight>
 
=={{header|Oberon-2}}==
Line 4,777 ⟶ 5,029:
[ "Exponentiation: ('a'^'b')"
</syntaxhighlight>
 
=={{header|RPL}}==
≪ → a b
≪ "a + b = " a b + →STR +
"a - b = " a b - →STR +
"a * b = " a b * →STR +
"a / b = " a b / →STR +
"a % b = " a b / LAST ROT * - →STR +
"a ^ b = " a B→R b B→R ^ R→B →STR +
≫ ≫ '<span style="color:blue">SHOWA</span>' STO
 
#14 #3 <span style="color:blue">SHOWA</span>
<pre>
6: "a + b = # 17d"
5: "a - b = # 11d"
4: "a * b = # 42d"
3: "a / b = # 4d"
2: "a % b = # 2d"
1: "a ^ b = # 2744d"
</pre>
 
=={{header|Ruby}}==
Line 4,790 ⟶ 5,062:
"Quotient: #{x.fdiv(y)}", # float
"Remainder: #{x%y}", # same sign as second operand
"Exponentiation: #{x**y}"</syntaxhighlight>,
"Quotient: %d with Remainder: %d" % x.divmod(y)</syntaxhighlight>
 
=={{header|Run BASIC}}==
Line 4,969 ⟶ 5,242:
 
] applyTo: {Integer readFrom: (query: 'Enter a: '). Integer readFrom: (query: 'Enter b: ')}.</syntaxhighlight>
 
=={{header|SmallBASIC}}==
<syntaxhighlight lang="SmallBASIC">
input "Enter first number : "; A
input "Enter second number: "; B
 
print "Sum : "; A + B
print "Difference: "; A - B
print "Product : "; A * B
print "Quotient : "; A \ B ' Integer quotient rounds towards smaller number
print "Remainder : "; A % B ' sign of remainder is given by sign of first operand
print "Power : "; A ^ B
</syntaxhighlight>
 
=={{header|Smalltalk}}==
Line 5,595 ⟶ 5,881:
End Module</syntaxhighlight>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="go">// Arithmetic-integer in V (Vlang)
// Tectonics: v run arithmetic-integer.v
module main
Line 5,653 ⟶ 5,939:
 
The sign of the remainder operator '%' matches the sign of the first operand.
<syntaxhighlight lang="ecmascriptwren">import "io" for Stdin, Stdout
System.write("first number: ")
Stdout.flush()
Anonymous user