Skip to content

Commit

Permalink
Add number::sqrt
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed May 12, 2024
1 parent 1fd202c commit 422ca2a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/global.navi
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ intrinsic def number::exp(a: number): 0..inf | nan;
intrinsic def number::log(a: number): number;
intrinsic def number::pow(a: number, b: number): number;

// Returns the square root of a number. Negative numbers return `never`.
def number::sqrt(x: number): 0..inf | nan {
number::pow(x & (0..inf | nan), 0.5)
}

let number::PI = 3.141592653589793;
let number::E = 2.718281828459045;

Expand Down
69 changes: 69 additions & 0 deletions tests/__snapshots__/evaluate.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23264,6 +23264,75 @@ number::sin(never) => never
number::sin(any) => Error: Incompatible argument type: The supplied argument type any is not compatible with the definition type."
`;

exports[`Builtin functions number::sqrt evaluate 1`] = `
"number::sqrt(number) => 0..inf | nan
number::sqrt(-3.14) => never
number::sqrt(-2) => never
number::sqrt(-1) => never
number::sqrt(-0.5) => never
number::sqrt(0) => 0
number::sqrt(0.5) => 0.7071067811865475
number::sqrt(1) => 1
number::sqrt(2) => 1.414213562373095
number::sqrt(2.78) => 1.6673332000533065
number::sqrt(3) => 1.7320508075688772
number::sqrt(3.14) => 1.772004514666935
number::sqrt(10) => 3.16227766016838
number::sqrt(100) => 10
number::sqrt(-inf) => never
number::sqrt(inf) => inf
number::sqrt(nan) => nan
number::sqrt(0..1) => 0..1
number::sqrt(0!..1) => 0..1
number::sqrt(0..!1) => 0..1
number::sqrt(0..2) => 0..1.414213562373095
number::sqrt(0!..!2) => 0..1.414213562373095
number::sqrt(0.5..1.5) => 0.7071067811865475..1.224744871391589
number::sqrt(0.5..2.5) => 0.7071067811865475..1.5811388300841898
number::sqrt(0.5!..2.5) => 0.7071067811865475..1.5811388300841898
number::sqrt(0.5..!2.5) => 0.7071067811865475..1.5811388300841898
number::sqrt(-2.56..3.35) => 0..1.8303005217723127
number::sqrt(0.5..0.75) => 0.7071067811865475..0.8660254037844386
number::sqrt(1..2) => 1..1.414213562373095
number::sqrt(0..inf) => 0..inf
number::sqrt(0!..inf) => 0..inf
number::sqrt(0..!inf) => 0..inf
number::sqrt(0!..!inf) => 0..inf
number::sqrt(1..inf) => 1..inf
number::sqrt(1!..inf) => 1..inf
number::sqrt(1..!inf) => 1..inf
number::sqrt(2..inf) => 1.414213562373095..inf
number::sqrt(2!..inf) => 1.414213562373095..inf
number::sqrt(2.5..inf) => 1.5811388300841898..inf
number::sqrt(-inf..0) => 0
number::sqrt(-inf..inf) => 0..inf
number::sqrt(-inf!..inf) => 0..inf
number::sqrt(-inf..!inf) => 0..inf
number::sqrt(-inf!..!inf) => 0..inf
number::sqrt(int(0..1)) => int(0..1)
number::sqrt(int(0..2)) => int(0..1) | 1.414213562373095
number::sqrt(int(1..2)) => 1 | 1.414213562373095
number::sqrt(int(5..20)) => 2.23606797749979..4.472135954999579
number::sqrt(int(-20..-5)) => never
number::sqrt(int(-10..10)) => int(0..3) | 1.414213562373095 | 1.7320508075688772 | 2.23606797749979 | 2.449489742783178 | 2.6457513110645907 | 2.82842712474619 | 3.16227766016838
number::sqrt(int(0..inf)) => int(0..1) | 1.414213562373095..inf
number::sqrt(int(1..inf)) => 1 | 1.414213562373095..inf
number::sqrt(int(-inf..0)) => 0
number::sqrt(int(-inf..1)) => int(0..1)
number::sqrt(int(-inf..inf)) => int(0..1) | 1.414213562373095..inf
number::sqrt(nonInt(0..1)) => 0..1
number::sqrt(nonInt(0..2)) => 0..1.414213562373095
number::sqrt(nonInt(-1..2)) => 0..1.414213562373095
number::sqrt(nonInt(0..inf)) => 0..inf
number::sqrt(nonInt(-1..inf)) => 0..inf
number::sqrt(nonInt(-inf..0)) => never
number::sqrt(nonInt(-inf..1)) => 0..1
number::sqrt(nonInt(-inf..inf)) => 0..inf
number::sqrt(1 | int(3..4)) => int(1..2) | 1.7320508075688772
number::sqrt(never) => never
number::sqrt(any) => Error: Incompatible argument type: The supplied argument type any is not compatible with the definition type."
`;

exports[`Builtin functions round evaluate 1`] = `
"round(number) => -inf | int(-inf..inf) | inf | nan
round(-3.14) => -3
Expand Down
1 change: 1 addition & 0 deletions tests/evaluate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ describe('Builtin functions', () => {

testUnary('number::exp', numbers);
testUnary('number::log', numbers);
testUnary('number::sqrt', numbers);

const ints = numbers.map((n) => intersect(n, INT)).filter((n) => n.type !== 'never');

Expand Down

0 comments on commit 422ca2a

Please sign in to comment.