RomanZhu / FixedPoint-Sharp

Fixed point math with 48.16 precision (based on lib by https://github.com/fholm)
MIT License
168 stars 40 forks source link

The cos LUT is out of range when the angle is close to 90° or 270°. #6

Closed Zonciu closed 3 years ago

Zonciu commented 3 years ago

When the angle is close to 90 degrees or 270 degrees, cos LUT out of range.

Test Code

static void cos()
{
    using var str = new FileStream(@"cos.txt", FileMode.OpenOrCreate);
    using var wr = new StreamWriter(str, Encoding.UTF8);
    for (var i = (-fp.pi2).value; i <= fp.pi2.value; i++)
    {
        var angle = fp.ParseRaw(i);
        wr.Write($"raw: {i}, rad: {angle.ToString()}, ");
        var fcos = Math.Cos(angle.AsDouble);

        try
        {
            var cos = fixmath.Cos(angle);
            wr.WriteLine($"cos: {cos.ToString()}, fcos: {fcos:F5}");
        }
        catch (Exception e)
        {
            wr.WriteLine($"failed: {e.Message}, fcos: {fcos:F5}");
        }
    }
}

Result cos.txt

raw: -411774, rad: -6.28317, cos: 1.00000, fcos: 1.00000
raw: -411773, rad: -6.28316, cos: 0.99998, fcos: 1.00000
raw: -411772, rad: -6.28314, cos: 0.99998, fcos: 1.00000

......

raw: -308845, rad: -4.71260, cos: 0.00009, fcos: 0.00021
raw: -308844, rad: -4.71259, cos: 0.00009, fcos: 0.00020
raw: -308843, rad: -4.71257, cos: 0.00009, fcos: 0.00018
raw: -308842, rad: -4.71255, failed: Index was outside the bounds of the array., fcos: 0.00017
raw: -308841, rad: -4.71254, failed: Index was outside the bounds of the array., fcos: 0.00015
raw: -308840, rad: -4.71252, failed: Index was outside the bounds of the array., fcos: 0.00014
raw: -308839, rad: -4.71251, failed: Index was outside the bounds of the array., fcos: 0.00012
raw: -308838, rad: -4.71249, failed: Index was outside the bounds of the array., fcos: 0.00010
raw: -308837, rad: -4.71248, failed: Index was outside the bounds of the array., fcos: 0.00009
raw: -308836, rad: -4.71246, cos: -0.00011, fcos: 0.00007
raw: -308835, rad: -4.71245, cos: -0.00011, fcos: 0.00006
raw: -308834, rad: -4.71243, cos: -0.00011, fcos: 0.00004

......

raw: 308840, rad: 4.71252, cos: -0.00011, fcos: 0.00014
raw: 308841, rad: 4.71254, cos: -0.00011, fcos: 0.00015
raw: 308842, rad: 4.71255, cos: -0.00011, fcos: 0.00017
raw: 308843, rad: 4.71257, failed: Index was outside the bounds of the array., fcos: 0.00018
raw: 308844, rad: 4.71259, failed: Index was outside the bounds of the array., fcos: 0.00020
raw: 308845, rad: 4.71260, failed: Index was outside the bounds of the array., fcos: 0.00021
raw: 308846, rad: 4.71262, failed: Index was outside the bounds of the array., fcos: 0.00023
raw: 308847, rad: 4.71263, failed: Index was outside the bounds of the array., fcos: 0.00024
raw: 308848, rad: 4.71265, failed: Index was outside the bounds of the array., fcos: 0.00026
raw: 308849, rad: 4.71266, cos: 0.00009, fcos: 0.00027
raw: 308850, rad: 4.71268, cos: 0.00009, fcos: 0.00029
raw: 308851, rad: 4.71269, cos: 0.00009, fcos: 0.00030

......

raw: 411772, rad: 6.28314, cos: 0.99998, fcos: 1.00000
raw: 411773, rad: 6.28316, cos: 0.99998, fcos: 1.00000
raw: 411774, rad: 6.28317, cos: 1.00000, fcos: 1.00000
RomanZhu commented 3 years ago

Do you have the latest version? I pushed the fix for something similar yesterday

On Wed, 30 Dec 2020, 09:49 Zonciu Liang, notifications@github.com wrote:

When the angle is close to 90 degrees or 270 degrees, cos LUT out of range. Test Code

static void cos() { using var str = new FileStream(@"cos.txt", FileMode.OpenOrCreate); using var wr = new StreamWriter(str, Encoding.UTF8); for (var i = (-fp.pi2).value; i <= fp.pi2.value; i++) { var angle = fp.ParseRaw(i); wr.Write($"raw: {i}, rad: {angle.ToString()}, "); var fcos = Math.Cos(angle.AsDouble);

    try
    {
        var cos = fixmath.Cos(angle);
        wr.WriteLine($"cos: {cos.ToString()}, fcos: {fcos:F5}");
    }
    catch (Exception e)
    {
        wr.WriteLine($"failed: {e.Message}, fcos: {fcos:F5}");
    }
}

}

Result cos.txt

raw: -411774, rad: -6.28317, cos: 1.00000, fcos: 1.00000 raw: -411773, rad: -6.28316, cos: 0.99998, fcos: 1.00000 raw: -411772, rad: -6.28314, cos: 0.99998, fcos: 1.00000

......

raw: -308845, rad: -4.71260, cos: 0.00009, fcos: 0.00021 raw: -308844, rad: -4.71259, cos: 0.00009, fcos: 0.00020 raw: -308843, rad: -4.71257, cos: 0.00009, fcos: 0.00018 raw: -308842, rad: -4.71255, failed: Index was outside the bounds of the array., fcos: 0.00017 raw: -308841, rad: -4.71254, failed: Index was outside the bounds of the array., fcos: 0.00015 raw: -308840, rad: -4.71252, failed: Index was outside the bounds of the array., fcos: 0.00014 raw: -308839, rad: -4.71251, failed: Index was outside the bounds of the array., fcos: 0.00012 raw: -308838, rad: -4.71249, failed: Index was outside the bounds of the array., fcos: 0.00010 raw: -308837, rad: -4.71248, failed: Index was outside the bounds of the array., fcos: 0.00009 raw: -308836, rad: -4.71246, cos: -0.00011, fcos: 0.00007 raw: -308835, rad: -4.71245, cos: -0.00011, fcos: 0.00006 raw: -308834, rad: -4.71243, cos: -0.00011, fcos: 0.00004

......

raw: 308840, rad: 4.71252, cos: -0.00011, fcos: 0.00014 raw: 308841, rad: 4.71254, cos: -0.00011, fcos: 0.00015 raw: 308842, rad: 4.71255, cos: -0.00011, fcos: 0.00017 raw: 308843, rad: 4.71257, failed: Index was outside the bounds of the array., fcos: 0.00018 raw: 308844, rad: 4.71259, failed: Index was outside the bounds of the array., fcos: 0.00020 raw: 308845, rad: 4.71260, failed: Index was outside the bounds of the array., fcos: 0.00021 raw: 308846, rad: 4.71262, failed: Index was outside the bounds of the array., fcos: 0.00023 raw: 308847, rad: 4.71263, failed: Index was outside the bounds of the array., fcos: 0.00024 raw: 308848, rad: 4.71265, failed: Index was outside the bounds of the array., fcos: 0.00026 raw: 308849, rad: 4.71266, cos: 0.00009, fcos: 0.00027 raw: 308850, rad: 4.71268, cos: 0.00009, fcos: 0.00029 raw: 308851, rad: 4.71269, cos: 0.00009, fcos: 0.00030

......

raw: 411772, rad: 6.28314, cos: 0.99998, fcos: 1.00000 raw: 411773, rad: 6.28316, cos: 0.99998, fcos: 1.00000 raw: 411774, rad: 6.28317, cos: 1.00000, fcos: 1.00000

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/RomanZhu/FixedPoint-Sharp/issues/6, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC3V5VTX3OZHEZBJ6Z2L6TLSXLSRFANCNFSM4VOC72QQ .

Zonciu commented 3 years ago

This is the latest version, after issue 4 was fixed Test project here: Test.zip

RomanZhu commented 3 years ago

bah, in the fix it was like this

if (value > 65536) {
   value -= 65536;
}

shoud've been

if (value >= 65536) {
   value -= 65536;
}

pushed the fix and adjusted my tests