> For the complete documentation index, see [llms.txt](https://educacion.gitbook.io/programacion/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://educacion.gitbook.io/programacion/ud1-introduccion-a-programacion/execucion-secuencial/a-libreria-math.md).

# A librería Math

A clase Math en Java é unha colección de métodos estáticos predefinidos que permiten realizar operacións matemáticas e trigonométricas, como obter o valor absoluto, calcular o seo ou o coseno, atopar a raíz cadrada ou xerar números aleatorios, entre outros cálculos complexos. Ao ser estática, non é necesario crear un obxecto de Math para usar as súas funcións; vanse chamar directamente usando Math.nombreMetodo().

{% hint style="warning" %}
**Documentación oficial:** <https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html>
{% endhint %}

* **Valor absoluto**

```java
Math.abs(-5)    // → 5
Math.abs(3.14)  // → 3.14
```

* **Potencia**

```java
Math.pow(2, 3)    // → 8.0
Math.pow(5, 2)    // → 25.0
```

* **Raíz cadrada**

```java
Math.sqrt(16)     // → 4.0
Math.sqrt(2)      // → 1.4142...
```

* **Logaritmo**

```java
Math.log(Math.E)  // → 1.0
```

* **Máximo e mínimo**

```java
Math.max(8, 12)  // → 12
Math.min(5, 2)   // → 2
```

* **Redondeo o enteiro mais próximo**

```java
Math.round(3.4)  // → 3
Math.round(3.6)  // → 4
Math.round(3.5)  // → 4
```

* **Redondeo cara arriba**

```java
Math.ceil(3.1)   // → 4.0
Math.ceil(3.9)   // → 4.0
```

* **Redondeo cara abaixo**

```java
Math.floor(3.9)  // → 3.0
Math.floor(3.1)  // → 3.0
```

* **Funcions trigonométicas**

```java
Math.sin(Math.PI/2)  // → 1.0
Math.cos(Math.PI)    // → -1.0
```

* **Números aleatorios:** Devolve un número entre 0.0 e 0.999... Por isto e necesario multiplicar por N+1 se queremos obter o número N. (Ex: 0.999 \* 10 = 9.999... o trucar a int queda co 9, polo que se queremos obter o número 10 hay que multiplicar por 11 ⇒ 0.999\*11 = 10.989, truncado da 10).

```javascript
// Xerar número entre 0 e 10
int numero = (int)(Math.random() * 11);

// Xerar número entre 1 e 6 (dado)
int dado = (int)(Math.random() * 6) + 1;
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://educacion.gitbook.io/programacion/ud1-introduccion-a-programacion/execucion-secuencial/a-libreria-math.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
