Naruyoko / OmegaNum.js

A huge number library holding up to 10{1000}9e15. A basement for planned {10,x,1,2}.
https://naruyoko.github.io/OmegaNum.js/index.html
MIT License
32 stars 15 forks source link

add(n, -n) is incorrect for numbers layer 1 and higher #4

Closed Patashu closed 5 years ago

Patashu commented 5 years ago
OmegaNum.add(1e15, -1e15).toString()
"-0"
OmegaNum.add(1e16, -1e16).toString()
"-Infinity"

break_eternity.js special cases this and you probably should too, since it holds true no matter how massive your number is. https://github.com/Patashu/break_eternity.js/blob/master/break_eternity.js#L1198

EDIT: Actually, the root cause might be an off by one error when computing layer 1 or higher negative numbers vs computing positive numbers:

new OmegaNum("-1e16").array[0]
16
new OmegaNum("-1e16").array[1]
2
new OmegaNum("1e16").array[0]
16
new OmegaNum("1e16").array[1]
1

Obviously both should be layer 1.

Actually, this bug might be independent, because even if we do a neg instead of construct from string:

OmegaNum.add("e16", new OmegaNum("e16").neg()).toString()
"-Infinity"

yet:

new OmegaNum("e16").neg().array[0]
16
new OmegaNum("e16").neg().array[1]
1
new OmegaNum("e16").array[0]
16
new OmegaNum("e16").array[1]
1
Naruyoko commented 5 years ago

α 1.0.1

Patashu commented 5 years ago

This now works:

OmegaNum.add("ee16", new OmegaNum("ee16").neg()).toString()
"0"

But I can't make negative numbers with toString anymore:

new OmegaNum("-ee16").sign
1
new OmegaNum("-e16").sign
1
new OmegaNum("-1e16").sign
1
new OmegaNum(-1e16).sign
-1
Naruyoko commented 5 years ago

Thonk I need to carry over... Duh. Obviously I'm not great at this.

Naruyoko commented 5 years ago

α 1.0.1.1

Patashu commented 5 years ago

Fixed!