datai-sdk 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/utils/mathUtils.ts +17 -6
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "datai-sdk",
3
3
  "main": "index.ts",
4
- "version": "1.0.4",
4
+ "version": "1.0.5",
5
5
  "description": "Datai SDK which has useful libraries to help building projections",
6
6
  "scripts": {
7
7
  "postinstall": "node postinstall-script.js"
@@ -6,17 +6,28 @@ const MIN_DECIMALS = -18
6
6
 
7
7
  export function pow(value: BigDecimal, power: i32): BigDecimal {
8
8
  if (power == 0) {
9
- return BD_1
9
+ return BD_1 // Any number to the power of 0 is 1
10
10
  }
11
+
11
12
  const negativePower = power < 0
12
- let result = BD_0.plus(value)
13
- const powerAbs = Math.abs(power)
14
- for (let i = 1; i < powerAbs; i++) {
15
- result = result.times(value) //TODO: move to host side
13
+ let result = BD_1 // Start with 1
14
+ let base = value
15
+ let powerAbs = power < 0 ? -power : power // Use absolute value of power
16
+
17
+ while (powerAbs > 0) {
18
+ if (powerAbs & 1) {
19
+ // Bitwise check for oddness
20
+ result = result.times(base)
21
+ }
22
+ base = base.times(base)
23
+ powerAbs >>= 1 // Bitwise right shift is equivalent to divide by 2
16
24
  }
17
25
 
18
26
  if (negativePower) {
19
- result = safeDiv(BD_1, result)
27
+ if (result.equals(BD_0)) {
28
+ throw new Error('Division by zero in power calculation')
29
+ }
30
+ result = safeDiv(BD_1, result) // Handle negative powers
20
31
  }
21
32
 
22
33
  return result