merge: Improved Pow function (#911)

* feat: add negative power option

* docs: add js doc for powOn function

* feat: add PowFaster with faster algorithm, complexity O(logN)

* chore: rename to exponent

* chore: rename fixed

* style: formated with standard
This commit is contained in:
Fahim Faisaal
2022-03-02 09:36:40 +06:00
committed by GitHub
parent e8de031539
commit ab06131656
2 changed files with 89 additions and 12 deletions

View File

@@ -1,15 +1,41 @@
import { pow } from '../Pow'
import { powLinear, powFaster } from '../Pow'
describe('Pow', () => {
describe('Testing powLinear function', () => {
it('should return 1 for numbers with exponent 0', () => {
expect(pow(2, 0)).toBe(1)
expect(powLinear(2, 0)).toBe(1)
})
it('should return 0.5 for numbers with exponent -1', () => {
expect(powLinear(2, -1)).toBe(0.5)
})
it('should return 0 for numbers with base 0', () => {
expect(pow(0, 23)).toBe(0)
expect(powLinear(0, 23)).toBe(0)
})
it('should return the base to the exponent power', () => {
expect(pow(24, 4)).toBe(331776)
expect(powLinear(24, 4)).toBe(331776)
})
})
describe('Testing powFaster function', () => {
it('should return 1 for numbers with exponent 0', () => {
expect(powFaster(2, 0)).toBe(1)
})
it('should return 0.5 for numbers with exponent -1', () => {
expect(powFaster(2, -1)).toBe(0.5)
})
it('should return 0 for numbers with base 0', () => {
expect(powFaster(0, 23)).toBe(0)
})
it('should return the base to the exponent power', () => {
expect(powFaster(24, 4)).toBe(331776)
})
it('should return the result in O(lonN) complexity', () => {
expect(powFaster(2, 64)).toBe(18446744073709552000) // execution time Math.log2(64) -> 6
})
})