SPECTRALANG_

// DOCS NAVIGATION

std.math — Matemática_

SOURCE: content/docs/reference/05-stdlib.md05 Stdlib / 3. std.math — Matemática / Mathematics

import std.math
// ou / or
import std.math as math

Funções Inteiras / Integer Functions

abs(x: int) -> int

let v = std.math.abs(-42)
     // 42
let v2 = std.math.abs(10)
     // 10

min(lhs: int, rhs: int) -> int

let menor = std.math.min(3, 7)
    // 3

max(lhs: int, rhs: int) -> int

let maior = std.math.max(3, 7)
    // 7

clamp(n: int, min: int, max: int) -> int

PT-BR: Restringe n ao intervalo [min, max].
EN-US: Restricts n to the range [min, max].

let v = std.math.clamp(150, 0, 100)
   // 100
let v2 = std.math.clamp(-5, 0, 100)
   // 0
let v3 = std.math.clamp(50, 0, 100)
   // 50

sign(n: int) -> int

PT-BR: Retorna -1, 0 ou 1.
EN-US: Returns -1, 0, or 1.

let s1 = std.math.sign(-5)
   // -1
let s2 = std.math.sign(0)
    // 0
let s3 = std.math.sign(10)
   // 1

gcd(a: int, b: int) -> int

PT-BR: Máximo divisor comum.
EN-US: Greatest common divisor.

let g = std.math.gcd(12, 8)
    // 4

lcm(a: int, b: int) -> int

PT-BR: Mínimo múltiplo comum.
EN-US: Least common multiple.

let l = std.math.lcm(4, 6)
    // 12

Funções de Ponto Flutuante / Float Functions

abs_f(x: float) -> float

let v = std.math.abs_f(-3.14)
   // 3.14

sqrt_f(x: float) -> float

let r = std.math.sqrt_f(16.0)
   // 4.0
let r2 = std.math.sqrt_f(2.0)
   // ~1.4142

pow_f(base: float, exp: float) -> float

let p = std.math.pow_f(2.0, 10.0)
   // 1024.0
let p2 = std.math.pow_f(3.0, 0.5)
   // ~1.732 (raiz / sqrt)

floor_f(x: float) -> float

let f = std.math.floor_f(3.7)
    // 3.0
let f2 = std.math.floor_f(-1.2)
  // -2.0

ceil_f(x: float) -> float

let c = std.math.ceil_f(3.2)
     // 4.0
let c2 = std.math.ceil_f(-1.8)
   // -1.0

round_f(x: float) -> float

let r = std.math.round_f(3.5)
    // 4.0
let r2 = std.math.round_f(3.4)
   // 3.0

sin_f(x: float) -> float / cos_f(x: float) -> float / tan_f(x: float) -> float

PT-BR: Funções trigonométricas. x em radianos.
EN-US: Trigonometric functions. x in radians.

import std.math as m

let pi = m.pi()
let seno  = m.sin_f(pi / 2.0)
    // ~1.0
let coss  = m.cos_f(0.0)
          // 1.0
let tang  = m.tan_f(pi / 4.0)
    // ~1.0

log_f(x: float) -> float

PT-BR: Logaritmo natural (base e).
EN-US: Natural logarithm (base e).

let ln_e = std.math.log_f(2.71828)
   // ~1.0

log2_f(x: float) -> float / log10_f(x: float) -> float

let l2  = std.math.log2_f(8.0)
     // 3.0
let l10 = std.math.log10_f(1000.0)
 // 3.0

atan2_f(y: float, x: float) -> float

PT-BR: Arco-tangente de y/x, considerando o quadrante.
EN-US: Arc-tangent of y/x, considering the quadrant.

let angulo = std.math.atan2_f(1.0, 1.0)
   // pi/4 (~0.785)

is_nan_f(x: float) -> bool / is_infinite_f(x: float) -> bool

let nan_check = std.math.is_nan_f(0.0 / 0.0)
        // true (comportamento impl-defined)
let inf_check = std.math.is_infinite_f(1.0 / 0.0)
   // true

Constantes / Constants

pi() -> float

let pi = std.math.pi()
    // ~3.14159265358979

e_const() -> float

let e = std.math.e_const()
    // ~2.71828182845905

Exemplo Completo / Complete Example

module matematica

import std.math as m
from std.io import println

public func main() {
    let pi = m.pi()
    let raio = 5.0
    let area = pi * m.pow_f(raio, 2.0)
    let circunferencia = 2.0 * pi * raio

    println(f"Raio: {raio}")
    println(f"Área: {area}")
    println(f"Circunferência: {circunferencia}")

    // Teorema de Pitágoras / Pythagorean theorem
    let a = 3.0
    let b = 4.0
    let hipotenusa = m.sqrt_f(m.pow_f(a, 2.0) + m.pow_f(b, 2.0))
    println(f"Hipotenusa: {hipotenusa}")
    // 5.0
}