Kotlin kotlin.math.hypot
In Kotlin, kotlin.math.hypot computes the length of a right triangle’s hypotenuse from its two legs. Legs of 3 and 4 give a length of 5.
A simple example
kotlin.math.hypot
- Start with Legs 3 and 4
- Do this Combine perpendicular lengths
- Result 5
fun main() {
println(kotlin.math.hypot(3.0, 4.0))
}
5.0
Syntax
kotlin.math.hypot(width, height)
Run these examples in Kotlin
Save one example at a time as Lesson.kt. Use Kotlin 2.2 on JDK 21.
kotlinc Lesson.kt -include-runtime -d lesson.jar
java -jar lesson.jarMeasure the diagonal of a screen
A designer knows the horizontal and vertical distances between two points. Calculate the direct diagonal distance so the measurement can be used without tracing two separate legs.
fun main() {
val width = 3.0
val height = 4.0
System.out.printf(java.util.Locale.ROOT, "%.3f%n", (kotlin.math.hypot(width, height)).toDouble())
}
5.000
A screen three units wide and four high has a five-unit diagonal.
Estimate a direct walk across campus
A student crosses a courtyard instead of following two perpendicular paths around it. Calculate the direct walking distance from the horizontal and vertical distances shown on the campus map.
fun main() {
val east = 6.0
val north = 8.0
System.out.printf(java.util.Locale.ROOT, "%.3f%n", (kotlin.math.hypot(east, north)).toDouble())
}
10.000
Six blocks east and eight north form perpendicular sides. Their direct diagonal measures ten blocks.
Measure an exhibit’s diagonal cable route
An exhibition plan shows a socket 5 m east and 12 m north of a screen. A drafter marks the straight-line separation on the plan.
The perpendicular offsets form the legs of a right triangle.
What to watch for
Both inputs must use the same unit. A diagonal is a length, not an area.