Skip to content

Commit

Permalink
#5447 - Added fixed precision class, updated multitail calculations w…
Browse files Browse the repository at this point in the history
…ith fixed precision usage (#5450)

- fixed tails movement
  • Loading branch information
daniil-sloboda authored Sep 10, 2024
1 parent d334270 commit 589624b
Show file tree
Hide file tree
Showing 2 changed files with 399 additions and 158 deletions.
63 changes: 63 additions & 0 deletions packages/ketcher-core/src/domain/entities/fixedPrecision.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
export class FixedPrecisionCoordinates {
static MULTIPLIER = 10 ** 5;
public readonly value: number;

static fromFloatingPrecision(value: number) {
return new FixedPrecisionCoordinates(
Math.round(value * FixedPrecisionCoordinates.MULTIPLIER),
);
}

constructor(value: number | FixedPrecisionCoordinates) {
this.value =
value instanceof FixedPrecisionCoordinates ? value.value : value;
}

add(
fixedPrecisionValue: FixedPrecisionCoordinates,
): FixedPrecisionCoordinates {
return new FixedPrecisionCoordinates(
this.value + fixedPrecisionValue.value,
);
}

sub(
fixedPrecisionValue: FixedPrecisionCoordinates,
): FixedPrecisionCoordinates {
return new FixedPrecisionCoordinates(
this.value - fixedPrecisionValue.value,
);
}

multiply(
value: FixedPrecisionCoordinates | number,
): FixedPrecisionCoordinates {
const isFixedPrecision = value instanceof FixedPrecisionCoordinates;
const multiplier = isFixedPrecision ? value.value : value;
const result = this.value * multiplier;
return new FixedPrecisionCoordinates(
Math.round(
isFixedPrecision
? result / FixedPrecisionCoordinates.MULTIPLIER
: result,
),
);
}

divide(value: FixedPrecisionCoordinates | number): FixedPrecisionCoordinates {
const isFixedPrecision = value instanceof FixedPrecisionCoordinates;
const delimiter = isFixedPrecision ? value.value : value;
const result = this.value / delimiter;
return new FixedPrecisionCoordinates(
Math.round(
isFixedPrecision
? result * FixedPrecisionCoordinates.MULTIPLIER
: result,
),
);
}

getFloatingPrecision(): number {
return this.value / FixedPrecisionCoordinates.MULTIPLIER;
}
}
Loading

0 comments on commit 589624b

Please sign in to comment.