math-problems v0.2.0
math-problems
Solve math problems easily with this utility library. Includes temperature conversions, geometric area calculations, number utilities, and population statistics.
Installation
npm install math-problems
Usage
import {
area,
toCelsius,
isEven,
populationDensity,
population,
} from 'math-problems';
// Calculate the area of a circle with radius 5
console.log(area.circle(5)); // 78
// Convert 73°F to Celsius
console.log(toCelsius(73)); // 22.777... (repeating decimal)
// Check if a number is even
console.log(isEven(4)); // true
// Calculate population density
console.log(populationDensity(11452000, 1483)); // 7724
// Access population data
console.log(population.br.sp); // 11452000
API Documentation
š Area Calculations
All area functions return an object with a floored area
value.
Rectangle
rect(base: number, height: number)
area.rect(4, 5) ā 20
Triangle
triangle(base: number, height: number)
area.triangle(4, 5) ā 10
Rhombus
rhombus(D: number, d: number)
area.rhombus(8, 6) ā 24
Trapezoid
trapezoid(B: number, b: number, height: number)
area.trapezoid(10, 6, 5) ā 40
Circle
circle(radius: number)
area.circle(3) ā 28
š”ļø Temperature Conversions
Convert between Celsius (°C), Fahrenheit (°F), and Kelvin (K).
toCelsius
toCelsius(temperature: number, unit?: 'F' | 'K')
Default unit: FahrenheittoCelsius(32) ā 0
toCelsius(273.15, 'K') ā 0
toFahrenheit
toFahrenheit(temperature: number, unit?: 'C' | 'K')
Default unit: CelsiustoFahrenheit(0) ā 32
toFahrenheit(273.15, 'K') ā 32
toKelvin
toKelvin(temperature: number, unit?: 'C' | 'F')
Default unit: CelsiustoKelvin(0) ā 273
toKelvin(32, 'F') ā 273
š¢ Number Utilities
isEven(number)
Returnstrue
if the number is even.isOdd(number)
Returnstrue
if the number is odd.difference(x, y)
Returns the floored difference between two numbers.difference(10, 3.8) ā 6
PI
Math.PI constant (ā3.14159).
š Interpoolation and regression
- interpolation(input: { function: (number) => number, value: number, points: number[] })
Interpolates a set of data points from a function and value through Newton Interpolation.
interpolation({
function: Math.log,
value: 2,
points: [1, 4, 6]
})
ā 0.5658443469009827
- regression(input: number[])
Computes the linear and polynomial regression from a set of data points.
regression([
[-2, -1],
[1, 2],
[4, 59],
[-1, 4],
[3, 24],
[-4, -53]
]);
ā {
linear: {
m: 10.97864768683274,
b: 4.00355871886121
},
polynomial: {
a: 6.689189189189188,
b: 11.060810810810809,
c: -0.34459459459459435
}
}
š Population Statistics
populationDensity(population, area)
Returns density (people per unit area).birthRate(births, population)
Returns births per 1000 people.deathRate(deaths, population)
Returns deaths per 1000 people.Population Data
Pre-defined population figures (as of Feb 2025):population.br.sp; // Brazil SĆ£o Paulo: 11,452,000 population.us.ny; // USA New York: 20,201,249
Contributing
Contributions are welcome! Please follow the contribution guidelines.
- Clone the repository:
git clone https://github.com/igorwastaken/math-utils.git
- Install dependencies:
npm install
- Run tests:
npm test
License
MIT Ā© Igor Figueiredo
Full License Text