Log In  

Since PICO-8's sin(), cos() and atan2() are not the standard functions, how to implement them in another language? Some pseudocode would be ok, I'm aiming javascript by the way.

I looked into PicoLove's code as an example, but the results are different. For example in PICO-8:

print(sin(0)) -- 0
print(sin(0.5)) -- 0
print(sin(0.25)) -- -1
print(sin(-3)) -- 0
print(sin(-0.45)) -- 0.309
print(sin(78.4)) -- 0.5877

The PicoLove code remaps sin() to:

sin = function(x) return math.sin(-(x or 0)*(math.pi*2)) end

that in javascript is:

window.sinp8 = function (x) { return Math.sin(-(x || 0) * (Math.PI * 2)); };

Now, the results are very different:

    console.log(sinp8(0));     // -0 
    console.log(sinp8(0.5));   // -1.2246467991473532e-16 
    console.log(sinp8(0.25));  // -1 
    console.log(sinp8(-3));    // -7.347880794884119e-16 
    console.log(sinp8(-0.45)); // 0.3090169943749475 
    console.log(sinp8(78.4));  // 0.5877852522924427 

Can you provide me with some working pseudocode for these three functions? Thanks

P#14774 2015-10-01 06:23 ( Edited 2015-10-03 16:49)

I think results of P8's sin is "normalized" i.e. made so it is within -1..1 range. Try normalizing your function and I think it should be close enough.

P#14778 2015-10-01 08:24 ( Edited 2015-10-01 12:24)

@darkhog do, or do not, there is no think

P#14800 2015-10-01 13:48 ( Edited 2015-10-01 17:48)

Looks correct to me except for the last value, which I suspect was copy pasted without the negative sign.

P#14857 2015-10-02 10:16 ( Edited 2015-10-02 14:16)

sin and cos of negative angles are the same as sin and cos of positive angles. always.

pico8 sin and cos take values from 0 to 1 instead of 0 to 2pi so if you want to implement it in an other language, just multiply the input angle by 2pi.

print(sin(0)) -- 0
print(sin(0.5)) -- 0 ... 0.5 on pico is the same as pi in math sin(pi)==0
print(sin(0.25)) -- -1 ... 0.25 on pico equals pi/2 in math.
print(sin(-3)) -- 0 ... -3 is the same as 0, 1, 2, 3 or -1, -2, -3 ... just 0 ... 1 is a full period (2*pi)
print(sin(-0.45)) -- 0.309 ... -0.45 is the same as 0.45
print(sin(78.4)) -- 0.5877 ... 78.4 is just 0.4

Here's a visualisation that might help:

Cart #14870 | 2015-10-02 | Code ▽ | Embed ▽ | No License

P#14866 2015-10-02 13:12 ( Edited 2015-10-02 18:05)

pistacchio, in reality your results are not very different, and they are both correct.
when you see "-1.2246467991473532e-16" in javascript it's mean "-1.22(...) * 10 ^ -16", so "0.000000000000000122(...)", it's nearly 0.
the difference comes from the way that floats are stores in 32bits (https://en.wikipedia.org/wiki/Single-precision_floating-point_format).
Try a simple operation like "1.05 - 1" in javascript and the result will be "0.050000000000000044" instead of "0.05" :)

P#14924 2015-10-03 12:49 ( Edited 2015-10-03 16:49)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-18 04:41:44 | 0.038s | Q:25