Home > Web Front-end > JS Tutorial > body text

Find the hypotenuse of a number in TypeScript

WBOY
Release: 2023-09-07 11:53:13
forward
1122 people have browsed it

在 TypeScript 中查找数字的斜边

The longest side of a right triangle and the side facing away from the right angle are called the hypotenuse. The Pythagorean theorem explains that the square of the hypotenuse is equal to the sum of the squares of the other two sides. We can use this theorem to determine it. The formula for this theorem is c2 = a2 b2, where c represents the hypotenuse and a and b are the two sides of the triangle. The Pythagorean theorem allows you to quickly determine the value of the hypotenuse when the lengths of the other two sides of the triangle are known. First, we need to take the sum of the squares of the other two sides to get the hypotenuse.

The Pythagorean Theorem can be used to calculate the hypotenuse in TypeScript by writing a function that accepts the length of the two shorter sides as arguments. As a result, the function returns the hypotenuse. There is a condition for applying this theorem and finding the hypotenuse. The triangle must be a right triangle for this function to work, so one of the angles must be right (90 degrees). If the triangle is not a right triangle, you cannot apply Pythagoras' theorem to determine the hypotenuse. We will describe the functionality of typescript through two examples.

grammar

This function can be defined as follows -

function hypotenuse(a: number, b: number): number {
   return Math.sqrt(a * a + b * b);
}
Copy after login

This function has two parameters, a and b, representing the lengths of the two short sides of the triangle. It then calculates the square of the hypotenuse by adding the squares of a and b, and finally returns the square root of that sum.

It is important to note that this function assumes that the triangle is a right triangle, which means that one of the angles is a right angle (90 degrees). If the triangle is not a right triangle, you cannot use Pythagoras' theorem to find the hypotenuse.

Example

In this example, we will find the hypotenuse of a number in TypeScript. The following steps are required and explanations are also given below -

step

  • We first define a function called hypotenuse, which accepts two parameters a and b, which represent the lengths of the two short sides of the triangle respectively. This function uses the Pythagorean theorem to calculate the square of the hypotenuse by adding the squares of a and b, and then returns the square root of that sum using the Math.sqrt() method in TypeScript.

  • Then we define two variables side1 and side2, which are the two short sides of the triangle. These values ​​are specified as 3 and 4 respectively.

  • We then call the hypotenuse function by passing side1 and side2 as parameters and store the result in the variable hypotenuseValue.

  • Finally, we use the console.log() method to print the results in the console.

function hypotenuse(a: number, b: number): number {
  return Math.sqrt(a * a + b * b)
}

let side1: number = 3
let side2: number = 4

let hypotenuseValue: number = hypotenuse(side1, side2)
console.log(
  `The hypotenuse of the triangle with sides ${side1} and ${side2} is ${hypotenuseValue}.`
)
Copy after login

When compiled, it will generate the following JavaScript code -

function hypotenuse(a, b) {
   return Math.sqrt(a * a + b * b);
}
var side1 = 3;
var side2 = 4;
var hypotenuseValue = hypotenuse(side1, side2);
console.log("The hypotenuse of the triangle with sides " + side1 + " and " + side2 + " is " + hypotenuseValue + ".");
Copy after login

Output

The above code will produce the following output -

The hypotenuse of the triangle with sides 3 and 4 is 5.
Copy after login

Example

In this example, we will use the Math.pow and Math.sqrt methods in TypeScript to find the hypotenuse of a number. The following steps are required and explanations are also given below -

step

  • We created a function called findHypotenuse, which accepts two parameters a and b, which represent the lengths of the two short sides of the triangle.

  • Inside the function, we use the Math.pow(base, exponent) method to square the values ​​​​of a and b, and then use the Math.sqrt() method to find the square root of the sum of the values ​​​​a and b, a and b. squared. This will give us the hypotenuse of the triangle.

  • Then we define two variables, side A and side B, which are the two shorter sides of the triangle. These values ​​are specified as 5 and 12 respectively.

  • We then call the findHypotenuse function by passing side A and side B as arguments and store the result in the variable hypotenuse.

function findHypotenuse(a: number, b: number): number {
   return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2))
}

let sideA: number = 5
let sideB: number = 12

let hypotenuse: number = findHypotenuse(sideA, sideB)
console.log(
   `The hypotenuse of the triangle with sides ${sideA} and ${sideB} is ${hypotenuse}.`
)
Copy after login

When compiled, it will generate the following JavaScript code -

function findHypotenuse(a, b) {
   return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
}
var sideA = 5;
var sideB = 12;

var hypotenuse = findHypotenuse(sideA, sideB);
console.log("The hypotenuse of the triangle with sides " + sideA + " and " + sideB + " is " + hypotenuse + ".");
Copy after login

Output

The above code will produce the following output -

The hypotenuse of the triangle with sides 5 and 12 is 13.
Copy after login

Using TypeScript, we can even perform more mathematical calculations efficiently. Finding the hypotenuse is one of them. Plus, the results are fast and accurate.

The above is the detailed content of Find the hypotenuse of a number in TypeScript. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!