Help with Lua Cosine, incorrect results returned

Well, first, this is NOT for a class, test, or other type of student type.

I am a screenwriter for a game, and I am trying to implement a math library for everyone, and unfortunately, all I have for me is a very simple lua. The embedded version cannot be modified and does not contain libraries. For those who are wondering for scripting at Fold.It.

Here is what I have ...

math={}
math.fact = function(b) if(b==1)or(b==0) then return 1 end e=1 for c=b,1,-1 do e=e*c end return e end
math.pow = function(b,p) e=b if(p==0) then return 1 end if(p<0) then p=p*(-1) end for c=p,2,-1 do e=e*b end return e end
math.cos = function(b,p) e=0 p=p or 10 for i=1,p do e=e+(math.pow(-1,i)*math.pow(b,2*i)/math.fact(2*i)) end return e end

To clarify above, math.fact returns a factorial, which is returned with an accuracy of 10 points of accuracy and is a new function that I made to calculate the cosine.

Math.pow is also a new feature for handling returning credentials, also working as expected.

The problem is with the cosine function. Its returning unexpected meanings. It’s easier to digest the version (I wrote my material in the hyperfine library) ...

function math.cos(value,precision) 
    result=0 
    precision=precision or 10 
    for i=1,precision do 
        result=result+(math.pow(-1,i)*math.pow(value,2*i)/math.fact(2*i)) 
    end 
    return e 
end

The problem is that with these print functions (math.cos (90)) it returns 4.77135 ... when I expect -0.44807 ... (based on calc in scientific mode or using the online tool to cos (90) )

I also have problems with sin and tanning, but they are similarly written in cos, which seems to have been done in many languages. If I can understand what I'm doing wrong, I can fix them all.

EDIT: fixed typo

+3
source share
1 answer

-, lua . -, . -, .

, , , , . , . :

pi. while, , () < 2 * :

math.pi = 3.14159265358
while value > math.pi*2 do 
  value = value - math.pi * 2 
end
while value < -math.pi*2 do
  value = value + math.pi * 2
end

- fmod lua.

( ):

math={}
math.fact = function(b)
    if(b==1)or(b==0) then
        return 1
    end
    local e=1
    for c=b,1,-1 do
        e=e*c
    end
    return e
end

math.pow = function(b,p)
    local e=b
    if(p==0) then
        return 1
    end
    if(p<0) then
        p=p*(-1)
    end
    for c=p,2,-1 do
        e=e*b
    end
    return e
end
math.cos = function(b,p)
    local e=1 
    b = math.correctRadians(b) 
    p=p or 10
    for i=1,p do
        e=e+(math.pow(-1,i)*math.pow(b,2*i)/math.fact(2*i))
    end
    return e
end

math.pi = 3.1415926545358
math.correctRadians = function( value )
    while value > math.pi*2 do
        value = value - math.pi * 2
    end           
    while value < -math.pi*2 do
        value = value + math.pi * 2
    end 
    return value
end 

lua:

imac:~ root$ lua -i temp.lua 
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> print( math.cos( 90 ) )
-0.44807359244883
> 
+3

Source: https://habr.com/ru/post/1770604/


All Articles