PMath (Perfect and Precise Math)

Search:
Group by:

Introduction

PMath is meant to be a library that resolves the inaccuracies of normal float math. PMath does not use any floats, and retains the most accurate format of a given number.

Remember:

  • PMath is focused on accuracy, not speed or size
  • All numbers must be converted to PNum before they are used, and all PNum numbers are accurate
  • Currently, to do certain actions, like add radicals, you will have to convert to float. You will loose accuracy becuase you are using float math.

Module Division

  • PMath (Main Module): Imports and exports basic and types
  • Basic: Basic math ops, eg. add, subtract, multiply, divide...
  • Types: Automatically imported, holds the PNum type and it's utils, eg. simplify, copyToRef, newPNum, $
  • Expanded (Coming Soon): Other math functions like log, cos, sin, tan, etc.
  • Constants: Contains math constants like i and PI.

Support

FeatureRadical SupportFraction SupportFuture SupportNotes
Radicalsβœ… (All)
Fractionsβœ… (All)
Additive Groupsβœ…Goes with irrational numbers and adds support for adding and subtracting radicals.
Irrational Numbersβœ…E, PI, etc.
BigInt Supportβœ… (All)
Imaginary Numbersβœ… (All)
Additionβœ…
Subtraction βœ…
Multiplicationβœ…βœ…
Division βœ…βœ…
Exponentsβœ…βœ…
Rootsβœ…βœ…
Equals Operatorβœ…βœ…
Comparison Operatorsβœ…βœ…
Shortcutsβœ…βœ…βœ…+=, *=, inc, etc.

Examples

Examples of the PMath library.

Basic Example

import pmath

## `!!` means that all math inside of the parentheses is PMath

echo !!(((1 / 3) + (5 / 4)) / 2 == (1 / 24) + (18 / 24))

echo !!(sqrt(5) * sqrt(3) == sqrt(15))

echo !!((1 / 4) * 3 - 5)

BigInt Examples (Calculating PI)

Please enable the -d:pmathBigInts flag to use bigints.

import pmath

## Leibniz’s formula

var pi = !0
var k = !1

for i in !!(0..(10 ^ 3)):
  
  if i mod 2 == 0:
    pi += !!(4 / k)
  else:
    pi -= !!(4 / k)
  
  k += 2

echo pi

proc readableFloat(pn: PNum): BiggestFloat =
  return BiggestFloat(pn * (10 ^ 8)) / BiggestFloat(10 ^ 8)

echo readableFloat(pi * 8) # Circumference of a circle with a diameter of 8. Should ~= 25

Accuracy Example

import pmath

var meters = 0.0
for _ in 1..100000000:
  meters += 0.01

echo meters
echo meters == 1000000 # false


let change = newPNum(1, 100) # Declared as let for speed
var meters2 = !0

for _ in 1..100000000:
  meters2 += change

echo meters2
echo meters2 == !1000000 # true