Arithmetic/Integer: Difference between revisions
Content added Content deleted
(→{{header|Ruby}}: added divmod) |
(→{{header|Kotlin}}: Removed error handling (as specified by the task) and made the code use the Kotlin standard library instead of the Java standard library.) |
||
Line 2,816: | Line 2,816: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
<syntaxhighlight lang=" |
<syntaxhighlight lang="kotlin"> |
||
import kotlin.math.pow // not an operator but in the standard library |
|||
fun main( |
fun main() { |
||
val r = Regex("""-? |
val r = Regex("""-?[0-9]+\s+-?[0-9]+""") |
||
⚫ | |||
while(true) { |
|||
val input: String = readLine()!!.trim() |
|||
⚫ | |||
val index = input.lastIndexOf(' ') |
|||
⚫ | |||
if (input == "q" || input == "Q") break |
|||
val b = input.substring(index + 1).toLong() |
|||
⚫ | |||
println("Invalid input, try again") |
|||
println("$a - $b = ${a - b}") |
|||
} |
println("$a * $b = ${a * b}") |
||
⚫ | |||
val index = input.lastIndexOf(' ') |
|||
⚫ | |||
⚫ | |||
println("$a ^ $b = ${a.toDouble().pow(b.toDouble())}") |
|||
} |
|||
⚫ | |||
println("$a - $b = ${a - b}") |
|||
println("$a * $b = ${a * b}") |
|||
if (b != 0L) { |
|||
⚫ | |||
⚫ | |||
} |
|||
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> |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Enter two integers separated by space(s) |
Enter two integers separated by space(s): 2 63 |
||
2 + 63 = 65 |
2 + 63 = 65 |
||
2 - 63 = -61 |
2 - 63 = -61 |
||
Line 2,866: | Line 2,843: | ||
2 / 63 = 0 |
2 / 63 = 0 |
||
2 % 63 = 2 |
2 % 63 = 2 |
||
2 ^ 63 = |
2 ^ 63 = 9.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> |
</pre> |
||