Skip to content

need check pow2 num is neg #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions FixedPointSharp/fixnum/fixmath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public static fp Pow2(fp num) {
if (num.value > 1638400) {
return fp.max;
}


var isNeg = num.value < 0;
if (isNeg) num = -num;

var i = num.AsInt;
num = Fractions(num) * _pow2Number1 + fp._1;
num *= num;
Expand All @@ -46,7 +49,10 @@ public static fp Pow2(fp num) {
num *= num;
num *= num;
num *= num;
return num * num * fp.Parse(1 << i);

var result = num * num * fp.Parse(1 << i);
if (isNeg) result = fp._1 / result;
return result;
}

///Approximate version of Exp
Expand Down Expand Up @@ -334,4 +340,4 @@ public static fp Exp(fp num) {
return result;
}
}
}
}