Kotlin Math Operations and Functions Overview
1. Basic Arithmetic Operations
Kotlin supports standard arithmetic operations like addition, subtraction, multiplication, division, and modulus.
val a = 10
val b = 3
println(a + b) // Addition: 13
println(a - b) // Subtraction: 7
println(a * b) // Multiplication: 30
println(a / b) // Division: 3 (integer division)
println(a % b) // Modulus: 1 (remainder)For floating-point division, ensure at least one operand is a Double or Float:
println(a.toDouble() / b) // 3.33333333333333352. Math Functions
Kotlin uses Java's Math class for advanced mathematical operations. You can access functions like sqrt, pow, abs, sin, cos, etc.
import kotlin.math.*
val number = 25.0
println(sqrt(number)) // Square root: 5.0
println(2.0.pow(3.0)) // Power: 8.0
println(abs(-10)) // Absolute value: 10
println(sin(PI / 2)) // Sine of 90 degrees: 1.03. Constants
Kotlin provides mathematical constants like PI and E in the kotlin.math package.
import kotlin.math.PI
import kotlin.math.E
println(PI) // 3.141592653589793
println(E) // 2.7182818284590454. Random Numbers
You can generate random numbers using Kotlin's Random class or extension functions.
import kotlin.random.Random
val randomInt = Random.nextInt(1, 100) // Random integer between 1 and 99
val randomDouble = Random.nextDouble() // Random double between 0.0 and 1.0
println(randomInt)
println(randomDouble)5. Rounding Numbers
Kotlin provides functions to round numbers to the nearest integer or to a specific number of decimal places.
val num = 3.14159
println(num.roundToInt()) // Rounds to nearest integer: 3
println("%.2f".format(num)) // Formats to 2 decimal places: 3.146. Bitwise Operations
Kotlin supports bitwise operations for integers:
val x = 0b1010 // Binary 10
val y = 0b1100 // Binary 12
println(x and y) // Bitwise AND: 8 (0b1000)
println(x or y) // Bitwise OR: 14 (0b1110)
println(x xor y) // Bitwise XOR: 6 (0b0110)
println(x.inv()) // Bitwise inversion: -11 (0b11110101 in 8-bit)
println(x shl 1) // Left shift: 20 (0b10100)
println(x shr 1) // Right shift: 5 (0b0101)7. BigInteger and BigDecimal
For very large numbers or precise decimal calculations, Kotlin supports BigInteger and BigDecimal from Java's standard library.
import java.math.BigInteger
import java.math.BigDecimal
val bigInt = BigInteger("12345678901234567890")
val bigDec = BigDecimal("123.4567890123456789")
println(bigInt + BigInteger.ONE) // 12345678901234567891
println(bigDec * BigDecimal.TEN) // 1234.5678901234567898. Custom Mathematical Functions
You can define your own mathematical functions in Kotlin.
fun factorial(n: Int): Int {
return if (n <= 1) 1 else n * factorial(n - 1)
}
println(factorial(5)) // 1209. Using Kotlin's Standard Library
Kotlin's standard library provides utility functions for working with numbers, such as coerceIn, inRange, and takeIf.
val value = 15
println(value.coerceIn(1, 10)) // Limits value to range: 1010. Working with Collections
Kotlin provides functions for mathematical operations on collections, such as sum, average, min, and max.
val numbers = listOf(1, 2, 3, 4, 5)
println(numbers.sum()) // 15
println(numbers.average()) // 3.0
println(numbers.minOrNull()) // 1
println(numbers.maxOrNull()) // 5Kotlin's flexibility and interoperability with Java make it a powerful language for mathematical computations. Whether you're working on simple arithmetic or complex algorithms, Kotlin has the tools you need!
Kotlin, a statically typed programming language, is widely used for Android development and other JVM-based applications. It provides robust support for mathematical operations, leveraging both standard library functions and Java's mathematical capabilities. Here's an overview of how to perform common mathematical operations in Kotlin:
1. Basic Arithmetic Operations
Kotlin supports standard arithmetic operations like addition, subtraction, multiplication, division, and modulus.
val a = 10
val b = 3
println(a + b) // Addition: 13
println(a - b) // Subtraction: 7
println(a * b) // Multiplication: 30
println(a / b) // Division: 3 (integer division)
println(a % b) // Modulus: 1 (remainder)For floating-point division, ensure at least one operand is a Double or Float:
println(a.toDouble() / b) // 3.33333333333333352. Math Functions
Kotlin uses Java's Math class for advanced mathematical operations. You can access functions like sqrt, pow, abs, sin, cos, etc.
import kotlin.math.*
val number = 25.0
println(sqrt(number)) // Square root: 5.0
println(2.0.pow(3.0)) // Power: 8.0
println(abs(-10)) // Absolute value: 10
println(sin(PI / 2)) // Sine of 90 degrees: 1.03. Constants
Kotlin provides mathematical constants like PI and E in the kotlin.math package.
import kotlin.math.PI
import kotlin.math.E
println(PI) // 3.141592653589793
println(E) // 2.7182818284590454. Random Numbers
You can generate random numbers using Kotlin's Random class or extension functions.
import kotlin.random.Random
val randomInt = Random.nextInt(1, 100) // Random integer between 1 and 99
val randomDouble = Random.nextDouble() // Random double between 0.0 and 1.0
println(randomInt)
println(randomDouble)5. Rounding Numbers
Kotlin provides functions to round numbers to the nearest integer or to a specific number of decimal places.
val num = 3.14159
println(num.roundToInt()) // Rounds to nearest integer: 3
println("%.2f".format(num)) // Formats to 2 decimal places: 3.146. Bitwise Operations
Kotlin supports bitwise operations for integers:
val x = 0b1010 // Binary 10
val y = 0b1100 // Binary 12
println(x and y) // Bitwise AND: 8 (0b1000)
println(x or y) // Bitwise OR: 14 (0b1110)
println(x xor y) // Bitwise XOR: 6 (0b0110)
println(x.inv()) // Bitwise inversion: -11 (0b11110101 in 8-bit)
println(x shl 1) // Left shift: 20 (0b10100)
println(x shr 1) // Right shift: 5 (0b0101)7. BigInteger and BigDecimal
For very large numbers or precise decimal calculations, Kotlin supports BigInteger and BigDecimal from Java's standard library.
import java.math.BigInteger
import java.math.BigDecimal
val bigInt = BigInteger("12345678901234567890")
val bigDec = BigDecimal("123.4567890123456789")
println(bigInt + BigInteger.ONE) // 12345678901234567891
println(bigDec * BigDecimal.TEN) // 1234.5678901234567898. Custom Mathematical Functions
You can define your own mathematical functions in Kotlin.
fun factorial(n: Int): Int {
return if (n <= 1) 1 else n * factorial(n - 1)
}
println(factorial(5)) // 1209. Using Kotlin's Standard Library
Kotlin's standard library provides utility functions for working with numbers, such as coerceIn, inRange, and takeIf.
val value = 15
println(value.coerceIn(1, 10)) // Limits value to range: 1010. Working with Collections
Kotlin provides functions for mathematical operations on collections, such as sum, average, min, and max.
val numbers = listOf(1, 2, 3, 4, 5)
println(numbers.sum()) // 15
println(numbers.average()) // 3.0
println(numbers.minOrNull()) // 1
println(numbers.maxOrNull()) // 5Kotlin's flexibility and interoperability with Java make it a powerful language for mathematical computations. Whether you're working on simple arithmetic or complex algorithms, Kotlin has the tools you need!
Comments
Post a Comment