mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2026-03-13 15:21:15 +08:00
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:
@@ -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
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user