gibbsly / gm

A utility datapack for doing math operations
The Unlicense
33 stars 2 forks source link

Add sqrt function #4

Closed GlitchlessCode closed 8 months ago

GlitchlessCode commented 8 months ago

The Suggestion

Add a square root function (√x) to the set of functions

Implementation description

gibbsly commented 8 months ago

woah, impressive thanks! I gave up on trying to implement this

GlitchlessCode commented 8 months ago

By the way, I ran some test data:

$(x) missing

fail (Correct)

$(x) < 0

fail (Correct)

typeof $(x) !== number

fail (Correct)

$(x) == 0

~0.0f (Correct) (This one was very slightly bigger than 0, but only after a lot zeros. It's effectively equivalent to 0)

$(x) == 0.25f

~0.5f (Correct)

$(x) == 1.0f

~1.0f (Correct)

$(x) == 2.0f

~1.4142135f (Correct) aka ~√2 (Correct)

$(x) == 64.0f

8.0f (Correct)

GlitchlessCode commented 8 months ago

woah, impressive thanks! I gave up on trying to implement this

Ha, thanks. It's not my own algorithm, it's just one of the more common ones out there, but I just figured out how to implement it in MC, that's all

gibbsly commented 8 months ago

still more than I could figure out, I was mostly unmotivated because I only needed it to calculate distance and that has its own operation

GlitchlessCode commented 8 months ago

still more than I could figure out, I was mostly unmotivated because I only needed it to calculate distance and that has its own operation

Fair. I'm personally planning to implement arcsin, arccos and arctan, which is why I wanted the sqrt, cause I couldn't find a simple solution for the inverse trigs that didn't use sqrt, or vice versa.

Which is why we have to settle for an approximation, but it's still very accurate at least.

gibbsly commented 8 months ago

I see, I think you have more ambitions for this repo than I do, I just did the operations I need accurate outputs for, I'll absolutely merge any ops you add though