-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#5447 - Added fixed precision class, updated multitail calculations w…
…ith fixed precision usage (#5450) - fixed tails movement
- Loading branch information
1 parent
d334270
commit 589624b
Showing
2 changed files
with
399 additions
and
158 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
packages/ketcher-core/src/domain/entities/fixedPrecision.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.