Snippet Natural Functions

Light Alkmst

New Member
Reaction score
20
Earlier this morning, I had realized that there are probably no natural functions in the JASS files, and I figured that I might as well make them in case someone will need them.


e^(x)
JASS:

function e0 takes real x returns real
     return 1. + x + x * x / 2. + x * x * x / 6 + x * x * x * x / 24
endfunction

function e1 takes real x returns real
     return 1. + (x - 1.) + (x - 1.) * (x - 1.) / 2. + (x - 1.) * (x - 1.) * (x - 1.) / 6 + (x - 1.) * (x - 1.) * (x - 1.) * (x - 1.) / 24
endfunction

function eC takes real x, real center returns real
     return 1. + (x - center) + (x - center) * (x - center) / 2. + (x - center) * (x - center) * (x - center) / 6 + (x - center) * (x - center) * (x - center) * (x - center) / 24
endfunction


ln(x)
JASS:

function ln1 takes real x returns real
     return (x - 1.) - (x - 1.) * (x - 1.) / 2. + (x - 1.) * (x - 1.) * (x - 1.) / 3. - (x - 1.) * (x - 1.) * (x - 1.) * (x - 1.) / 4.
endfunction

function lnC takes real x, real center returns real
     return (x - center) - (x - center) * (x - center) / 2. + (x - center) * (x - center) * (x - center) / 3. - (x - center) * (x - center) * (x - center) * (x - center) / 4.
endfunction


All of the functions are more accurate closer to their centers, but the lower functions are a little slower. I'm a little lazy right now, so if someone could check the natural logs for me, it would be great.

The _0 functions are centered at 0
The _1 functions are centered at 1, and is slower than 0
The _C functions are centered at your choice, but will be slower
 

Romek

Super Moderator
Reaction score
964
These should be constant.
 

Jesus4Lyf

Good Idea™
Reaction score
397
JASS:
function eC takes real x, real center returns real
     return e0(x-center)
endfunction


Same applies for most of the others. Spamming the subtraction isn't necessary either way:
JASS:
set x=x-center
return ...


You may also want to explain why you have centering.

>These should be constant.
Yes, as in, constant function ...
 

Romek

Super Moderator
Reaction score
964
> Yes, as in, constant function ...
Obviously.
 

phyrex1an

Staff Member and irregular helper
Reaction score
447
Why would you want the e1, eC and functions when e1(x) = e0(x-1) and eC(x,c) = e0(x-c)?
Even if you want to keep them you could easily extract the constant factors you you don't have to do all those substractions.

e^x can be written as Pow(bj_E, x)

Also, isn't this the taylor series for e and ln? If I remember right they're only good approximations for |x| < 1
 

Light Alkmst

New Member
Reaction score
20
These aren't the values of the actual e number. These are:

e to the power of x

-and-

logorithm base e of x

Also, isn't this the taylor series for e and ln? If I remember right they're only good approximations for |x| < 1

Yes, but moving the center lets it reach out further, and I'm pretty sure more terms let it last longer than |x| < 1. I'll have to check later. If there are already the natural functions provided, then just ignore and graveyard these I guess...

As of the constant functions, I'm really not sure how they work...would it cause all future calls of the function to return the same, even if the input were different?

Also, I'm not sure if the NewGen would inline it if there were local declarations.

You may also want to explain why you have centering.
All of the functions are more accurate closer to their centers
 

phyrex1an

Staff Member and irregular helper
Reaction score
447
These aren't the values of the actual e number.
I obviously mean taylor series for e^x and ln(x) :p

Anyway:
Code:
(0.1, 1.1051708333333332, 1.1051709180756477),
(0.5, 1.6484375, 1.6487212707001282),
(1.0, 2.708333333333333, 2.718281828459045),
(2.5, 10.856770833333332, 12.182493960703473),
(5.0, 65.375, 148.4131591025766)
First number is the x, second is your version of e^x, third is the correct version of e^x. As you can see this is indeed very inaccurate for x > 1.

>Yes, but moving the center lets it reach out further, and I'm pretty sure more terms let it last longer than |x| < 1. I'll have to check later. If there are already the natural functions provided, then just ignore and graveyard these I guess...
Edit: Missed this. Yes it is correct that moving the center makes it work for larger x, I fail to see the usefulness of that however in the context of wc3. Beside Pow(bj_E, x) is correct for all x.
 

Light Alkmst

New Member
Reaction score
20
I obviously mean taylor series for e^x and ln(x) :p

I meant for everyone else. Only you were the only one not clamoring about it. :)

Anyway:
Code:
(0.1, 1.1051708333333332, 1.1051709180756477),
(0.5, 1.6484375, 1.6487212707001282),
(1.0, 2.708333333333333, 2.718281828459045),
(2.5, 10.856770833333332, 12.182493960703473),
(5.0, 65.375, 148.4131591025766)
First number is the x, second is your version of e^x, third is the correct version of e^x. As you can see this is indeed very inaccurate for x > 1.

I think adding more terms increases accuracy even outside of x > 1, but I agree it does disappear very quickly even if it does.

>Yes, but moving the center lets it reach out further, and I'm pretty sure more terms let it last longer than |x| < 1. I'll have to check later. If there are already the natural functions provided, then just ignore and graveyard these I guess...
Edit: Missed this. Yes it is correct that moving the center makes it work for larger x, I fail to see the usefulness of that however in the context of wc3. Beside Pow(bj_E, x) is correct for all x.

I hadn't known that they even had a bj_E :eek:, and didn't think of using Pow(). At least ln() will be useful, but if there is already a function for ln() as well, then graveyard this then.
 

Jesus4Lyf

Good Idea™
Reaction score
397
If your functions have inaccuracies, you should really comment on how to avoid them/where they apply in more detail in your original post... <_<

>Also, I'm not sure if the NewGen would inline it if there were local declarations.
You shouldn't really worry about that when you're repeating the same calculation that many times.
I expect any advantage of inlining wouldn't be worth it in comparison.
 

Light Alkmst

New Member
Reaction score
20
If your functions have inaccuracies, you should really comment on how to avoid them/where they apply in more detail in your original post... <_<

I said use the ones that move the center, and set the center closer to where you need to calculate for.
 

Artificial

Without Intelligence
Reaction score
326
> I'm not sure if the NewGen would inline it if there were local declarations.
It isn't inlining these now, either. The rules of inlining are more complicated than that the function has to be a one-liner. One of the rules is this:
JassHelper Manual said:
Every argument must be evaluated once and only once by the function, in the same order as they appear in the arguments list.
These functions are evaluating the arguments more than once, so they won't be inlined. ;p

And eC doesn't even compile (one "center" is "enter"). o_O
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top