src/vs/base/common/numbers.ts

121 LOC · 83 covered · 38 uncovered · 21 ranges · 1887 concepts · 8 introducers · 942 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- numbers.ts ×13
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { assert } from './assert.js';
7 >
8 > export function clamp(value: number, min: number, max: number): number {
9 return Math.min(Math.max(value, min), max);
10 }
12 > /**
13 > * Formats a token count for compact display (e.g. `128K`, `1M`, `1.5M`).
14 > */
15 > export function formatTokenCount(count: number): string {
16 > if (count >= 1_000_000) { numbers.ts ×2
17 > const value = count / 1_000_000; numbers.ts ×1
18 > const floored = Math.floor(value * 10) / 10;
19 > return floored % 1 === 0 ? `${floored.toFixed(0)}M` : `${floored.toFixed(1)}M`;
20 > } else if (count > 900_000) { numbers.ts ×2
21 > return '1M'; numbers.ts ×1
22 > } else if (count >= 1000) { numbers.ts ×1
23 > return `${Math.round(count / 1000)}K`; numbers.ts ×1
24 > }
25 > return count.toString(); numbers.ts ×1
26 > }
28 > export function rot(index: number, modulo: number): number {
29 return (modulo + (index % modulo)) % modulo;
30 }
32 > export class Counter {
33 private _next = 0;
35 > getNext(): number {
36 return this._next++;
37 }
39 >
40 > export class MovingAverage {
41
42 private _n = 1;
43 private _val = 0;
45 > update(value: number): number {
46 this._val = this._val + (value - this._val) / this._n;
47 this._n += 1;
48 return this._val;
49 }
51 > get value(): number {
52 return this._val;
53 }
55 >
56 > export class SlidingWindowAverage {
57 >
58 > private _n: number = 0;
59 > private _val = 0;
60 >
61 > private readonly _values: number[] = [];
62 > private _index: number = 0;
63 > private _sum = 0;
64 >
65 > constructor(size: number) {
66 this._values = new Array(size);
67 this._values.fill(0, 0, size);
68 }
70 > update(value: number): number {
71 const oldValue = this._values[this._index];
72 this._values[this._index] = value;
73 this._index = (this._index + 1) % this._values.length;
74
75 this._sum -= oldValue;
76 this._sum += value;
77
78 if (this._n < this._values.length) {
79 this._n += 1;
80 }
81
82 this._val = this._sum / this._n;
83 return this._val;
84 }
86 > get value(): number {
87 return this._val;
88 }
90 >
91 > /** Returns whether the point is within the triangle formed by the following 6 x/y point pairs */
92 > export function isPointWithinTriangle(
93 > x: number, y: number, numbers.ts ×1
94 > ax: number, ay: number,
95 > bx: number, by: number,
96 > cx: number, cy: number
97 > ) {
98 > const v0x = cx - ax;
99 > const v0y = cy - ay;
100 > const v1x = bx - ax;
101 > const v1y = by - ay;
102 > const v2x = x - ax;
103 > const v2y = y - ay;
104 >
105 > const dot00 = v0x * v0x + v0y * v0y;
106 > const dot01 = v0x * v1x + v0y * v1y;
107 > const dot02 = v0x * v2x + v0y * v2y;
108 > const dot11 = v1x * v1x + v1y * v1y;
109 > const dot12 = v1x * v2x + v1y * v2y;
110 >
111 > const invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
112 > const u = (dot11 * dot02 - dot01 * dot12) * invDenom;
113 > const v = (dot00 * dot12 - dot01 * dot02) * invDenom;
114 >
115 > return u >= 0 && v >= 0 && u + v < 1;
116 > }
118 > export function randomChance(p: number): boolean {
119 assert(p >= 0 && p <= 1, 'p must be between 0 and 1');
120 return Math.random() < p;
121 }