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
963
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
963
> 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.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top