Skip to content

Commit e87e676

Browse files
stepankuzmingithub-actions[bot]
authored andcommitted
Use Math.log2 instead of Math.log / Math.LN2 (internal-6161)
GitOrigin-RevId: e3b43cb7e4a36dbb1cbee1b3f9e2458a988c8cc7
1 parent 3c315a8 commit e87e676

7 files changed

Lines changed: 10 additions & 10 deletions

File tree

src/geo/projection/adjustments.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function getProjectionInterpolationT(
4747
// The interpolation ranges are manually defined based on what makes
4848
// sense in a 1024px wide map. Adjust the ranges to the current size
4949
// of the map. The smaller the map, the earlier you can start unskewing.
50-
const rangeAdjustment = Math.log(size / 1024) / Math.LN2;
50+
const rangeAdjustment = Math.log2(size / 1024);
5151
const zoomA = range[0] + rangeAdjustment;
5252
const zoomB = range[1] + rangeAdjustment;
5353
const t = smoothstep(zoomA, zoomB, zoom);
@@ -80,7 +80,7 @@ export function getZoomAdjustment(projection: Projection, loc: LngLat) {
8080

8181
const scale = Math.sqrt((mdx * mdx + mdy * mdy) / (pdx * pdx + pdy * pdy));
8282

83-
return Math.log(scale) / Math.LN2;
83+
return Math.log2(scale);
8484
}
8585

8686
function getShearAdjustment(projection: Projection, zoom: number, loc: LngLat, interpT: number, withoutRotation?: boolean) {

src/geo/transform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,7 @@ class Transform {
15511551
get unmodified(): boolean { return this._unmodified; }
15521552

15531553
zoomScale(zoom: number): number { return Math.pow(2, zoom); }
1554-
scaleZoom(scale: number): number { return Math.log(scale) / Math.LN2; }
1554+
scaleZoom(scale: number): number { return Math.log2(scale); }
15551555

15561556
// Transform from LngLat to Point in world coordinates [-180, 180] x [90, -90] --> [0, this.worldSize] x [0, this.worldSize]
15571557
project(lnglat: LngLat): Point {

src/source/image_source.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ export function getCoordinatesCenterTileID(coords: Array<MercatorCoordinate>): C
841841
const dx = maxX - minX;
842842
const dy = maxY - minY;
843843
const dMax = Math.max(dx, dy);
844-
const zoom = Math.max(0, Math.floor(-Math.log(dMax) / Math.LN2));
844+
const zoom = Math.max(0, Math.floor(-Math.log2(dMax)));
845845
const tilesAtZoom = Math.pow(2, zoom);
846846

847847
let x = Math.floor((minX + maxX) / 2 * tilesAtZoom);

src/style-spec/expression/definitions/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ CompoundExpression.register(expressions, {
388388
'log2': [
389389
NumberType,
390390
[NumberType],
391-
(ctx, [n]) => Math.log(n.evaluate(ctx)) / Math.LN2
391+
(ctx, [n]) => Math.log2(n.evaluate(ctx))
392392
],
393393
'sin': [
394394
NumberType,

src/ui/handler/touch_zoom_rotate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ function getTouchById(mapTouches: Array<Touch>, points: Array<Point>, identifier
105105
const ZOOM_THRESHOLD = 0.1;
106106

107107
function getZoomDelta(distance: number, lastDistance: number) {
108-
return Math.log(distance / lastDistance) / Math.LN2;
108+
return Math.log2(distance / lastDistance);
109109
}
110110

111111
export class TouchZoomHandler extends TwoTouchHandler {

src/util/util.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ export function uuid(): string {
362362
* @private
363363
*/
364364
export function isPowerOfTwo(value: number): boolean {
365-
return (Math.log(value) / Math.LN2) % 1 === 0;
365+
return (Math.log2(value)) % 1 === 0;
366366
}
367367

368368
/**
@@ -371,7 +371,7 @@ export function isPowerOfTwo(value: number): boolean {
371371
*/
372372
export function nextPowerOfTwo(value: number): number {
373373
if (value <= 1) return 1;
374-
return Math.pow(2, Math.ceil(Math.log(value) / Math.LN2));
374+
return Math.pow(2, Math.ceil(Math.log2(value)));
375375
}
376376

377377
/**
@@ -380,7 +380,7 @@ export function nextPowerOfTwo(value: number): number {
380380
*/
381381
export function prevPowerOfTwo(value: number): number {
382382
if (value <= 1) return 1;
383-
return Math.pow(2, Math.floor(Math.log(value) / Math.LN2));
383+
return Math.pow(2, Math.floor(Math.log2(value)));
384384
}
385385

386386
/**

test/unit/geo/transform.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('transform', () => {
3636
expect(transform.size.equals(new Point(500, 500))).toEqual(true);
3737
expect(transform.centerPoint.equals(new Point(250, 250))).toEqual(true);
3838
expect(transform.scaleZoom(0)).toEqual(-Infinity);
39-
expect(transform.scaleZoom(10)).toEqual(3.3219280948873626);
39+
expect(transform.scaleZoom(10)).toEqual(3.321928094887362);
4040
expect(transform.point).toEqual(new Point(262144, 262144));
4141
expect(transform.height).toEqual(500);
4242
expect(fixedLngLat(transform.pointLocation(new Point(250, 250)))).toEqual({lng: 0, lat: -0});

0 commit comments

Comments
 (0)